zoukankan      html  css  js  c++  java
  • 心脏病预测(SVM模型)

    题目

    Solve the heart disease problem

    Here is a small dataset provided by the Cleveland Clinic Foundation for Heart Disease, which are several hundred rows in the CSV. Each row describes a patient, and each column describes an attribute.

    Using this information to predict whether a patient has heart disease, which in this dataset is a binary classification task.

    Remember, the most important things is preprocessing the data and transform to feature column.

    代码

    数据预处理

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    //读取数据
    URL = 'https://storage.googleapis.com/applied-dl/heart.csv'
    df = pd.read_csv(URL)
    
    //结合数据集信息, 我们可以得到age,trestbpd,chol,thalach,oldpeak均为Numerical类型,不用处理。 sex,fbs,exang,target均为二分类数值,不用处理。 剩下的cp,restecg,slope,ca,thal均为多分类数值,需要数据预处理。
    
    //拆分属性的值
    a = pd.get_dummies(df['cp'], prefix = "cp")
    b = pd.get_dummies(df['restecg'], prefix = "restecg")
    c = pd.get_dummies(df['slope'], prefix = "slope")
    d = pd.get_dummies(df['ca'], prefix = "ca")
    e = pd.get_dummies(df['thal'], prefix = "thal")
    df = pd.concat([df, a, b, c, d, e], axis = 1)
    df = df.drop(columns = ['cp', 'restecg', 'slope', 'ca', 'thal'])
    df.head(5)
    //提取XY值
    Y = df.target.values
    X = df.drop(['target'], axis = 1)
    
    //数据标准化
    from sklearn.preprocessing import StandardScaler
    from sklearn.model_selection import train_test_split
    
    sc = StandardScaler()
    sc.fit(X)
    X = sc.transform(X)
    //拆分为训练集和测试集
    x_train, x_test, y_train, y_test = train_test_split(X, Y)
    

    建立SVM模型

    from sklearn.svm import SVC
    
    svm = SVC(random_state = 1)
    svm.fit(x_train, y_train)
    
    acc = svm.score(x_test, y_test)*100
    print("Test Accuracy of SVM Algorithm: {:.2f}%".format(acc))
    
  • 相关阅读:
    【NX二次开发】Block UI 组
    【NX二次开发】Block UI 双精度表
    【NX二次开发】Block UI 整数表
    自己写的简单的轮播图
    微信分享到朋友圈----摘录
    HTML5比较实用的代码
    苏格拉底的名言警句
    jQuery 幻灯片 ----摘录
    DeDe调用指定栏目ID下的文章
    JQuery 判断ie7|| ie8
  • 原文地址:https://www.cnblogs.com/chenshaowei/p/13118365.html
Copyright © 2011-2022 走看看