• 心脏病预测(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))
    
  • 相关阅读:
    HTML2
    HTML1
    MySQL进阶part4
    pymysql模块
    MySQL进阶part3
    MySQL进阶part2
    MySQL进阶part1
    java IO中的乱码问题
    解决在IDEA中无法使用Scanner输入的问题
    IDEA配置xml文件头报错:URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
  • 原文地址:https://www.cnblogs.com/chenshaowei/p/13118365.html
Copyright © 2020-2023  润新知