当前位置: 首页 > news >正文

【深度学习练习】心脏病预测

  • 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
  • 🍖 原作者:K同学啊

一、什么是RNN

RNN与传统神经网络最大的区别在于,每次都会将前一次的输出结果,带到下一隐藏层中一起训练。如下图所示:
在这里插入图片描述

二、前期工作

1. 设置GPU

import tensorflow as tfgpus = tf.config.list_physical_devices("GPU")if gpus:gpu0 = gpus[0]                                        #如果有多个GPU,仅使用第0个GPUtf.config.experimental.set_memory_growth(gpu0, True)  #设置GPU显存用量按需使用tf.config.set_visible_devices([gpu0],"GPU")

2. 导入数据

数据介绍:

age:年龄
sex:性别
cp:胸痛类型 (4 values)
trestbps:静息血压
chol:血清胆甾醇 (mg/dl)
fbs:空腹血糖 > 120 mg/dl
restecg:静息心电图结果 (值 0,1 ,2)
thalach:达到的最大心率
exang:运动诱发的心绞痛
oldpeak:相对于静止状态,运动引起的ST段压低
slope:运动峰值 ST 段的斜率
ca:荧光透视着色的主要血管数量 (0-3)
thal:0 = 正常;1 = 固定缺陷;2 = 可逆转的缺陷
target:0 = 心脏病发作的几率较小 1 = 心脏病发作的几率更大

import pandas as pd
import numpy as npdf = pd.read_csv(r"D:\Personal Data\Learning Data\DL Learning Data\heart.csv")
df

输出:
在这里插入图片描述

3. 检查数据

df.isnull().sum()

输出:
在这里插入图片描述

三、数据预处理

1. 划分数据集

from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_splitx = df.iloc[:,:-1]
y = df.iloc[:,-1]x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=1)
x_train.shape, y_train.shape

输出:
在这里插入图片描述

2. 标准化

# 将每一列特征标准化为标准正太分布,注意,标准化是针对每一列而言的
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test  = sc.transform(x_test)x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)
x_test  = x_test.reshape(x_test.shape[0], x_test.shape[1], 1)

3. 构建RNN模型

import tensorflow
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,LSTM,SimpleRNNmodel = Sequential()
model.add(SimpleRNN(128, input_shape= (13,1),return_sequences=True,activation='relu'))
model.add(SimpleRNN(64,return_sequences=True, activation='relu'))
model.add(SimpleRNN(32, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()

输出:
在这里插入图片描述

五、编译模型

opt = tf.keras.optimizers.Adam(learning_rate=1e-4)
model.compile(loss='binary_crossentropy', optimizer=opt,metrics=['accuracy'])

六、训练模型

epochs = 50history = model.fit(x_train, y_train,epochs=epochs,batch_size=128,validation_data=(x_test, y_test),verbose=1)

部分输出:
在这里插入图片描述

model.evaluate(x_test,y_test)

输出:
在这里插入图片描述

七、模型评估

import matplotlib.pyplot as pltacc = history.history['accuracy']
val_acc = history.history['val_accuracy']loss = history.history['loss']
val_loss = history.history['val_loss']epochs_range = range(epochs)plt.figure(figsize=(14, 4))
plt.subplot(1, 2, 1)plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

最后准确率输出:

scores = model.evaluate(x_test, y_test, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

八、总结

  1. 注意numpy与panda以及matplotlib等之间的兼容性
  2. 注意对每一列的特征数据标准化处理
http://www.lryc.cn/news/394964.html

相关文章:

  • 创建react的脚手架
  • 用例导图CMind
  • C++ 仿函数
  • Redhat 安装 docker 网络连接超时问题
  • Java面试题:undo log和redo log
  • 【Scrapy】Scrapy 中间件等级设置规则
  • SDK环境的安装(测试使用)
  • 【matlab】【python】爬虫实战
  • Android TV跨平台开发心得
  • View->裁剪框View的绘制,手势处理
  • 语言模型的进化:从NLP到LLM的跨越之旅
  • 应急响应--网站(web)入侵篡改指南
  • vue3+vue-router+vite 实现动态路由
  • Okhttp hostnameVerifier详解
  • TCP的p2p网络模式
  • 力扣-贪心算法4
  • 动手学深度学习6.2 图像卷积-笔记练习(PyTorch)
  • 展开说说:Android服务之bindService解析
  • node-sass 老版本4.14.0 安装失败解决办法
  • 最近很火的字幕截图生成器
  • 使用RabbitMQ实现可靠的消息传递机制
  • Function Call ReACT,Agent应用落地的加速器_qwen的function calling和react有什么不同
  • Java的JSONPath(fastjson)使用总结
  • 【大模型】大语言模型:光鲜背后的阴影——事实准确性和推理能力的挑战
  • Java面向对象练习(1.手机类)(2024.7.4)
  • 智慧生活新篇章,Vatee万腾平台领航前行
  • Spring Cloud Gateway报sun.misc.Unsafe.park(Native Method)
  • select single , select endselect
  • 后端学习(一)
  • 【活动行】参与上海两场线下活动,教育生态行业赛总决赛活动和WAIC人工智能大会活动 - 上海活动总结