递归特征消除后无法进行预测

时间:2019-05-30 21:25:13

标签: python machine-learning scikit-learn

我已经对数据集进行了递归特征消除,现在尝试根据我的RFE返回的特征进行预测,但仍然遇到此错误:

ValueError: X has 31 features per sample; expecting 9

这是我为获得最佳功能并根据返回的功能转换数据而编写的代码

no_list = np.arange(1,len(list(dat)))
acc_score = 0
n_features = 0
score_list = []
for x in range(len(no_list)):
    X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2,random_state=100)
    log_reg = LogisticRegression()
    rfe = RFE(log_reg,no_list[x])
    X_train_rfe = rfe.fit_transform(X_train,y_train)
    X_test_rfe = rfe.transform(X_test)
    log_reg.fit(X_train_rfe,y_train)
    score = log_reg.score(X_test_rfe,y_test)
    score_list.append(score)
    if(score > acc_score):
        acc_score = score
        n_features = no_list[x]
rfe = RFE(log_reg,n_features)
rfe.fit_transform(X_train,y_train)
predictions = rfe.predict(X_test)

1 个答案:

答案 0 :(得分:0)

先将X_test转换成预测值。您的rfe使用仅采用log_reg的{​​{1}}模型。

n_features