记录一下自己学习中遇到的问题。。
1. IDE使用的是pycharm,暂时没有发现什么问题。
2. 需要用到依赖包scipy& numpy & scikit-learn & matplotlib
其中由于matplotlib 在windows上安装比较麻烦所以在ubuntu上实现。
matplotlib 安装代码:(2选1)
sudo apt install python-matplotlib
sudo pip install matplotlib
3.第一次接触Matplotlib 画图,尝试把《机器学习系统设计》第二章的第一个图画了一下,进行数据的可视化,先对整个iris数据集有一个视觉上的印象方便采取相应的算法,代码如下:
from matplotlib import pyplot as plt
from sklearn.datasets import load_iris
import numpy
data = load_iris()
features = data['data']
feature_names = data['feature_names']
target = data['target']
#plt.figure(12)
plt.subplot(231)
for t,marker,c in zip(xrange(3),">oh","rgb"):
plt.scatter(features[target == t,0],features[target == t,1],50,marker = marker,c=c)
plt.xlabel(feature_names[0])
plt.ylabel(feature_names[1])
#plt.plot()
plt.subplot(232)
for t,marker,c in zip(xrange(3),">oh","rgb"):
plt.scatter(features[target == t,0],features[target == t,2],50,marker = marker,c=c)
plt.xlabel(feature_names[0])
plt.ylabel(feature_names[2])
#plt.plot()
plt.subplot(233)
for t,marker,c in zip(xrange(3),">oh","rgb"):
plt.scatter(features[target == t,0],features[target == t,3],50,marker = marker,c=c)
plt.xlabel(feature_names[0])
plt.ylabel(feature_names[3])
plt.subplot(234)
for t,marker,c in zip(xrange(3),">oh","rgb"):
plt.scatter(features[target == t,1],features[target == t,2],50,marker = marker,c=c)
plt.xlabel(feature_names[1])
plt.ylabel(feature_names[2])
plt.subplot(235)
for t,marker,c in zip(xrange(3),">oh","rgb"):
plt.scatter(features[target == t,1],features[target == t,3],50,marker = marker,c=c)
plt.xlabel(feature_names[1])
plt.ylabel(feature_names[3])
plt.subplot(236)
for t,marker,c in zip(xrange(3),">oh","rgb"):
plt.scatter(features[target == t,2],features[target == t,3],50,marker = marker,c=c)
plt.xlabel(feature_names[2])
plt.ylabel(feature_names[3])
plt.show()
4.画出各个数据点的分布后可以构建第一个分类模型。
5.简单阐述K-means.(clustering 聚类)
k-means 算法的具体步骤:
- 选定 K 个中心
的初值。这个过程通常是针对具体的问题有一些启发式的选取方法,或者大多数情况下采用随机选取的办法。因为前面说过 k-means 并不能保证全局最优,而是否能收敛到全局最优解其实和初值的选取有很大的关系,所以有时候我们会多次选取初值跑 k-means ,并取其中最好的一次结果。
- 将每个数据点归类到离它最近的那个中心点所代表的 cluster 中。
- 用公式
计算出每个 cluster 的新的中心点。
- 重复第二步,一直到迭代了最大的步数或者前后的
的值相差小于一个阈值为止。