在本项目的前期训练中,数据最后的结果都不理想。因此在代码中引入了多种数据模型:逻辑回归、高斯朴素贝叶斯、K近邻分类、决策树分类、支持向量机分类、xgboost。在训练集上,最高准确率为77%。
列名 | 解释 |
---|---|
Pregnancies | 怀孕了⼏次 |
Glucose | ⾎糖 |
BloodPressure | ⾎压 |
SkinThickness | ⽪脂厚度 |
Insulin | 胰岛素 |
BMI | 体质指数 |
DiabetesPedigreeFunction | 糖尿病⾎统 |
Age | 年龄 |
Outcome | label:是否患病 |
没有缺失值,而且全部是数字类型
Pregnancies 768 non-null int64
Glucose 768 non-null int64
BloodPressure 768 non-null int64
SkinThickness 768 non-null int64
Insulin 768 non-null int64
BMI 768 non-null float64
DiabetesPedigreeFunction 768 non-null float64
Age 768 non-null int64
Outcome 768 non-null int64
在本项目的前期训练中,数据最后的结果都不理想。因此在代码中引入了多种数据模型:逻辑回归、高斯朴素贝叶斯、K近邻分类、决策树分类、支持向量机分类、xgboost。在训练集上,最高准确率为77%。
from sklearn.linear_model import LogisticRegression
import pandas as pd
import numpy as np
df = pd.read_csv("/Users/wangsen/ai/13/homework/diabetes.csv")
df.info()
target = df.pop("Outcome")
data = df.values
print(data[:5])
lr = LogisticRegression()
lr.fit(data,target)
score = lr.score(data,target)
print("训练集上的预测准确率",score)
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
select_top_4 = SelectKBest(score_func=chi2, k =4)
select_top_4.fit(data,target)
data_4 = select_top_4.transform(data)
结果:'Glucose','Insulin','BMI','Age'
from sklearn.preprocessing import StandardScaler
X = StandardScaler().fit_transform(data_4)
X = data
y = target
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from xgboost import XGBClassifier
import sklearn
models = []
models.append(("LR", LogisticRegression())) #逻辑回归
models.append(("NB", GaussianNB())) # 高斯朴素贝叶斯
models.append(("KNN", KNeighborsClassifier(10))) #K近邻分类
models.append(("DT", DecisionTreeClassifier())) #决策树分类
models.append(("SVM", SVC())) # 支持向量机分类
models.append(("xgboost", XGBClassifier())) # xgboost
'''
estimator:数据对象
X:数据
y:预测数据
soring:调用的方法
cv:交叉验证生成器或可迭代的次数
n_jobs:同时工作的cpu个数(-1代表全部)
verbose:详细程度
fit_params:传递给估计器的拟合方法的参数
pre_dispatch:控制并行执行期间调度的作业数量。
'''
results = []
names = []
for name, model in models:
kflod = KFold(n_splits=10, random_state=22)
cv_result = cross_val_score(model, X,y, cv = kflod,scoring="accuracy")
names.append(name)
results.append(cv_result)
for i in range(len(names)):
print(names[i], results[i].mean())
结果:
LR 0.7695146958304853
NB 0.7551777170198223
KNN 0.74865003417635
DT 0.6952494873547506
SVM 0.6510252904989747
xgboost 0.7668660287081339