加载数据 X = data.iloc[:, :-1].values y = data.iloc[:, -1:].values X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 训练模型 input_dim=13 hidden_dim=25 output_dim=1 nn=NeuralNetwork(input_dim, hidden_dim, output_dim) learning_rate=0.0016 num_epochs=2000 loss_history=nn.train(X, y, learning_rate, num_epochs) plt.plot(loss_history) plt.title('loss') plt.xlabel('Epoch') plt.ylabel('MSE') X_pred = data_pred.iloc[:, :-1].values y_pred = data_pred.iloc[:, -1:].values y_pred = nn.predict(X_pred)报错NameError: name 'data_pred' is not defined解决代码
时间: 2024-03-26 22:39:16 浏览: 118
Pandas-Data-Manipulation:数据操作-Python-Pandas
你需要先定义 `data_pred` 变量并赋值,再执行 `X_pred = data_pred.iloc[:, :-1].values` 和 `y_pred = data_pred.iloc[:, -1:].values` 这两行代码。例如:
```python
import pandas as pd
# 加载数据
data = pd.read_csv('train_data.csv')
data_pred = pd.read_csv('predict_data.csv')
X = data.iloc[:, :-1].values
y = data.iloc[:, -1:].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 训练模型
input_dim = 13
hidden_dim = 25
output_dim = 1
nn = NeuralNetwork(input_dim, hidden_dim, output_dim)
learning_rate = 0.0016
num_epochs = 2000
loss_history = nn.train(X, y, learning_rate, num_epochs)
plt.plot(loss_history)
plt.title('loss')
plt.xlabel('Epoch')
plt.ylabel('MSE')
X_pred = data_pred.iloc[:, :-1].values
y_pred = data_pred.iloc[:, -1:].values
y_pred = nn.predict(X_pred)
```
阅读全文