Skip to content

Instantly share code, notes, and snippets.

@thinkler
Created June 11, 2017 11:04
Show Gist options
  • Save thinkler/c28f43b09028e61e46e264744c77da02 to your computer and use it in GitHub Desktop.
Save thinkler/c28f43b09028e61e46e264744c77da02 to your computer and use it in GitHub Desktop.
print(__doc__)
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import csv
from sklearn.cluster import KMeans
from sklearn import datasets
centers = [[1, 1], [-1, -1], [1, -1]]
iris = datasets.load_iris()
X = iris.data
y = iris.target
arr = []
with open('dgma5.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
row = [float(i.replace(",",".")) for i in row]
arr.append(row)
arr = np.array(arr)
kmeans = KMeans(n_clusters = 3)
fig = plt.figure(figsize=(12, 8))
plt.clf()
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
plt.cla()
kmeans.fit(arr)
centroids = kmeans.cluster_centers_
labels = kmeans.labels_
clasters = kmeans.clasters_
print("Centroids:")
print(centroids)
ax.scatter(arr[:, 1], arr[:, 0], arr[:, 2], c=labels.astype(np.float))
for i in range(3):
ax.scatter(centroids[i,1], centroids[i,0], centroids[i,2], s=200, c="black", marker="D")
ax.set_xlabel('Petal width')
ax.set_ylabel('Sepal length')
ax.set_zlabel('Petal length')
# fignum = fignum + 1
# Plot the ground truth
# fig = plt.figure(fignum, figsize=(10, 6))
# plt.clf()
# ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
# plt.cla()
# for name, label in [('Setosa', 0),
# ('Versicolour', 1),
# ('Virginica', 2)]:
# ax.text3D(X[y == label, 3].mean(),
# X[y == label, 0].mean() + 1.5,
# X[y == label, 2].mean(), name,
# horizontalalignment='center',
# bbox=dict(alpha=.5, edgecolor='w', facecolor='w'))
# Reorder the labels to have colors matching the cluster results
# y = np.choose(y, [1, 2, 0]).astype(np.float)
# print(y)
# ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=y)
# ax.set_xlabel('Petal width')
# ax.set_ylabel('Sepal length')
# ax.set_zlabel('Petal length')
plt.show()
#Обдумать разные варианты кластеризации
# Центроида - DONE
# Вывести кластеры в таблицы - CURRENT
# Дополнительные характеристики - RESEARCH
# Условия окончания кластеризации - RESEARCH
# Сколько параметров свернулось в фактор и какие параметры
# Прогнать старые данные
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment