import pandas as pd import numpy as np from sklearn.preprocessing import MinMaxScaler from sklearn.model_selection import train_test_split from keras.models import Sequential from keras.layers import Dense # 读取Excel文件 data = pd.read_excel('D://数据1.xlsx', sheet_name='8') # 把数据分成输入和输出 X = data.iloc[:, 0:8].values y = data.iloc[:, 0:8].values # 对输入和输出数据进行归一化 scaler_X = MinMaxScaler(feature_range=(0, 4)) X = scaler_X.fit_transform(X) scaler_y = MinMaxScaler(feature_range=(0, 4)) y = scaler_y.fit_transform(y) # 将数据集分成训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0) # 创建神经网络模型 model = Sequential() model.add(Dense(units=8, input_dim=8, activation='relu')) model.add(Dense(units=64, activation='relu')) model.add(Dense(units=8, activation='relu')) model.add(Dense(units=8, activation='linear')) # 编译模型 model.compile(loss='mean_squared_error', optimizer='sgd') # 训练模型 model.fit(X_train, y_train, epochs=230, batch_size=1000) # 评估模型 score = model.evaluate(X_test, y_test, batch_size=1258) 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) # 计算预测的概率 mse = ((y_test - y_pred) ** 2).mean(axis=None) probabilities = 1 / (1 + mse - ((y_pred_int - y_test) ** 2).mean(axis=None)) # 构建带有概率的预测结果 y_pred_prob = pd.DataFrame(y_pred_int, columns=data.columns[:8]) y_pred_prob['Probability'] = probabilities # 过滤掉和小于6或大于24的行 row_sums = np.sum(y_pred, axis=1) y_pred_filtered = y_pred[(row_sums >= 6) & (row_sums <= 6), :] # 去除重复的行 y_pred_filtered = y_pred_filtered.drop_duplicates() # 打印带有概率的预测结果 print('Predicted values with probabilities:') print(y_pred_filtered)显示Traceback (most recent call last): File "D:\pycharm\PyCharm Community Edition 2023.1.1\双色球8分区预测模型.py", line 61, in <module> y_pred_filtered = y_pred_filtered.drop_duplicates() AttributeError: 'numpy.ndarray' object has no attribute 'drop_duplicates'怎么修改
时间: 2024-02-13 19:07:25 浏览: 175
pandas.read_csv参数详解(小结)
你需要将 `y_pred_filtered` 数组转换为 pandas DataFrame,然后再使用 `drop_duplicates()` 方法进行去重。你可以使用 `pd.DataFrame()` 将 numpy 数组转换为 DataFrame,如下所示:
```
import pandas as pd
import numpy as np
# ... 其他代码 ...
# 对 y_pred 进行过滤和去重
row_sums = np.sum(y_pred, axis=1)
y_pred_filtered = y_pred[(row_sums >= 6) & (row_sums <= 24), :]
y_pred_filtered_df = pd.DataFrame(data=y_pred_filtered, columns=data.columns[:8])
y_pred_filtered_df.drop_duplicates(inplace=True)
# 打印预测结果
print('Predicted values with probabilities:')
print(y_pred_filtered_df)
```
这应该可以解决你遇到的问题。
阅读全文