在spark中用k-means做聚类,利用t-sne做可视化
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import pandas as pd
mdl_new = pd.read_csv("D:\\data\\resFea\\resFea.csv")
mdl_new22 = pd.read_csv("D:\\data\\resFea\\resFea22.csv")
r1 = pd.concat([mdl_new, pd.Series(mdl_new22['_2'], index = mdl_new.index)], axis = 1)
r1.columns = list(mdl_new.columns) + [u'_2']
def plot_points(colors):
tsne = TSNE(n_components=2, perplexity=5)
tsne = pd.DataFrame(tsne.fit_transform(mdl_new))
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
d = tsne[r1[u'_2'] == 0]
plt.plot(d[0], d[1], 'y.')
d = tsne[r1[u'_2'] == 1]
plt.plot(d[0], d[1], 'go')
d = tsne[r1[u'_2'] == 2]
plt.plot(d[0], d[1], 'b*')
d = tsne[r1[u'_2'] == 3]
plt.plot(d[0], d[1], 'c.')
d = tsne[r1[u'_2'] == 4]
plt.plot(d[0], d[1], 'ro')
d = tsne[r1[u'_2'] == 5]
plt.plot(d[0], d[1], 'mo')
plt.show()
colors = [
'#ffc0cb', '#bada55', '#008080', '#420420', '#7fe5f0', '#065535',
'#ffd700'
]
plot_points(colors)