Mushroom Classification with SVM: ROC Curve Visualization
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
# 读取数据集
data = pd.read_csv('mushrooms.csv')
# 将分类标签转换为0和1
label_encoder = LabelEncoder()
data['class'] = label_encoder.fit_transform(data['class'])
# 将离散属性进行编码转换
for column in data.columns[1:]:
data[column] = LabelEncoder().fit_transform(data[column])
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(data.drop('class', axis=1), data['class'], test_size=0.3, random_state=42)
from sklearn.svm import SVC
svm = SVC(kernel='rbf', C=10, probability=True)
svm.fit(X_train, y_train)
y_pred = svm.predict(X_test)
y_score = svm.predict_proba(X_test)[:,1]
# 输出分类报告
print("SVC:",classification_report(y_test, y_pred))
# 绘制ROC曲线
fpr, tpr, thresholds = roc_curve(y_test, y_score)
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, 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('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.show()
原文地址: https://www.cveoy.top/t/topic/oh0x 著作权归作者所有。请勿转载和采集!