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))
    
  • 相关阅读:
    C#中的int、long、float、double等类型都占多少个字节的内存
    Bit 存储操作代码碎片
    unity文件写入与读取
    unity调用系统剪切板功能
    LayerMask小结
    NGUI中获取鼠标在控件内部坐标
    【Unity技巧】Unity中的优化技术
    工程源码目录
    Unity3D_NGUI_性能优化实践_CPU卡顿
    Unity3d:UI面板管理整合进ToLua
  • 原文地址:https://www.cnblogs.com/chenshaowei/p/13118365.html
Copyright © 2011-2022 走看看