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))
    
  • 相关阅读:
    SpringBoot的多环境配置及配置文件位置
    SpringBoot;yaml配置, JSR303校验
    springboot原理探寻,自动装配
    SpringBoot入门:搭建SpringBoot
    Android控件阴影库
    Android开发Utils工具类集合
    Android 实现顶部状态栏的沉浸模式(任意设置状态栏的颜色)
    推荐一个博客代码高亮插件
    H5+Css+js 做App UI 与原生的区别
    Android线程切换简便方法
  • 原文地址:https://www.cnblogs.com/chenshaowei/p/13118365.html
Copyright © 2011-2022 走看看