修改下列代码第20行的错误:import numpy as np import matplotlib.pyplot as plt from sklearn.neural_network import MLPRegressor from sklearn.metrics import mean_squared_error #输入自变量和因变量 X = np.array([7.36, 7.37, 7.37, 7.39, 7.4]).reshape(-1, 1) y = np.array([7.37, 7.37, 7.39, 7.4, 7.41]) #创建并训练人工神经网络模型 model = MLPRegressor(hidden_layer_sizes=(50, 50), activation='relu', solver='adam') model.fit(X, y) #预测新的自变量对应的因变量 X_new = np.array([7.41]).reshape(-1, 1) y_pred = model.predict(X_new) print(y_pred) #计算均方误差(MSE) mse = mean_squared_error(y, y_pred) #计算均方根误差(RMSE) rmse = np.sqrt(mse) print("均方误差(MSE):", mse) print("均方根误差(RMSE):", rmse)
时间: 2024-04-09 11:31:06 浏览: 66
import numpy as np import matplotlib.pyplot as plt import math
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error
# 输入自变量和因变量
X = np.array([7.36, 7.37, 7.37, 7.39, 7.4]).reshape(-1, 1)
y = np.array([7.37, 7.37, 7.39, 7.4, 7.41])
# 创建并训练人工神经网络模型
model = MLPRegressor(hidden_layer_sizes=(50, 50), activation='relu', solver='adam')
model.fit(X, y)
# 预测新的自变量对应的因变量
X_new = np.array([[7.41]]) # 修改错误:将数组的形状由 (-1, 1) 改为 (1, 1)
y_pred = model.predict(X_new)
print(y_pred)
# 计算均方误差(MSE)
mse = mean_squared_error(y, y_pred)
# 计算均方根误差(RMSE)
rmse = np.sqrt(mse)
print("均方误差(MSE):", mse)
print("均方根误差(RMSE):", rmse)
阅读全文