利用深度学习模型BiLSTM进行数据预测和分析

  1. 导入必要的库和模块:

```python

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score

import matplotlib.pyplot as plt

import numpy as np

import tensorflow as tf

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Bidirectional, LSTM, Dense

```

  1. 加载数据并准备训练集和测试集:

```python

data = pd.read_excel('c.xlsx').iloc[0:, 1:]

X, y = data.iloc[:, 0:-1], data.iloc[:, -1]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, shuffle=False)

```

  1. 缩放特征:

```python

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)

X_test_scaled = scaler.transform(X_test)

```

  1. 构建 BiLSTM 模型并进行训练:

```python

model = Sequential()

model.add(Bidirectional(LSTM(units=64, return_sequences=True), input_shape=(X_train_scaled.shape[1], X_train_scaled.shape[2])))

model.add(Dense(1)) # Regression problems typically have output layer with one neuron

model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(X_train_scaled, y_train, epochs=10, batch_size=32)

```

  1. 在测试集上进行预测并评估模型:

```python

y_pred = model.predict(X_test_scaled)

mse = mean_squared_error(y_test, y_pred)

rmse = np.sqrt(mse)

mae = mean_absolute_error(y_test, y_pred)

r2 = r2_score(y_test, y_pred)

print("RMSE:", rmse)

print("MAE:", mae)

print("R²:", r2)

```

  1. 绘制拟合对比曲线图:

```python

plt.figure(figsize=(10, 6))

plt.plot(range(len(y_test)), y_test, color='darkorange', label='Actual')

plt.plot(range(len(y_pred)), y_pred, color='navy', linewidth=2, label='Predicted')

plt.xlabel('Sample Index')

plt.ylabel('Target Variable')

plt.title('BiLSTM Regression Fit Comparison')

plt.legend()

plt.grid(True)

plt.show()

```

相关推荐
jinanwuhuaguo5 分钟前
OpenClaw全网使用人群全景深度分析报告
网络·人工智能·网络协议·rpc·openclaw
踩着两条虫7 分钟前
AI驱动的Vue3应用开发平台 深入探究(十四):扩展与定制之插件系统开发指南
vue.js·人工智能·低代码·重构·架构
spider'19 分钟前
ROS2开发环境搭建
人工智能
Yiyaoshujuku26 分钟前
医院API接口,从医院真实世界数据HIS、LJS、EMR、PACS系统到医院药品流向数据....
大数据·前端·人工智能
STLearner30 分钟前
AI论文速读 | 元认知监控赋能深度搜索:认知神经科学启发的分层优化框架
大数据·论文阅读·人工智能·python·深度学习·学习·机器学习
AI浩39 分钟前
第 2 章:Claude Code 环境搭建与API配置。
人工智能·目标检测
不一样的故事1261 小时前
抓重点、留弹性、重节奏
大数据·网络·人工智能·安全
another heaven1 小时前
【深度学习 超参调优】optimizer=‘SGD‘ / ‘AUTO‘
人工智能·深度学习
balmtv1 小时前
从“知识检索”到“深度推理”:Gemini 3.1如何用三层思考模式解决学术难题
人工智能·gpt·chatgpt
2501_926978331 小时前
《与AI的妄想对话:如何给机器人造灵魂?》
人工智能·深度学习·机器学习·ai写作·agi