Neaya~

笔记、记录、总结

糖尿病模型预测

摘要:Kaggle竞赛题,ML实例应用

diabetes model prediction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
# @Time : 2020/9/6
# @Author : Jimou Chen
"""
from sklearn.linear_model import LogisticRegression
import pandas as pd
import matplotlib.pyplot as plt
import seaborn
import numpy as np
import missingno as msn
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split


def label_distribution(data):
p = data.Outcome.value_counts().plot(kind='bar') # 使用柱状图画出
plt.show()
# 可视化数据发布, 有些数据本不该为0的却为0,其实是空的
p = seaborn.pairplot(data, hue='Outcome')
plt.show()
# 把空值的用柱状图画出来
p = msn.bar(data)
plt.show()


def handle_data():
data = pd.read_csv('data/diabetes.csv')
# 查看标签分布
print(data.Outcome.value_counts())
# 把葡萄糖,血压,皮肤厚度,胰岛素,身体质量指数中的0替换为nan
handle_col = ['Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI']
data[handle_col] = data[handle_col].replace(0, np.nan)

# 设定阀值
thresh_count = data.shape[0] * 0.8
# 若某一列数据缺失的数量超过20%就会被删除
data = data.dropna(thresh=thresh_count, axis=1)

# 填充数据,得到新的数据集data
data['Glucose'] = data['Glucose'].fillna(data['Glucose'].mean())
data['BloodPressure'] = data['BloodPressure'].fillna(data['BloodPressure'].mean())
data['BMI'] = data['BMI'].fillna(data['BMI'].mean())

return data


if __name__ == '__main__':
new_data = handle_data()
label_distribution(new_data)

# 切分数据集
x_data = new_data.drop('Outcome', axis=1)
y_data = new_data.Outcome
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.3, stratify=y_data)

# 建模
model = LogisticRegression()
model.fit(x_train, y_train)

# 预测
pred = model.predict(x_test)
# 评估
print(classification_report(pred, y_test))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
D:\Anaconda\Anaconda3\python.exe D:/Appication/PyCharm/Git/kaggle-project/DiabetesPrediction/diabetes_predict.py
0 500
1 268
Name: Outcome, dtype: int64
precision recall f1-score support

0 0.90 0.80 0.85 169
1 0.58 0.76 0.66 62

accuracy 0.79 231
macro avg 0.74 0.78 0.75 231
weighted avg 0.81 0.79 0.80 231


Process finished with exit code 0



Welcome to reward