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

相关推荐
Brilliantwxx1 分钟前
【C++】String的模拟实现(代码实现与坑点讲解)
开发语言·c++·笔记·算法
DogDaoDao2 分钟前
【GitHub】OpenClaw:开源个人AI助手的新标杆
人工智能·深度学习·开源·大模型·github·ai编程·opeclaw
机器学习之心18 分钟前
信号分解+深度学习+RUL预测!MVMD-Transformer-BiGRU锂电池剩余寿命预测(容量特征提取+剩余寿命预测)
深度学习·transformer·锂电池剩余寿命预测
zhangrelay19 分钟前
ROS Kinetic-信号与系统-趣味案例
linux·笔记·学习·ubuntu
羊群智妍23 分钟前
2026 GEO监测工具|AI搜索优化技术方案与选型
笔记
maosheng114629 分钟前
RHCE的第一次笔记
服务器·网络·笔记
大江东去浪淘尽千古风流人物32 分钟前
【DROID-W / WildGS-SLAM】动态场景SLAM:不确定性驱动BA、3DGS建图与10个核心优化点深度解析
深度学习
ZC跨境爬虫33 分钟前
跟着 MDN 学 HTML day_8:(高级文本语义标签+适配核心功底)
前端·css·笔记·ui·html
就叫飞六吧35 分钟前
Hermes Agent 完整总结
笔记
HERR_QQ43 分钟前
端到端课程自用 5 规划 基于Difussion 的端到端planner AI 笔记
人工智能·笔记·学习·自动驾驶