コード例 #1
0
def init_matrix_kmeans(R, M, K):
    ''' Return a matrix initialised by running K-means on R, with K clusters. '''
    assert R is not None and M is not None, "Want to do K-means init but R or M is None."

    #print "Initialising matrix using KMeans."
    kmeans_matrix = KMeans(X=R, M=M, K=K)
    kmeans_matrix.initialise()
    kmeans_matrix.cluster()
    return kmeans_matrix.clustering_results + 0.2
コード例 #2
0
ファイル: index.py プロジェクト: KShaz/ox-patient
def kmeans():
    filename = 0
    threshold = 0

    numClusters = 4
    filename = 'data/4clusters.csv'
            
    kmeans = KMeans(filename, numClusters)
    clusters = kmeans.cluster()

    formatted = dict()
    for i, cluster in enumerate(clusters):
        formatted[i] = []
        for point in cluster:
            #f_cluster = dict()
            #f_cluster[point[0]] = point[1]
            #formatted[i].append(f_cluster)
            formatted[i].append(point)

    print formatted
    return {'clusters':formatted, 'k':len(formatted), 'get_url': app.get_url}
コード例 #3
0
def test_kmeans(db, k=2):
    kmeans = KMeans(db[:500], k)
    clusters = kmeans.cluster()
    plot_clusters(clusters)
    return clusters
コード例 #4
0
ファイル: main.py プロジェクト: milu-buet/DataMinning
# Md Lutfar Rahman
# [email protected]
# DataMining Assingment 4



from kmeans import Point, Cluster, KMeans
import random
from UserMatrix import UserMatrix

userMat = UserMatrix()
points = userMat.userpoints
fet = list(range(len(userMat.movieIds)))
k=3
#print(fet)
Point.set_features(*fet)

model = KMeans(points, k, 0.001)
model.cluster()
#print("clustring>>ended")
print('')
model.getIntraCentriodDensity()
print('')
model.getInterCentroidDensity()
コード例 #5
0
ファイル: main.py プロジェクト: lizj14/learn_ml
def test_kmeans():
    k = KMeans(10, test=True)
    dots, centers = generate_centered_values(2, 100, 10)
    print('real: {}'.format(centers))
    get = k.cluster(dots, 2, 10)
    print('get: {}'.format(get))
コード例 #6
0
    data = loadmat('ex7data2.mat')
    X = data['X']
    kmeans = KMeans(X, 3)
    initial_centroids = np.array([[3, 3], [6, 2], [8, 5]])
    colors = kmeans.color(initial_centroids)
    assert np.array_equal(colors[0:3], np.array([0, 2, 1]))

    # ==================== Part 1.2 Finding Centroids ====================
    centroids = kmeans.compute_centroids(colors)
    expected_centroids = np.array([[2.428301, 3.157924], [5.813503, 2.633656],
                                   [7.119387, 3.616684]])
    assert np.allclose(centroids, expected_centroids)

    # ==================== Part 1.3 K-Means ====================
    kmeans.update_parameters(X=X, k=3, max_iters=10)
    kmeans.cluster()
    expected_mu = np.array([[1.9540, 5.0256], [6.0337, 3.0005],
                            [3.0437, 1.0154]])
    assert np.all([
        np.any([np.allclose(i, j, atol=1e-4) for j in kmeans.mu])
        for i in expected_mu
    ])

    styles = ['bo', 'ro', 'go']
    colors = [0, 1, 2]
    for color, style in zip(colors, styles):
        c = X[np.where(color == kmeans.c)]
        plt.plot(c[:, 0], c[:, 1], style)
        plt.plot(kmeans.mu[color, 0], kmeans.mu[color, 1], 'kx')
    plt.show()
コード例 #7
0
ファイル: test_kmeans.py プロジェクト: milu-buet/DataMinning
    with open(file_name) as f:
        header = f.readline()
        points = []
        for line in f:
            items = line.strip().split(',')
            r = (float(items[0]), float(items[1]), float(items[2]),
                 float(items[3]), items[4])
            points.append(Point(r))
    random.shuffle(points)
    return points


#-----------------------------------------------------------

Point.set_features(0, 1)
points = get_iris_data()

#C1 = Cluster(points[0])
km = KMeans(points, 3)
km.cluster()
km.show()
# for p in points:
# 	print(p)
#a = points[0]
#b = points[1]
#print(a,b)
# print('The distance between a and b:', a.RMS(b))

# model = KMeans(points, 3)
# model.cluster()