显示部分数据
import numpy as np # 导入numpy包
import pandas as pd # 导入pandas库
# 1.数据读取
df_heart = pd.read_csv("../input/heart-disease/heart.csv") # 读取文件
df_heart.head() # 显示前五行数据
age | sex | cp | trestbps | chol | fbs | restecg | thalach | exang | oldpeak | slope | ca | thal | target | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 63 | 1 | 3 | 145 | 233 | 1 | 0 | 150 | 0 | 2.3 | 0 | 0 | 1 | 1 |
1 | 37 | 1 | 2 | 130 | 250 | 0 | 1 | 187 | 0 | 3.5 | 0 | 0 | 2 | 1 |
2 | 41 | 0 | 1 | 130 | 204 | 0 | 0 | 172 | 0 | 1.4 | 2 | 0 | 2 | 1 |
3 | 56 | 1 | 1 | 120 | 236 | 0 | 1 | 178 | 0 | 0.8 | 2 | 0 | 2 | 1 |
df_heart.target.value_counts() # 输出分类值及各个类别数目
1 165
0 138
Name: target, dtype: int64
# 对显示年龄/最大心率两个特征与是否患病之间的关系
import matplotlib.pyplot as plt # 导入绘图工具
# 年龄 + 最大心率 作为输入,查看分类结果散点图
plt.scatter(x=df_heart.age[df_heart.target==1],
y=df_heart.thalach[df_heart.target==1], c="red")
plt.scatter(x=df_heart.age[df_heart.target==0],
y=df_heart.thalach[df_heart.target==0], marker="^")
plt.legend(["Disease", "No Disease"]) # 显示图例
plt.xlabel("Age") # x轴标签
plt.ylabel("Heart Rate") # y轴标签
plt.show() # 显示散点图
# 调用逻辑回归函数训练机器
loss_history, weight_history, bias_history = logistic_regression(X_train, y_train, weight, bias, alpha, iterations)
Output exceeds the size limit. Open the full output data in a text editor
轮次: 1 当前轮训练集损失: 0.6698072081866441
轮次: 2 当前轮训练集损失: 0.6441357015045888
轮次: 3 当前轮训练集损失: 0.6217225572201343
轮次: 4 当前轮训练集损失: 0.6020047939937195
轮次: 5 当前轮训练集损失: 0.5846004197913068
轮次: 6 当前轮训练集损失: 0.5691802153339961
轮次: 7 当前轮训练集损失: 0.5554626339278943
轮次: 8 当前轮训练集损失: 0.5432083414568926
轮次: 9 当前轮训练集损失: 0.5322145949254381
轮次: 10 当前轮训练集损失: 0.5223099138040177
轮次: 11 当前轮训练集损失: 0.5133492722518356
轮次: 12 当前轮训练集损失: 0.5052098949044318
轮次: 13 当前轮训练集损失: 0.49778765499909156
轮次: 14 当前轮训练集损失: 0.4909940300120578
轮次: 15 当前轮训练集损失: 0.4847535511142182
轮次: 16 当前轮训练集损失: 0.4790016781187627
轮次: 17 当前轮训练集损失: 0.4736830345323378
轮次: 18 当前轮训练集损失: 0.4687499438076212
轮次: 19 当前轮训练集损失: 0.4641612156208906
轮次: 20 当前轮训练集损失: 0.45988113871759656
轮次: 21 当前轮训练集损失: 0.45587864397137
轮次: 22 当前轮训练集损失: 0.45212660754001593
轮次: 23 当前轮训练集损失: 0.4486012693266089
轮次: 24 当前轮训练集损失: 0.4452817464155596
轮次: 25 当前轮训练集损失: 0.4421496248472059
...
轮次: 499 当前轮训练集损失: 0.32685224520496237
轮次: 500 当前轮训练集损失: 0.32682730315068426
训练最终损失: 0.32682730315068426
逻辑回归训练准确率: 85.95%
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。