请将下列MATLAB程序转为python并列出需要的函数库:data = importdatatemptxt;x = data 1;t = data 2;y = data 4; 神经网络拟合inputs = x t;net = fitnet100; 使用10个隐藏层神经元的神经网络net = trainnet inputs y; 训练神经网络x_range = 15;t_range = 011
需要的函数库:
- numpy
- matplotlib
- sklearn
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.neural_network import MLPRegressor
# 读取数据
data = np.loadtxt('temp.txt')
x = data[:, 0]
t = data[:, 1]
y = data[:, 3]
# 神经网络拟合
inputs = np.column_stack((x, t))
net = MLPRegressor(hidden_layer_sizes=(100,))
net.fit(inputs, y)
x_range = np.arange(1, 6)
t_range = np.arange(0, 1200)
X, T = np.meshgrid(x_range, t_range)
z = np.zeros((len(x_range), len(t_range)))
for i in range(len(x_range)):
for j in range(len(t_range)):
W = np.array([[x_range[i], t_range[j]]])
z[i, j] = net.predict(W)
Y = z.reshape(-1, len(t_range))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, T, Y, cmap='jet')
ax.scatter(x, t, y, c='b', marker='o')
ax.set_xlabel('U/V')
ax.set_ylabel('t/s')
ax.set_zlabel('浓度/%')
ax.set_title('拟合结果')
plt.colorbar()
plt.show()
# 保存拟合结果
import joblib
joblib.dump(net, 'trained_model.pkl')
``
原文地址: https://www.cveoy.top/t/topic/ipNA 著作权归作者所有。请勿转载和采集!