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))
    
  • 相关阅读:
    selenium测试(Java)-- 键盘事件(七)
    selenium测试(Java)--鼠标事件(六)
    selenium测试(Java)--元素操作(五)
    selenium测试(Java)--浏览器控制(四)
    selenium测试(Java)(三)
    selenium定位方法(java实例)(二)
    selenium测试环境搭建(一)
    HTML5前端(移动端网站)性能优化指南
    dede使用方法----如何调用最新文章,最热文章,友情链接
    dede使用方法---如何调用指定栏目
  • 原文地址:https://www.cnblogs.com/chenshaowei/p/13118365.html
Copyright © 2011-2022 走看看