数据集说明
数据下载地址:https://archive.ics.uci.edu/ml/index.php
data文件为数据,由逗号分开,names文件为数据说明。data文件可以用excel打开。
70%数据用于训练集,30%是测试集。
快速下载Sklearn的各种包
cmd+R,然后输入下面代码,下自己需要的包就好
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple *****
Wine数据集
这些数据包括了三种酒中13种不同成分的数量。13种成分分别为:Alcohol,Malicacid,Ash,Alcalinity of ash,Magnesium,Total phenols,Flavanoids,Nonflavanoid phenols,Proanthocyanins,Color intensity,Hue,OD280/OD315 of diluted wines,Proline。在 “wine.data”文件中,每行代表一种酒的样本,共有178个样本;一共有14列,其中,第一列为类标志属性,共有三类,分别记为“1”,“2”,“3”;后面的13列为每个样本的对应属性的样本值。其中第1类有59个样本,第2类有71个样本,第3类有48个样本。
Iris数据集
Iris也称鸢尾花卉数据集,是一类多重变量分析的数据集。通过花萼长度,花萼宽度,花瓣长度,花瓣宽度4个属性预测鸢尾花卉属于(Setosa,Versicolour,Virginica)三个种类中的哪一类。
代码
其中包含了朴素贝叶斯的高斯模型和伯努利模型,SVM分别使用三种核函数。
在对数据集分割上写的很low,反正数据量不大,就用笨方法来了。可以自行优化。
有个坑是在分训练集和数据集时要先打乱顺序,不然比如红酒数据会没有用到第3类来训练(使用Shuffle),最后测试集都是1或者2类,准确率0.76。。。很难受!-_-!
import pandas as pd
import random
import sklearn.svm as svm
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.naive_bayes import BernoulliNB
from sklearn.utils import shuffle
from sklearn.svm import SVC
souce_data = pd.read_csv("C:\\Users\\jwj13\\Desktop\\MyCode\\机器学习\\wine.data")
print("数据量:{}".format(len(souce_data)))#177
print("数据列数:{}".format(souce_data.columns.size))#14
souce_data = shuffle(souce_data)#数据随机排列
x_train=souce_data.iloc[:125,1:14]#0到124行;1-14列,训练集
y_train=souce_data.iloc[:125,0]#训练结果集
x_test=souce_data.iloc[125:,1:14]#测试集
y_test=souce_data.iloc[125:,0]#测试集的目标结果集
print("*****1.使用神经网络进行训练*****")
# 神经网络对数据尺度敏感,所以最好在训练前标准化,或者归一化,或者缩放到[-1,1]
#数据标准化
scaler = StandardScaler() # 标准化转换
scaler.fit(x_test) # 训练标准化对象
x_test_Standard= scaler.transform(x_test) # 转换数据集
scaler.fit(x_train) # 训练标准化对象
x_train_Standard= scaler.transform(x_train) # 转换数据集
y_test1=y_test.tolist()
print("测试集的目标类别:{}".format(y_test1))
bp=MLPClassifier(hidden_layer_sizes=(500,3 ), activation='relu', solver='lbfgs', alpha=0.0001, batch_size='auto',learning_rate='constant')
bp.fit(x_train_Standard,y_train.astype('int'))
y_predict_mlp=bp.predict(x_test_Standard)#预测的结果集
y_predict_mlp=list(y_predict_mlp)
print("MLP预测准确率:{}".format(sum([1 for n in range(len(y_test1)) if y_predict_mlp[n]==y_test1[n] ])/len(y_test1)))
print("*****2.使用朴素贝叶斯进行训练*****")
#高斯
gauNB = GaussianNB()
gauNB.fit(x_train_Standard,y_train.astype('int'))
y_predict_gau = gauNB.predict(x_test_Standard)
y_predict_gau=list(y_predict_gau)
#print(y_predict_gau)
print("高斯模型预测准确率:{}".format(sum([1 for n in range(len(y_test1)) if y_predict_gau[n]==y_test1[n] ])/len(y_test1)))
#伯努利
berNB = BernoulliNB()
berNB.fit(x_train_Standard,y_train.astype('int'))
y_predict_ber = berNB.predict(x_test_Standard)
y_predict_ber=list(y_predict_ber)
#print(y_predict_ber)
print("伯努利模型预测准确率:{}".format(sum([1 for n in range(len(y_test1)) if y_predict_ber[n]==y_test1[n] ])/len(y_test1)))
print("*****3.使用支持向量机进行训练*****")
# 训练svm模型---基于线性核函数
model1 = svm.SVC(kernel='linear')
# 训练svm模型---基于多项式核函数
model2 = svm.SVC(kernel='poly', degree=3)
# 训练svm模型---基于径向基核函数
model3 = svm.SVC(kernel='rbf', C=600)
model1.fit(x_train_Standard, y_train)
y_predict_svm1 = model1.predict(x_test_Standard)
y_predict_svm1=list(y_predict_svm1)
print("SVM(基于线性核函数)预测准确率:{}".format(sum([1 for n in range(len(y_test1)) if y_predict_svm1[n]==y_test1[n] ])/len(y_test1)))
model2.fit(x_train_Standard, y_train)
y_predict_svm2 = model2.predict(x_test_Standard)
y_predict_svm2=list(y_predict_svm2)
print("SVM(基于多项式核函数)预测准确率:{}".format(sum([1 for n in range(len(y_test1)) if y_predict_svm2[n]==y_test1[n] ])/len(y_test1)))
model3.fit(x_train_Standard, y_train)
y_predict_svm3 = model3.predict(x_test_Standard)
y_predict_svm3=list(y_predict_svm3)
print("SVM(基于径向基核函数)预测准确率:{}".format(sum([1 for n in range(len(y_test1)) if y_predict_svm3[n]==y_test1[n] ])/len(y_test1)))
Iris数据集:
(除训练集测试集分割代码不同,其他与第一个数据集几乎一致)
souce_data = pd.read_csv("C:\\Users\\jwj13\\Desktop\\MyCode\\机器学习\\iris.data")
print("数据量:{}".format(len(souce_data)))
print("数据列数:{}".format(souce_data.columns.size))
souce_data = shuffle(souce_data)#数据随机排列
x_train=souce_data.iloc[:105,0:4]#0到104行;前4列,训练集
y_train=souce_data.iloc[:105,4]#训练结果集
x_test=souce_data.iloc[105:,0:4]#测试集
y_test=souce_data.iloc[105:,4]#测试集的目标结果集,第5列
输出结果
1图1 Wine数据集结果
图2 iris数据集结果