支持向量机分类器

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/King_key/article/details/79233737

1 支持向量机分类器

    根据训练样本的分布,搜索所有可能的线性分类器中最佳的那个。

   本文是利用支持向量机进行手写体数字图像的分类任务。

2 实验代码及结果截图

#coding:utf-8
#支持向量机分类器

#手写体数据读取
#从slearn.datasets里导入手写数字加载器
from sklearn.datasets import load_digits
#获取图像数据并出储存在digits变量中
digits=load_digits()
#检视数据规模和特征维度
print '数据规模:',digits.data.shape[0]
print '特征维度:',digits.data.shape[1]

#手写体数据分割
#从sklearn.cross_validation中导入train_test_split用于数据分割
from sklearn.cross_validation import train_test_split
#随机75%的训练样本,25%的测试样本
X_train,X_test,y_train,y_test=train_test_split(digits.data,digits.target,test_size=0.25,random_state=33)
print '训练数据规模:',y_train.shape[0]

print '测试数据规模:',y_test.shape[0]


#使用支持向量机(分类)对手写体数字图像进行识别
#从sklearn.preprocessing里导入数据标准化模块
from sklearn.preprocessing import StandardScaler
#从sklearn.svm中导入基于线性假设的支持向量机分类器LinearSVC
from sklearn.svm import LinearSVC
#标准化数据
ss=StandardScaler()
X_train=ss.fit_transform(X_train)
X_test=ss.transform(X_test)
#初始化LinearSVC
lsvc=LinearSVC()
#进行模型训练
lsvc.fit(X_train, y_train)
#进行预测
y_predict=lsvc.predict(X_test)
#准确性测评
print 'The Accuracy of LinearSVC is:',lsvc.score(X_test,y_test)
#预测结果的详细分析
from sklearn.metrics import classification_report
print classification_report(y_test,y_predict,target_names=digits.target_names.astype(str))  



猜你喜欢

转载自blog.csdn.net/King_key/article/details/79233737