^_^ 2023-03-31 16:39 采纳率: 57.9%
浏览 16
已结题

python程序模拟后的输出

使用卷积神经网络预测之后输出了实际与预测的曲线图,我想知道实际与预测的在图中的具体的点的坐标,如何将其输出。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Conv1D, MaxPooling1D, Flatten, Dense

1.
#数据清洗
data = pd.read_csv('zhendaorushuju.csv', encoding='gb18030')

2.
#特征选择
features = ['Nb%', 'Ti%', 'AL%', 'V%', 'Cr%', 'Mo%', 'C%', 'Mn%', 'P%', 'Ni%', 'Cu%', 'Si%', 'S%', '奥氏体化温度0℃',
            '油冷时间0min', '保温时间0min', '上下窜动时间0min', '回火0退火温度0℃', '保温时间0min']
X = data[features]
y = data['硬度']

3.
#特征缩放
scaler = MinMaxScaler()
X = scaler.fit_transform(X)

4.
#数据划分
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=400)

5.
#重塑数据为三维数组
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))

6.
#构建卷积神经网络模型
model = Sequential()
model.add(Conv1D(filters=33, kernel_size=6, activation='relu', input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(MaxPooling1D(pool_size=1))
model.add(Flatten())
model.add(Dense(2000, activation='relu'))
model.add(Dense(1))

7.
#编译模型
model.compile(optimizer='adam', loss='mse')

8.
#训练模型
history = model.fit(X_train, y_train, epochs=78, batch_size=6, validation_data=(X_test, y_test), verbose=0)

9.
#评估模型
score = model.evaluate(X_test, y_test, verbose=0)
print('Test loss:', score)

10.
#使用模型进行预测
y_pred = model.predict(X_test)

11.
#绘制预测结果与真实结果的对比图
plt.plot(y_test.values, label='true')
plt.plot(y_pred, label='pred')
plt.legend()
plt.show()

  • 写回答

2条回答 默认 最新

  • apples_kk 2023-03-31 16:45
    关注

    可以使用matplotlib库进行输出。可以使用matplotlib.pyplot.plot函数输出曲线图,matplotlib.pyplot.get_lines函数用来获取xdata和ydata数据,用于表示实际与预测在图中的具体点的坐标。例如:

    import matplotlib.pyplot as plt
    
    # plot the line chart
    plt.plot(x_data, y_data)
    
    # get the coordinates of each point
    lines = plt.get_lines()
    
    # loop through all lines
    for line in lines: 
        x_points = line.get_xdata()
        y_points = line.get_ydata()
        print('x points: %s, y points: %s' % (x_points, y_points))
    
    # show the line chart
    plt.show()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月1日
  • 已采纳回答 4月23日
  • 创建了问题 3月31日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)