机器学习sklearn库的使用--部署环境(python2.7 windows7 64bit)

时间:2022-08-03 20:24:21

最近在学习机器学习的内容,难免地,要用到Scikit-learn(sklearn,下同)这一机器学习包。为了使用sklearn库,我们需要安装python2.7,pip install工具,numpy+mkl、scipy、pandas、sklearn等开源包。其中numpy+mkl和scipy安装比较费劲,不能通过pip install工具直接安装。
各文件下载链接:
Python2.7.13 : Python2.7.13
numpy+mkl : numpy‑1.11.3+mkl‑cp27‑cp27m‑win_amd64.whl
Scipy:scipy‑0.19.1‑cp27‑cp27m‑win_amd64.whl

下载上述文件之后首先按照步骤安装Python27。重点来了,安装numpy+mkl,下面介绍两种方法:

  1. 进入到Python27安装目录,比如我的是:G:\Python\Scripts,在cmd界面进入到该目录下。转3
  2. 在环境变量path中加分号添加pip所在目录G:\Python\Scripts,保存后退出。转3
  3. 然后输入以下指令安装pip工具:
 easy_install.exe pip
pip install wheel
pip install [numpy+mkl目录]

此时很可能出现如下提示信息:
numpy‑1.11.3+mkl‑cp27‑cp27m‑win_amd64.whl is not a supported wheel on this platform
Storing debug log for failure in C:\Users\Administrator\pip\pip.log网上研究后发现是由于pip安装工具没有更新的原因,所以使用pip指令更新:

pip install --upgrade setuptools
pip install --upgrade pip

然后输入在命令行下输入控制指令:

pip install wheel
pip install [numpy+m [Scipy目录]kl目录]

然后在Python27的Shell中输入import numpy
如果输出没有错误,说明安装没有问题,继续安装Scipy。
在命令行输入指令:pip install [scipy目录],在Shell中输入指令import scipy,如果输出没有错误,说明安装没有问题。
继续安装sklearn和panda,在命令行输入指令:

pip install sklearn
pip install pandas

安装完成后在Shell中输入指令:

import sklearn
import pandas

如果输出没有错误,至此,sklearn库安装完成。
下面以简单的鸢尾花数据集做一个简单的测试,代码如下:

from sklearn.datasets import load_iris
from sklearn.preprocessing import Normalizer
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
iris = load_iris()
data = Normalizer().fit_transform(iris.data) #normalize the data
labels = iris.target
clf = DecisionTreeClassifier()
X_train,X_test,y_train,y_test = train_test_split(data,labels,test_size=0.2)
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test,y_pred)
print accuracy

运行结果:

>>> 
0.866666666667

测试成功,整个安装过程结束。