import pandas as pd from sklearn.preprocessing import MinMaxScaler from sklearn.model_selection import train_test_split from keras.models import Sequential from keras.layers import Dense from keras.models import load_model model = load_model('model.h5') # 读取Excel文件 data = pd.read_excel('D://数据1.xlsx', sheet_name='4') # 把数据分成输入和输出 X = data.iloc[:, 0:5].values y = data.iloc[:, 0:5].values # 对输入和输出数据进行归一化 scaler_X = MinMaxScaler(feature_range=(0, 6)) X = scaler_X.fit_transform(X) scaler_y = MinMaxScaler(feature_range=(0, 6)) y = scaler_y.fit_transform(y) # 将数据集分成训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) # 创建神经网络模型 model = Sequential() model.add(Dense(units=4, input_dim=4, activation='relu')) model.add(Dense(units=36, activation='relu')) model.add(Dense(units=4, activation='relu')) model.add(Dense(units=4, activation='linear')) # 编译模型 model.compile(loss='mean_squared_error', optimizer='sgd') # 训练模型 model.fit(X_train, y_train, epochs=100, batch_size=1257) # 评估模型 score = model.evaluate(X_test, y_test, batch_size=30) print('Test loss:', score) # 使用训练好的模型进行预测 X_test_scaled = scaler_X.transform(X_test) y_pred = model.predict(X_test_scaled) # 对预测结果进行反归一化 y_pred_int = scaler_y.inverse_transform(y_pred).round().astype(int) # 构建带有概率的预测结果 y_pred_prob = pd.DataFrame(y_pred_int, columns=data.columns[:4]) mse = ((y_test - y_pred) ** 2).mean(axis=None) y_pred_prob['Probability'] = 1 / (1 + mse - ((y_pred_int - y_test) ** 2).mean(axis=None)) # 过滤掉和值超过6或小于6的预测值 y_pred_filtered = y_pred_prob[(y_pred_prob.iloc[:, :4].sum(axis=1) == 6)] # 去除重复的行 y_pred_filtered = y_pred_filtered.drop_duplicates() # 重新计算低于1.2的 Probability 值 low_prob_indices = y_pred_filtered[y_pred_filtered['Probability'] < 1.5].index for i in low_prob_indices: y_pred_int_i = y_pred_int[i] y_test_i = y_test[i] mse_i = ((y_test_i - y_pred_int_i) ** 2).mean(axis=None) new_prob_i = 1 / (1 + mse_i - ((y_pred_int_i - y_test_i) ** 2).mean(axis=None)) y_pred_filtered.at[i, 'Probability'] = new_prob_i # 打印带有概率的预测结果 print('Predicted values with probabilities:') print(y_pred_filtered)这段代码有问题,你帮忙改一下
时间: 2024-02-09 19:09:33 浏览: 124
keras_IMDB_keras_kerasidmb二分类_二分类_
经过仔细查看代码,我发现问题出在第7行和第8行。它们应该分别是:
```python
X = data.iloc[:, :4].values
y = data.iloc[:, 4].values.reshape(-1, 1)
```
这样就可以将输入数据和输出数据正确地分离开来了。同时,第37行的 `y_test` 应该是 `y_test_scaled`。修改后的代码如下:
```python
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model
model = load_model('model.h5')
# 读取Excel文件
data = pd.read_excel('D://数据1.xlsx', sheet_name='4')
# 把数据分成输入和输出
X = data.iloc[:, :4].values
y = data.iloc[:, 4].values.reshape(-1, 1)
# 对输入和输出数据进行归一化
scaler_X = MinMaxScaler(feature_range=(0, 6))
X = scaler_X.fit_transform(X)
scaler_y = MinMaxScaler(feature_range=(0, 6))
y = scaler_y.fit_transform(y)
# 将数据集分成训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# 使用训练好的模型进行预测
X_test_scaled = scaler_X.transform(X_test)
y_test_scaled = scaler_y.transform(y_test)
y_pred_scaled = model.predict(X_test_scaled)
# 对预测结果进行反归一化
y_pred = scaler_y.inverse_transform(y_pred_scaled)
# 构建带有概率的预测结果
y_pred_prob = pd.DataFrame(y_pred, columns=['Prediction'])
mse = ((y_test_scaled - y_pred_scaled) ** 2).mean(axis=None)
y_pred_prob['Probability'] = 1 / (1 + mse - ((y_pred_scaled - y_test_scaled) ** 2).mean(axis=None))
# 过滤掉和值超过6或小于1的预测值
y_pred_filtered = y_pred_prob[(y_pred_prob.iloc[:, :1].sum(axis=1) <= 6) & (y_pred_prob.iloc[:, :1].sum(axis=1) >= 1)]
# 去除重复的行
y_pred_filtered = y_pred_filtered.drop_duplicates()
# 重新计算低于1.2的 Probability 值
low_prob_indices = y_pred_filtered[y_pred_filtered['Probability'] < 1.2].index
for i in low_prob_indices:
y_pred_i = y_pred[i]
y_test_i = y_test[i]
mse_i = ((y_test_i - y_pred_i) ** 2).mean(axis=None)
new_prob_i = 1 / (1 + mse_i - ((y_pred_i - y_test_i) ** 2).mean(axis=None))
y_pred_filtered.at[i, 'Probability'] = new_prob_i
# 打印带有概率的预测结果
print('Predicted values with probabilities:')
print(y_pred_filtered)
```
请注意,这段代码需要在正确的环境中运行,且文件路径需要根据实际情况修改。
阅读全文