-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_SVC.py
60 lines (48 loc) · 1.72 KB
/
model_SVC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, recall_score, f1_score, confusion_matrix, roc_curve, auc
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score
# 读取数据
data = pd.read_csv('Training_setdata.csv')
# 分割特征和标签
X = data[['Height', 'Weight']]
y = data['Gender']
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 创建模型
svc = SVC(probability=True)
# 训练模型
svc.fit(X_train, y_train)
# 预测
y_pred = svc.predict(X_test)
# 计算精确度、召回率和F1分数
accuracy = accuracy_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
print('Accuracy: ', accuracy)
print('Recall: ', recall)
print('F1 score: ', f1)
# 生成混淆矩阵
cm = confusion_matrix(y_test, y_pred)
print('Confusion Matrix: \n', cm)
# 计算ROC曲线和AUC值
y_pred_proba = svc.predict_proba(X_test)[:,1]
fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)
roc_auc = auc(fpr, tpr)
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=1, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=1, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('SVC Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.rcParams['font.sans-serif']=['SimHei']
plt.figtext(0.5, 0.01, '图6', ha='center')
plt.show()
# 进行交叉验证
scores = cross_val_score(svc, X, y, cv=5)
print('Cross-validation scores: ', scores)