Skip to content

Commit

Permalink
pecok poc on point clustering & sbm
Browse files Browse the repository at this point in the history
  • Loading branch information
martinroyer committed Oct 18, 2017
1 parent 00335fa commit 58120d2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
5 changes: 0 additions & 5 deletions test.py

This file was deleted.

24 changes: 24 additions & 0 deletions tests/test_pointclustering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

import numpy as np
from sklearn import cluster
from pecok import pecok_clustering

seed = 432
np.random.seed(seed)
print("seed is %i" % seed)

n_samples = 10
n_features = 100
truth = np.concatenate((np.repeat(0, n_samples//2), np.repeat(1, n_samples//2)))
X = np.zeros((n_samples, n_features))
X[:n_samples//2, :] = np.ones(n_features)*0.1 + np.random.normal(scale=1, size=(n_samples//2, n_features))
X[n_samples//2:, :] = -np.ones(n_features)*0.1 + np.random.normal(scale=0.1, size=(n_samples//2, n_features))

Bhat = pecok_clustering.cluster(X, 2)
kMeans = cluster.KMeans(n_clusters=2, init='k-means++', n_init=100, copy_x=True)
print("truth:".ljust(10), truth)
print("pecok:".ljust(10), kMeans.fit(Bhat).labels_)
print("kmeans:".ljust(10), kMeans.fit(X).labels_)
29 changes: 29 additions & 0 deletions tests/test_sbm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

import numpy as np
from sklearn import cluster
from pecok import pecok_clustering

seed = 432
np.random.seed(seed)
print("seed is %i" % seed)

n_samples = 10
p,q = 0.7, 0.3
truth = np.concatenate((np.repeat(0, n_samples//2), np.repeat(1, n_samples//2)))
expA = q*np.ones((n_samples, n_samples))
expA[:n_samples//2,:n_samples//2] += p-q
expA[n_samples//2:,n_samples//2:] += p-q
A = np.vectorize(lambda p : np.random.binomial(1, p))(expA)
A.flat[::n_samples+1] = 10
i_lower = np.tril_indices(n_samples, -1)
A[i_lower] = A.T[i_lower]
print(A)

Bhat = pecok_clustering.cluster(A, 2)
kMeans = cluster.KMeans(n_clusters=2, init='k-means++', n_init=100, copy_x=True)
print("truth:".ljust(10), truth)
print("pecok:".ljust(10), kMeans.fit(Bhat).labels_)
print("kmeans:".ljust(10), kMeans.fit(A).labels_)

0 comments on commit 58120d2

Please sign in to comment.