python 中的 sklearn 初识

版权声明:如需转载请联系本人[email protected] https://blog.csdn.net/u012491646/article/details/79312645
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 10 00:35:51 2018
@author: zhangll
"""
from sklearn.neighbors.kde import KernelDensity as KD
from sklearn import svm
import matplotlib.pyplot as plt
from sklearn import datasets
import numpy as np
#import PyQt5
#%matplotlib qt
#iritContain = datasets.load_iris()
#irtarget = iritContain.target
#irdata = iritContain.data


digitContain = datasets.load_digits()
train_data_target = digitContain.target
train_data_source = digitContain.data




##the repeat number of data
#print(set(train_data_target))
## show the images of the different image matrix 
#for i in range(2):    
#    plt.matshow(digitContain.images[i])
#1.build the estimator of classify model


# so if you want to predict some sample,the first step you
# should do is find the estimator that range from ['unsupervised','supervised'..balabala] 
clf = svm.SVC(gamma=0.001,C =100.)
#2. fit mean that you should feed the model using a dataset
clf.fit(train_data_source[:-1],train_data_target[:-1])


#3. pridict the source data into the class


num_index = -296


want_predict_number = train_data_source[num_index:(num_index+1)]
predict_n = clf.predict(want_predict_number)[0]
Real_n = digitContain.target[num_index:(num_index+1)][0]


print("the predict result of the data is [%d],the image \
show as follow:"%predict_n)
#plt.close()
#fig = plt.figure(1)
#plt.hold()
#plt.subplot(111)
plt.matshow(digitContain.images[num_index],cmap = plt.get_cmap('Greys'))
plt.title("The predict number is %d \n the actual number is %d"%(predict_n,Real_n))
#plt.close()


# if you want to store the clf into the disk,the first package you
# will find is the 'Pickle' whick pickle the data into string but not into the
# disk,so the alternative package you should choose is the 'jobslib' from the external packge of sklearn
from sklearn.externals import joblib
joblib.dump(clf,'SvmclfDigts.pkl')
# so you if want to use the class which is stored as a pkl file,you should use joblib.load(file) as the 
# 'pickle' does!
clf = joblib.load('SvmclfDigts.pkl') 







猜你喜欢

转载自blog.csdn.net/u012491646/article/details/79312645