sklearn之svm-葡萄酒质量预测(12)
Posted 麦好
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sklearn之svm-葡萄酒质量预测(12)相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 14 20:48:28 2018
@author: myhaspl
@email:myhaspl@myhaspl.com
svm
"""
import pandas as pd
import numpy as np
from sklearn import svm
from sklearn.externals import joblib
print "准备数据"
testDf=pd.read_csv("winequality-white-test.csv",sep=";")
testData=testDf.values
wineDf=pd.read_csv("winequality-white.csv",sep=";")
wineData=wineDf.values
dataColName=wineDf.columns
rsColName=dataColName[-1]
ftColName=list(dataColName[:len(dataColName)-1])
testFeature=testDf[ftColName].values
testResult=testDf[rsColName].values
wineFeature=wineDf[ftColName].values[:2000]
wineResult=wineDf[rsColName].values[:2000]
print "建立模型"
clf = svm.SVC(gamma='scale',kernel='poly',C=0.8,degree=3)
print "训练模型"
clf.fit(wineFeature,wineResult)
print "保存模型"
joblib.dump(clf, 'winepred.joblib')
print "测试结果"
y_pred=clf.predict(testFeature)
print y_pred
print testResult
predWine=np.equal(y_pred,testResult)
correctCount=float(sum(map(lambda x: 1 if x else 0, predWine)))
print "正确样本数:%d,总测试样本数:%d,正确率:%g"%(correctCount,len(testResult),correctCount/len(testResult))
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 14 20:48:28 2018
@author: myhaspl
@email:myhaspl@myhaspl.com
svm读取模型,测试数据
"""
import pandas as pd
import numpy as np
from sklearn.externals import joblib
print "准备数据"
testDf=pd.read_csv("winequality-white-test.csv",sep=";")
testData=testDf.values
wineDf=pd.read_csv("winequality-white.csv",sep=";")
wineData=wineDf.values
dataColName=wineDf.columns
rsColName=dataColName[-1]
ftColName=list(dataColName[:len(dataColName)-1])
testFeature=testDf[ftColName].values
testResult=testDf[rsColName].values
print "读取模型"
clf=joblib.load('winepred.joblib')
print "测试结果"
y_pred=clf.predict(testFeature)
print y_pred
print testResult
predWine=np.equal(y_pred,testResult)
correctCount=float(sum(map(lambda x: 1 if x else 0, predWine)))
print "正确样本数:%d,总测试样本数:%d,正确率:%g"%(correctCount,len(testResult),correctCount/len(testResult))
准备数据
读取模型
测试结果
[5 5 5 5 6 5 7 5 6 5 6 6 6 5 6 5 7 6 5 5 6 6 6 6 5 5 5 6 5 5 5 5 7 7 6 5 5
6 5 5 5 6 5 5 5 6 6 6 6 6 5 5 5 5 5 5]
[6 6 5 6 6 5 7 5 8 5 6 5 5 6 8 5 7 7 5 5 6 6 5 6 5 6 6 6 5 6 6 5 7 7 7 6 6
7 4 6 5 5 5 5 5 6 5 6 6 5 6 5 5 5 5 4]
正确样本数:31,总测试样本数:56,正确率:0.553571
以上是关于sklearn之svm-葡萄酒质量预测(12)的主要内容,如果未能解决你的问题,请参考以下文章