sklearn之svm-葡萄酒质量预测(12)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010255642/article/details/83051739
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 14 20:48:28 2018

@author: myhaspl
@email:[email protected]
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:[email protected]
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

猜你喜欢

转载自blog.csdn.net/u010255642/article/details/83051739