【发布时间】:2020-07-07 17:15:36
【问题描述】:
关于这个:
NLP in Python: Obtain word names from SelectKBest after vectorizing
我找到了这段代码:
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_selection import chi2
THRESHOLD_CHI = 5 # or whatever you like. You may try with
# for threshold_chi in [1,2,3,4,5,6,7,8,9,10] if you prefer
# and measure the f1 scores
X = df['text']
y = df['labels']
cv = CountVectorizer()
cv_sparse_matrix = cv.fit_transform(X)
cv_dense_matrix = cv_sparse_matrix.todense()
chi2_stat, pval = chi2(cv_dense_matrix, y)
chi2_reshaped = chi2_stat.reshape(1,-1)
which_ones_to_keep = chi2_reshaped > THRESHOLD_CHI
which_ones_to_keep = np.repeat(which_ones_to_keep ,axis=0,repeats=which_ones_to_keep.shape[1])
此代码计算卡方检验,并应将最佳特征保持在所选阈值内。 我的问题是如何选择卡方测试分数的阈值?
【问题讨论】:
标签: python scikit-learn text-classification tf-idf feature-selection