文章内容
- 实验结果
- 代码展示
# -*- coding: utf-8 -*-
"""
/*
**********************************************************************
* 模块名称: 机器学习SVM实验
* 摘 要: 心脏病预测
* 当前版本: 1.0
* 作 者: guangjie2333
* 完成日期: 2021-10-24
* 内 容:
* 注 意:
*********************************************************************/
"""
"""
/*
**********************************************************************
* 包含头文件
**********************************************************************
*/
"""
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import svm
#三维决策边界
from mpl_toolkits import mplot3d
#测试集和训练集
from sklearn.model_selection import train_test_split
#模型
from sklearn.multiclass import OneVsOneClassifier
from sklearn import metrics
from sklearn.metrics import confusion_matrix
import seaborn as sns
#评估
from sklearn.metrics import classification_report
from sklearn.metrics import f1_score
from sklearn.metrics import roc_auc_score
#打乱数据
from sklearn.utils import shuffle
"""
/*
**********************************************************************
* 引入数据集
**********************************************************************
*/
"""
example= pd.read_csv("processed.cleveland.csv")
example = shuffle(example)#打乱数据
"""
/*
**********************************************************************
* 内部变量
**********************************************************************
*/
"""
header = example.columns;
example = example.loc[example.ca != '?'];
example = example.loc[example.thal != '?'];
row = example.shape[0]
col = example.shape[1]
epsilon = 1e-8
"""
/*
**********************************************************************
* 功能函数
**********************************************************************
*/
"""
"""
/*
******************************************************************************
* 函数名称: __main__
* 函数功能: 主函数
* 输入参数: none
* 输出参数: none
* 返 回 值: void
* 创建日期: 2021年-10月-24日
* 注 意:
*******************************************************************************
*/
"""
if __name__ == '__main__':
#取前13列特征赋值给X, 取目标值赋值给Y
XX = np.array(example.loc[:,header[0:13]]).reshape(row,col - 1)
XX = np.asarray(XX,'float')
YY = np.array(example.loc[:,header[13]])
#分为训练集和测试集,验证集
X_train,X_test,y_train,y_test = train_test_split(XX,YY,test_size = 0.3,random_state=1)
X_Validation = X_test[0:16];
Y_Validation = y_test[0:16];
# X_test = X_test[16:-1];
# y_test = y_test[16:-1];
#for i in range(0,)
model = OneVsOneClassifier(svm.SVC(C=1,kernel='linear',random_state=1)).fit(X_train, y_train)
y_predict = model.predict(X_test)
# 由原标签和预测标签生成混淆矩阵
cm = confusion_matrix(y_test, y_predict);
plt.figure();
sns.heatmap(cm,annot=True); #画热力图
plt.ylabel('True label') # 坐标轴标签
plt.xlabel('Predicted label') # 坐标轴标签
plt.title('confusion matrix')
print(cm);
print ("Classification report: \n", (classification_report(y_test, y_predict)))
print ("F1 micro averaging:",(f1_score(y_test, y_predict, average='micro')))
#end