神经网络建模预测

1 我们用tensorflow去构建神经网络

import tensorflow as tf
tf.enable_eager_execution()
# 导入库
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline

2 数据

data = pd.read_csv(r'/home//data.csv')

3 神经网络建模

# 数据划分
x = data.iloc[:, 3:]
y = data.iloc[:, 2]
# 搭建神经网络
model = tf.keras.Sequential([tf.keras.layers.Dense(600, input_shape=(12,), activation='tanh'), # tanh
                             tf.keras.layers.Dense(300, activation='relu'),
                             tf.keras.layers.Dense(150, activation='relu'),
                             tf.keras.layers.Dense(50, activation='relu'),
                             tf.keras.layers.Dense(25, activation='relu'),
                             tf.keras.layers.Dense(1)])
print(model.summary())
# 设置优化模型、损失函数
model.compile(optimizer='adam', loss='mse',metrics=['mape'])   
# 模型训练,迭代次数为150
history = model.fit(x, y, epochs=150,batch_size=10,validation_split=0.1) # ,varbose=0

模型训练层数情况:
Colocations handled automatically by placer.


Layer (type) Output Shape Param #

dense (Dense) (None, 600) 7800


dense_1 (Dense) (None, 300) 180300


dense_2 (Dense) (None, 150) 45150


dense_3 (Dense) (None, 50) 7550


dense_4 (Dense) (None, 25) 1275


dense_5 (Dense) (None, 1) 26

Total params: 242,101
Trainable params: 242,101
Non-trainable params: 0

网络迭代以及指标:
Epoch 1/150
18/18 [] - 1s 57ms/sample - loss: 6848.0991 - mean_absolute_percentage_error: 97.4036 - val_loss: 8904.9209 - val_mean_absolute_percentage_error: 94.7421
Epoch 2/150
18/18 [
] - 0s 445us/sample - loss: 6045.1309 - mean_absolute_percentage_error: 87.6766 - val_loss: 7586.9141 - val_mean_absolute_percentage_error: 87.4439
Epoch 3/150
18/18 [] - 0s 403us/sample - loss: 4934.8857 - mean_absolute_percentage_error: 76.1649 - val_loss: 5794.1787 - val_mean_absolute_percentage_error: 76.4067



Epoch 147/150
18/18 [
] - 0s 4ms/sample - loss: 60.0847 - mean_absolute_percentage_error: 7.7134 - val_loss: 3.8309 - val_mean_absolute_percentage_error: 1.9660
Epoch 148/150
18/18 [] - 0s 395us/sample - loss: 59.0289 - mean_absolute_percentage_error: 7.8850 - val_loss: 4.7361 - val_mean_absolute_percentage_error: 1.9454
Epoch 149/150
18/18 [
] - 0s 416us/sample - loss: 60.0656 - mean_absolute_percentage_error: 7.9295 - val_loss: 5.1167 - val_mean_absolute_percentage_error: 1.9418
Epoch 150/150
18/18 [==============================] - 0s 413us/sample - loss: 58.7168 - mean_absolute_percentage_error: 8.0490 - val_loss: 4.7343 - val_mean_absolute_percentage_error: 1.9454

4 查看过拟情况

def plot_history(history):
    hist = pd.DataFrame(history.history)
    hist['epoch'] = history.epoch
    plt.figure(figsize=(25,8))
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.plot(hist['epoch'], hist['loss'],
             label='Train Error')
    plt.plot(hist['epoch'], hist['val_loss'],
             label = 'Val Error')
    plt.legend()
    plt.show()
plot_history(history)

在这里插入图片描述
注: 训练集和测试集指标查看,不存在过拟合和欠拟合问题,所以这个模型是可以用来预测的。

5 预测数据

prediction_data = model.predict(data_.iloc[:,1:])

相关推荐

  1. 【分形技术在神经网络中的应用】

    2024-01-18 07:26:04       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 07:26:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 07:26:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 07:26:04       82 阅读
  4. Python语言-面向对象

    2024-01-18 07:26:04       91 阅读

热门阅读

  1. Linux 使用 curl 调用接口

    2024-01-18 07:26:04       60 阅读
  2. 微软推出了Copilot Pro 每月20美金

    2024-01-18 07:26:04       56 阅读
  3. AWS Secrets Manager 实战指南

    2024-01-18 07:26:04       59 阅读
  4. 敲诈勒索该怎么办

    2024-01-18 07:26:04       51 阅读
  5. 汽车售后服务客户满意度调查报告

    2024-01-18 07:26:04       52 阅读
  6. Spring boot 常见注解

    2024-01-18 07:26:04       50 阅读
  7. ChatGPT 和 文心一言 的优缺点及需求和使用场景

    2024-01-18 07:26:04       48 阅读
  8. GitLab服务器忘记root密码处理方式

    2024-01-18 07:26:04       52 阅读
  9. 【VUE】点击按钮下载全部链接文件

    2024-01-18 07:26:04       56 阅读