开始新的征程~~~~从第一个python 的机器学习实验开始~~~~~SVM实验

xiaoxiao2021-02-28  8

1.学习python;

2.学习机器学习;

3.培养良好的职业素养;找自己的方向。

工作还是虚拟机比较好,对于我这种有轻微强迫症的人来说,有个独立的环境还是不错的。

     9:12 安装虚拟机;win7-amd64; 安装pycharm-professional-2017.1;虚拟机安装出现问题一定要看BIOS是否开启了虚拟化;

4.windows下安装sklearn及所需要的库;(有先后顺序,下载对应的版本就可以了)

http://www.lfd.uci.edu/~gohlke/pythonlibs/这里面都是外国友人维护的windows下python扩展二进制包;

(1)numpy安装;

(2)scipy安装;

(3)scikit learn安装;

(4)matplotlib安装;

(5)pandas安装;

pip2 install ***.whl(文件全名)

5.验证安装结果:pip list;

好啦,进入正题:以Iris兰花数据集为例子:于从UCI数据库中下载的Iris原始数据集的样子是这样的,前四列为特征列,第五列为类别列,分别有三种类别Iris-setosa, Iris-versicolor, Iris-virginica。直接上代码吧:

from sklearn import svm from numpy import * from sklearn.model_selection import * import numpy as np def iris_type(s): it = {'Iris-setosa':0,'Iris-versicolor':1,'Iris-virginica':2} return it[s] path = u'D:/3-project/StudyPro/iris_data/iris.data' data = np.loadtxt(path, dtype=float, delimiter=',', converters={4:iris_type}) #print data #(2)将Iris分为训练集和测试集; x,y = np.split(data,(4,),axis=1) x = x[:,:2] x_train, x_test, y_train, y_test = train_test_split(x,y,random_state=1,train_size=0.6) #(3)训练SVM分类器 #clf = svm.SVC(C=0.1, kernel='linear',decision_function_shape='ovr') clf = svm.SVC(C=0.8, kernel='rbf', gamma=20, decision_function_shape='ovr') clf.fit(x_train, y_train.ravel()) #(4)计算分类器的准确率 print clf.score(x_train, y_train) #精度 y_hat = clf.predict(x_train) #show_accuracy(y_hat,y_train,'训练集') # print y_hat # print y_train print clf.score(x_test, y_test) y_hat = clf.predict(x_test) #show_accuracy(y_hat,y_test,'测试集')2

转载请注明原文地址: https://www.6miu.com/read-450143.html

最新回复(0)