题目:十折交叉验证SVM算法,计算精确度、召回率、特异性
代码:
import numpy as np
import scipy.io as sio
from sklearn.model_selection import
train_test_split# 分割数据模块
from sklearn import svm
#导入数据集
data=sio.loadmat('C:/Users/Desktop/data/AD')#导入数据的路径
X=data['PET']#数据
y=data['gnd']#标签
#X.shape, y.shape
#分割数据
X_train, X_test, y_train, y_test =train_test_split(X, y, test_size=0.1, random_state=0)
#print(X_train.shape, y_train.shape)
#print(X_test.shape, y_test.shape)
#建立模型,训练模型
#clf = svm.SVC(kernel='linear',C=1).fit(X_train, y_train)
#clf.score(X_test, y_test)
from sklearn.model_selection import
cross_val_score## K折交叉验证模块
from sklearn.model_selection importcross_val_predict
clf = svm.SVC(kernel='linear', C=1)
scores = cross_val_score(clf, X, y, cv=10)#cv=10,十折
print('十折的交叉验证得分:',scores)
print('平均得分:',scores.mean())
predicted = cross_val_predict(clf,X, y,cv=10)
'''
#scores1与scores的结果相同,都可以用来计算精准度。
print('标准差:',scores.std())
print("Accuracy: %0.2f (+/-%0.2f)" % (scores.mean(), scores.std() * 2))
#print('predicted:',predicted)#预测的值
scores1 = cross_val_score(clf, X, y, cv=10,scoring='accuracy')
print(scores1.mean())
'''
#计算特征
a,b,p1,t1,t2=0,0,0,0,0
for i in range(102):
if y[i]==1&predicted[i]==1:a=a+1
if y[i]==2&predicted[i]==2:b=b+1
if predicted[i]==1:p1=p1+1
if y[i]==1:t1=t1+1
if y[i]==2:t2=t2+1
print('真阳性a:{},真阴性b:{},所有预测为阳性数量p1:{},所有实际为阳性数量t1:{},t2:{}'.format(a,b,p1,t1,t2))
print('精确度:',a/p1)
print('召回率:',a/t1)#灵敏度
print('特异性:',b/t2)
结果:
十折的交叉验证得分: [0.91666667 0.90909091 0.9 0.8 0.9 1. 0.9 0.8 0.9 0.9 ]
平均得分:0.8925757575757576
真阳性a:45,真阴性b:46,所有预测为阳性数量p1:50,所有实际为阳性数量t1:51,t2:51
精确度:0.9
召回率:0.8823529411764706
特异性:0.9019607843137255
链接:https://pan.baidu.com/s/1J-X1w9WQSSMWMn9mYw1PvA
提取码:hxoe
网友评论