26 lines
510 B
Python
26 lines
510 B
Python
from scipy.io import arff
|
|
import numpy as np
|
|
from sklearn.cluster import KMeans
|
|
from sklearn.datasets import make_blobs
|
|
import matplotlib.pyplot as plt
|
|
|
|
#K_means algorithm
|
|
|
|
n_clusters = 3
|
|
|
|
data = arff.loadarff('2d-4c-no9.arff')[0]
|
|
data_final = []
|
|
x_list = []
|
|
y_list = []
|
|
|
|
for (x, y, z) in data :
|
|
x_list.append(x)
|
|
y_list.append(y)
|
|
data_final.append([x,y])
|
|
|
|
kmeans = KMeans(n_clusters, init='k-means++').fit(data_final)
|
|
|
|
colors = kmeans.labels_
|
|
|
|
plt.scatter(x_list, y_list, c=colors, s=5)
|
|
plt.show()
|