windows下安装pip
cd到 C:\Python27\Scripts目录下
执行 easy_install pip 安装完成后,在Scripts目录下生成pip工具
whl文件格式及安装方法
whl是一种压缩文件格式,其中包含py文件和编译后的pyd文件 安装whl包:pip install xxx.whl 更新whl包:pip install -U xxx.whl注意:安装whl包之前需要先安装wheel工具
安装wheel:pip install wheel
numpy下载链接:
安装numpy:pip install numpy-xxx-xxx.whl
错误记录:安装时,文件名中出现问号,可能是由于编码问题导致,手动重命名文件后解决
scipy :SciPy是一款方便、易于使用、专为科学和工程设计的Python工具包.
它包括统计,优化,整合,线性代数模块,傅里叶变换,信号和图像处理,常微分方程求解器等等
安装Scipy函数库,SciPy依赖Numpy
下载链接:
安装Scipy : pip install scipy-xxx-xxx.whl
matplotlib:Python绘图库,可以绘制直方图,功率谱,条形图,错误图,散点图等
安装matplotlib:直接运行 pip install matplotlib
或者下载whl安装包安装
下载地址:
scikit-learn(简称sklearn):sklearn是Scipy的扩展,建立在NumPy和matplotlib库的基础上;支持包括分类、回归、降维和聚类四大机器学习算法。还包含了特征提取、数据处理和模型评估三大模块
安装:pip install -U scikit-learn
import numpy as npfrom sklearn.cluster import KMeansfrom sklearn.metrics import silhouette_scoreimport matplotlib.pyplot as plt plt.subplot(3,2,1)#np.array()的类型是arrayx1=np.array([1,2,3,1,5,6,5,5,6,7,8,9,7,9])x2=np.array([1,3,2,2,8,6,7,6,7,1,2,1,1,3])#zip()函数,接收的参数是可迭代的对象#返回的是一个list,list中的元素为元组#zip(x1,x2)就是将两个数组中按顺序对应的数字组成一个元组#返回的是 [(1,1),(2,3),(3,2)...]#此处如果将x1和x2定义为普通的list也可返回相同的listx=np.array(zip(x1,x2)).reshape(len(x1),2)#reshape()改变Array的维数,例如#a = np.array([1,2,3,4,5,6,7,8])#b = a.reshape(2,4) 形如:[[1,2,3,4],[5,6,7,8]]#c = a.reshape(2,2,2) 将a变成三维数组plt.xlim([0,10])plt.ylim([0,10])plt.title('Instances')plt.scatter(x1,x2)colors=['b','g','r','c','m','y','k','b']markers=['o','s','D','v','^','p','*','+']clusters=[2,3,4,5,8]subplot_counter=1sc_scores=[]for t in clusters : subplot_counter+=1 plt.subplot(3,2,subplot_counter) kmeans_model=KMeans(n_clusters=t).fit(x) for i,l in enumerate(kmeans_model.labels_): plt.plot(x1[i],x2[i],color=colors[l],marker=markers[l],ls='None') plt.xlim([0,10]) plt.ylim([0,10]) sc_score=silhouette_score(x,kmeans_model.labels_,metric='euclidean') sc_scores.append(sc_score)#程序运行出错,提示字符出错 plt.title('K=%s,silhouette coefficient= %0.03 f' % (t,sc_score))plt.figure()plt.plot(clusters,sc_scores,'*-')plt.xlabel('Number of Cluster')plt.ylabel('Silhouette Coefficient Score')plt.show()