纠正模型:trainsets = pd.read_csv('/Users/zhangxinyu/Desktop/trainsets82.csv') testsets = pd.read_csv('/Users/zhangxinyu/Desktop/testsets82.csv') y_train_forced_turnover_nolimited = trainsets['m3_forced_turnover_nolimited'] X_train = trainsets.drop(['m3_P_perf_ind_all_1','m3_P_perf_ind_all_2','m3_P_perf_ind_all_3','m3_P_perf_ind_allind_1',\ 'm3_P_perf_ind_allind_2','m3_P_perf_ind_allind_3','m3_P_perf_ind_year_1','m3_P_perf_ind_year_2',\ 'm3_P_perf_ind_year_3','m3_forced_turnover_nolimited','m3_forced_turnover_3mon',\ 'm3_forced_turnover_6mon','m3_forced_turnover_1year','m3_forced_turnover_3year',\ 'm3_forced_turnover_5year','m3_forced_turnover_10year',\ 'CEOid','CEO_turnover_N','year','Firmid','appo_year'],axis=1) y_test_forced_turnover_nolimited = testsets['m3_forced_turnover_nolimited'] X_test = testsets.drop(['m3_P_perf_ind_all_1','m3_P_perf_ind_all_2','m3_P_perf_ind_all_3','m3_P_perf_ind_allind_1',\ 'm3_P_perf_ind_allind_2','m3_P_perf_ind_allind_3','m3_P_perf_ind_year_1','m3_P_perf_ind_year_2',\ 'm3_P_perf_ind_year_3','m3_forced_turnover_nolimited','m3_forced_turnover_3mon',\ 'm3_forced_turnover_6mon','m3_forced_turnover_1year','m3_forced_turnover_3year',\ 'm3_forced_turnover_5year','m3_forced_turnover_10year',\ 'CEOid','CEO_turnover_N','year','Firmid','appo_year'],axis=1) # 构建模型 model = Sequential() model.add(Dense(64, input_dim=X_train.shape[1], activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam') # 训练模型 model.fit(X_train, y_train, epochs=50, batch_size=32, verbose=0) # 预测测试集 y_pred = model.predict(X_test) # 计算AUC auc = roc_auc_score(y_test, y_pred) print('AUC:', auc)
时间: 2024-02-24 16:59:32 浏览: 95
在这段代码中,y_train_forced_turnover_nolimited和y_test_forced_turnover_nolimited是用于训练和测试的目标变量。然而,在训练模型时,使用了y_train作为目标变量,而没有使用正确的y_train_forced_turnover_nolimited。同样,在预测测试集时,使用了y_pred而没有使用y_test_forced_turnover_nolimited。
以下是修正后的代码:
```
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
trainsets = pd.read_csv('/Users/zhangxinyu/Desktop/trainsets82.csv')
testsets = pd.read_csv('/Users/zhangxinyu/Desktop/testsets82.csv')
y_train_forced_turnover_nolimited = trainsets['m3_forced_turnover_nolimited']
X_train = trainsets.drop(['m3_P_perf_ind_all_1','m3_P_perf_ind_all_2','m3_P_perf_ind_all_3','m3_P_perf_ind_allind_1',
'm3_P_perf_ind_allind_2','m3_P_perf_ind_allind_3','m3_P_perf_ind_year_1','m3_P_perf_ind_year_2',
'm3_P_perf_ind_year_3','m3_forced_turnover_nolimited','m3_forced_turnover_3mon',
'm3_forced_turnover_6mon','m3_forced_turnover_1year','m3_forced_turnover_3year',
'm3_forced_turnover_5year','m3_forced_turnover_10year','CEOid','CEO_turnover_N','year',
'Firmid','appo_year'], axis=1)
y_test_forced_turnover_nolimited = testsets['m3_forced_turnover_nolimited']
X_test = testsets.drop(['m3_P_perf_ind_all_1','m3_P_perf_ind_all_2','m3_P_perf_ind_all_3','m3_P_perf_ind_allind_1',
'm3_P_perf_ind_allind_2','m3_P_perf_ind_allind_3','m3_P_perf_ind_year_1','m3_P_perf_ind_year_2',
'm3_P_perf_ind_year_3','m3_forced_turnover_nolimited','m3_forced_turnover_3mon',
'm3_forced_turnover_6mon','m3_forced_turnover_1year','m3_forced_turnover_3year',
'm3_forced_turnover_5year','m3_forced_turnover_10year','CEOid','CEO_turnover_N','year',
'Firmid','appo_year'], axis=1)
# 构建模型
model = Sequential()
model.add(Dense(64, input_dim=X_train.shape[1], activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')
# 训练模型
model.fit(X_train, y_train_forced_turnover_nolimited, epochs=50, batch_size=32, verbose=0)
# 预测测试集
y_pred = model.predict(X_test)
# 计算AUC
auc = roc_auc_score(y_test_forced_turnover_nolimited, y_pred)
print('AUC:', auc)
```
在修正后的代码中,我们使用了正确的目标变量y_train_forced_turnover_nolimited进行训练,并且在计算AUC时使用了正确的目标变量y_test_forced_turnover_nolimited。
阅读全文