机器学习多变量多标签

重要参考文献

文章内容

  1. 实验结果
  2. 代码展示
# -*- 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
    
    
   
    
    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鄢广杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值