import pandas as pd from keras.models import Sequential from keras.layers import Dense, SimpleRNN from sklearn.metrics import mean_squared_error # 从Excel文件中读取数据 data = pd.read_excel('D://数据1.xlsx', sheet_name='8') X = data.iloc[:, 0:8].values Y = data.iloc[:, 0:8].values # 将数据划分为训练集和测试集 train_size = int(len(X) * 0.7) test_size = len(X) - train_size X_train, X_test = X[0:train_size, :], X[train_size:len(X), :] Y_train, Y_test = Y[0:train_size, :], Y[train_size:len(Y), :] # 创建RNN模型 model = Sequential() model.add(SimpleRNN(units=50, input_shape=(8, 1))) model.add(Dense(units=8, activation='linear')) model.compile(loss='mean_squared_error', optimizer='adam') # 训练模型 model.fit(X_train.reshape(-1, 8, 1), Y_train, epochs=100, batch_size=32) # 在测试集上进行预测 predicted = model.predict(X_test.reshape(-1, 8, 1)) # 输出预测结果和均方误差 print("预测结果:\n", predicted) print("均方误差:", mean_squared_error(Y_test, predicted))这个程序里面加入预测值四舍五入取整值
时间: 2024-02-19 20:57:56 浏览: 122
first_NN_keras_keras_神经网络_多层感知机_糖尿病数据集_糖尿病_
好的,您可以在预测后将predicted中的值使用round()函数四舍五入为整数,如下所示:
```python
# 在测试集上进行预测
predicted = model.predict(X_test.reshape(-1, 8, 1))
# 四舍五入取整
predicted = predicted.round()
# 输出预测结果和均方误差
print("预测结果:\n", predicted)
print("均方误差:", mean_squared_error(Y_test, predicted))
```
这将使predicted中的值四舍五入为整数,以便更好地与实际值进行比较。
阅读全文