深度学习笔记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函数原型

相关推荐
代码游侠5 分钟前
学习笔记——栈
开发语言·数据结构·笔记·学习·算法
光头程序员35 分钟前
学习笔记——vite 打包构建优化之tree shaking
笔记·学习
AI即插即用42 分钟前
即插即用系列 | CVPR 2024 ABC-Attention:基于双线性相关注意力的红外小目标检测
图像处理·人工智能·深度学习·目标检测·计算机视觉·cnn·视觉检测
AI即插即用43 分钟前
即插即用系列 | WACV 2025 SvANet:专为极小目标(<1%)设计的尺度变化注意力网络,医学图像分割新SOTA!
人工智能·深度学习·神经网络·目标检测·计算机视觉·cnn·视觉检测
安得权1 小时前
Office365 SSO Azure的配置笔记
笔记·flask·azure
源于花海1 小时前
昇腾Catlass的算子优化:Transformer中小批量矩阵乘法优化与性能提升实践
深度学习
走在路上的菜鸟1 小时前
Android学Dart学习笔记第十一节 分支
android·笔记·学习·flutter
源于花海1 小时前
PyTorch模型轻松迁移昇腾平台:BERT优化与RoPE自定义算子实战
深度学习
Coding茶水间2 小时前
基于深度学习的水稻虫害检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉
合合技术团队2 小时前
论文解读-潜在思维链推理的全面综述
大数据·人工智能·深度学习·大模型