修改代码,使得输出结果是可重复的:# 定义模型参数 input_dim = X_train.shape[1] epochs = 100 batch_size = 32 learning_rate = 0.01 dropout_rate = 0.7 # 定义模型结构 def create_model(): model = Sequential() model.add(Dense(64, input_dim=input_dim, activation='relu')) model.add(Dropout(dropout_rate)) model.add(Dense(32, activation='relu')) model.add(Dropout(dropout_rate)) model.add(Dense(1, activation='sigmoid')) optimizer = Adam(learning_rate=learning_rate) model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy']) return model # 5折交叉验证 kf = KFold(n_splits=5, shuffle=True, random_state=42) cv_scores = [] for train_index, test_index in kf.split(X_train): # 划分训练集和验证集 X_train_fold, X_val_fold = X_train.iloc[train_index], X_train.iloc[test_index] y_train_fold, y_val_fold = y_train_forced_turnover_nolimited.iloc[train_index], y_train_forced_turnover_nolimited.iloc[test_index] # 创建模型 model = create_model() # 定义早停策略 #early_stopping = EarlyStopping(monitor='val_loss', patience=10, verbose=1) # 训练模型 model.fit(X_train_fold, y_train_fold, validation_data=(X_val_fold, y_val_fold), epochs=epochs, batch_size=batch_size,verbose=1) # 预测验证集 y_pred = model.predict(X_val_fold) # 计算AUC指标 auc = roc_auc_score(y_val_fold, y_pred) cv_scores.append(auc) # 输出交叉验证结果 print('CV AUC:', np.mean(cv_scores)) # 在全量数据上重新训练模型 model = create_model() model.fit(X_train, y_train_forced_turnover_nolimited, epochs=epochs, batch_size=batch_size, verbose=1) #测试集结果 test_pred = model.predict(X_test) test_auc = roc_auc_score(y_test_forced_turnover_nolimited, test_pred) test_f1_score = f1_score(y_test_forced_turnover_nolimited, np.round(test_pred)) test_accuracy = accuracy_score(y_test_forced_turnover_nolimited, np.round(test_pred)) print('Test AUC:', test_auc) print('Test F1 Score:', test_f1_score) print('Test Accuracy:', test_accuracy) #训练集结果 train_pred = model.predict(X_train) train_auc = roc_auc_score(y_train_forced_turnover_nolimited, train_pred) train_f1_score = f1_score(y_train_forced_turnover_nolimited, np.round(train_pred)) train_accuracy = accuracy_score(y_train_forced_turnover_nolimited, np.round(train_pred)) print('Train AUC:', train_auc) print('Train F1 Score:', train_f1_score) print('Train Accuracy:', train_accuracy)
时间: 2023-12-24 10:13:14 浏览: 214
为了使输出结果可重复,可以在代码开头设置随机种子。修改后的代码如下:
```python
import numpy as np
import pandas as pd
from sklearn.metrics import roc_auc_score, f1_score, accuracy_score
from sklearn.model_selection import KFold
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping
# 设置随机种子
np.random.seed(42)
# 定义模型参数
input_dim = X_train.shape[1]
epochs = 100
batch_size = 32
learning_rate = 0.01
dropout_rate = 0.7
# 定义模型结构
def create_model():
model = Sequential()
model.add(Dense(64, input_dim=input_dim, activation='relu'))
model.add(Dropout(dropout_rate))
model.add(Dense(32, activation='relu'))
model.add(Dropout(dropout_rate))
model.add(Dense(1, activation='sigmoid'))
optimizer = Adam(learning_rate=learning_rate)
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
# 5折交叉验证
kf = KFold(n_splits=5, shuffle=True, random_state=42)
cv_scores = []
for train_index, test_index in kf.split(X_train):
# 划分训练集和验证集
X_train_fold, X_val_fold = X_train.iloc[train_index], X_train.iloc[test_index]
y_train_fold, y_val_fold = y_train_forced_turnover_nolimited.iloc[train_index], y_train_forced_turnover_nolimited.iloc[test_index]
# 创建模型
model = create_model()
# 定义早停策略
#early_stopping = EarlyStopping(monitor='val_loss', patience=10, verbose=1)
# 训练模型
model.fit(X_train_fold, y_train_fold, validation_data=(X_val_fold, y_val_fold), epochs=epochs, batch_size=batch_size,verbose=1)
# 预测验证集
y_pred = model.predict(X_val_fold)
# 计算AUC指标
auc = roc_auc_score(y_val_fold, y_pred)
cv_scores.append(auc)
# 输出交叉验证结果
print('CV AUC:', np.mean(cv_scores))
# 在全量数据上重新训练模型
model = create_model()
model.fit(X_train, y_train_forced_turnover_nolimited, epochs=epochs, batch_size=batch_size, verbose=1)
#测试集结果
test_pred = model.predict(X_test)
test_auc = roc_auc_score(y_test_forced_turnover_nolimited, test_pred)
test_f1_score = f1_score(y_test_forced_turnover_nolimited, np.round(test_pred))
test_accuracy = accuracy_score(y_test_forced_turnover_nolimited, np.round(test_pred))
print('Test AUC:', test_auc)
print('Test F1 Score:', test_f1_score)
print('Test Accuracy:', test_accuracy)
#训练集结果
train_pred = model.predict(X_train)
train_auc = roc_auc_score(y_train_forced_turnover_nolimited, train_pred)
train_f1_score = f1_score(y_train_forced_turnover_nolimited, np.round(train_pred))
train_accuracy = accuracy_score(y_train_forced_turnover_nolimited, np.round(train_pred))
print('Train AUC:', train_auc)
print('Train F1 Score:', train_f1_score)
print('Train Accuracy:', train_accuracy)
```
阅读全文