使用iris 数据集应用SOM, 附带可视化
from minisom import MiniSom
import matplotlib.pyplot as plt
from sklearn import datasets
# your code
data = datasets.load_iris()
X = data.data
y = data.target
som = MiniSom(15, 15, 4)
som.pca_weights_init(X)
# random training
som.train(X, 5000, random_order=True)
# Visualising SOM results
im = 0
for x, t in zip(X, y): # scatterplot
w = som.winner(x)
plt. text(w[0]+.5, w[1]+.5, str(t),
color=plt.cm.rainbow(t / 3.))
plt.axis([0, som.get_weights().shape[0], 0, som.get_weights().shape[1]])
plt.savefig('som_digts.png', bbox_inches='tight',pad_inches = 0)
plt.show()
如果帮助到你,能点个赞吗?