深度学习笔记22-RNN心脏病预测(Tensorflow)

一、前期准备

1.导入数据

python 复制代码
import tensorflow as tf
import pandas as pd
import numpy as np
df=pd.read_csv("E:/heart.csv")
df

2.检查数据是否有空值

python 复制代码
df.isnull().sum()

二、数据预处理

1.划分训练集与测试集

python 复制代码
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
X=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)

2.标准化

python 复制代码
#将每一列特征标准化为标准正太分布,注意,标准化是针对每一列而言的#
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)

三、构建RNN模型

python 复制代码
import tensorflow
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,LSTM,SimpleRNN
model= Sequential()
model.add(SimpleRNN(200,input_shape=(13,1),activation='relu'))
model.add(Dense(100,activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.summary()

四、编译模型

python 复制代码
opt=tf.keras.optimizers.Adam(learning_rate=0.0001)
model.compile(loss='binary_crossentropy',optimizer=opt,metrics=['accuracy'])

五、训练模型

python 复制代码
epochs=100
history=model.fit(X_train,y_train,epochs=epochs,batch_size=128,validation_data=(X_test,y_test),verbose=1)

六、模型评估

python 复制代码
import matplotlib.pyplot as plt
from datetime import datetime
current_time=datetime.now()
acc=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='Test Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.xlabel(current_time) # 打卡请带上时间戳,否则代码截图无效

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Test Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
python 复制代码
scores=model.evaluate(X_test,y_test,verbose=0)
print("%s:%2f%%" % (model.metrics_names[1],scores[1]*100))

compile_metrics:83.6451%

七、总结

1.RNN函数原型

相关推荐
YOLO数据集集合40 分钟前
无人机山地灾害巡检数据集 | 滑坡多区域实例分割 遥感影像解译 地质灾害预警深度学习数据10296期
人工智能·深度学习·目标检测·计算机视觉·无人机
袁小皮皮不皮1 小时前
1.HCIP BFD 学习笔记(优化版)
服务器·网络·笔记·网络协议·学习·智能路由器·ip
手写码匠2 小时前
手写 GraphRAG:从零实现图增强检索增强生成系统
人工智能·深度学习·算法·aigc
装不满的克莱因瓶2 小时前
【自动驾驶领域】学习 Cityscapes 数据集——城市街景语义理解的标准基准
人工智能·pytorch·python·深度学习·学习·机器学习·自动驾驶
清辞8533 小时前
产品经理需求推进流程
大数据·深度学习·学习·产品经理
一一哥Sun4 小时前
第06课:Transformer与注意力机制——大模型背后的秘密武器
人工智能·深度学习·transformer
卡梅德生物科技小能手4 小时前
卡梅德生物科普CD124(IL-4Rα):2型免疫炎症的核心调控靶点
人工智能·经验分享·深度学习
LaughingZhu5 小时前
Product Hunt 每日热榜 | 2026-06-12
人工智能·经验分享·深度学习·神经网络·产品运营
数据门徒5 小时前
神经网络原理 第十一章:植根于统计力学的随机机器和它们的逼近
人工智能·深度学习·神经网络
hhcgchpspk6 小时前
汇编语言传递数据和地址的误区
汇编·笔记·nasm·masm