选择K个最佳功能

时间:2020-03-13 14:59:58

标签: python feature-selection

我试图从大型数据框中找出最佳功能。我可以获取压缩的数据帧值,但无法获取所选功能的名称。 下面是我的代码:

print('Shape of the bigramdf before feature selection:',bigram_df.shape)
if not os.path.isfile('smalldata/bigram_feather_top_100.feather'):
    SelectKBest(score_func=chi2,k=100).fit(bigram_df.iloc[:,:-1],df['class'])
    cols=SelectKBest.get_support(indices=False) # I am getting error here
    selc_k_best_byte_bigram=bigram_df[:,cols]
    selc_k_best_byte_bigram['id']=bigram_df['id']
    selc_k_best_byte_bigram.to_feather('smalldata/bigram_feather_top_100.feather')

    print('Shape of the bigramdf before feature selection:',selc_k_best_byte_bigram.shape)
else:
    selc_k_best_byte_bigram=pd.read_feather('smalldata/bigram_feather_top_100.feather')

我遇到以下错误:

TypeError: get_support() missing 1 required positional argument: 'self'

有人可以帮助我找到为什么我收到此TypeError

1 个答案:

答案 0 :(得分:1)

我认为您需要在变量中初始化类,然后调用.get_support。因此,请尝试替换:

SelectKBest(score_func=chi2,k=100).fit(bigram_df.iloc[:,:-1],df['class'])

k_best = SelectKBest(score_func=chi2,k=100).fit(bigram_df.iloc[:,:-1],df['class'])
cols = k_best.get_support(indices=False)