python - scikit-learn SelectPercentile TFIDF 数据特征缩减

标签 python text machine-learning scikit-learn

我正在使用 scikit-learn 中的各种机制来创建训练数据集和由文本特征组成的测试集的 tf-idf 表示。两个数据集都经过预处理以使用相同的词汇表,因此特征和特征数量相同。我可以根据训练数据创建模型并评估其在测试数据上的性能。我想知道如果我使用 SelectPercentile 来减少转换后训练集中的特征数量,如何识别测试集中的相同特征以用于预测?

trainDenseData = trainTransformedData.toarray()
testDenseData = testTransformedData.toarray()

if ( useFeatureReduction== True):
    reducedTrainData = SelectPercentile(f_regression,percentile=10).fit_transform(trainDenseData,trainYarray)

clf.fit(reducedTrainData, trainYarray)


# apply feature reduction to the test data

最佳答案

请参阅下面的代码和注释。

import numpy as np

from sklearn.datasets import make_classification
from sklearn import feature_selection

# Build a classification task using 3 informative features
X, y = make_classification(n_samples=1000,
                           n_features=10,
                           n_informative=3,
                           n_redundant=0,
                           n_repeated=0,
                           n_classes=2,
                           random_state=0,
                           shuffle=False)

sp = feature_selection.SelectPercentile(feature_selection.f_regression, percentile=30)
sp.fit_transform(X[:-1], y[:-1])  #here, training are the first 9 data vectors, and the last one is the test set
idx = np.arange(0, X.shape[1])  #create an index array
features_to_keep = idx[sp.get_support() == True]  #get index positions of kept features

x_fs = X[:,features_to_keep] #prune X data vectors
x_test_fs = x_fs[-1] #take your last data vector (the test set) pruned values
print x_test_fs #these are your pruned test set values 

关于python - scikit-learn SelectPercentile TFIDF 数据特征缩减,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29392754/

相关文章:

python - 未找到 TestLink xmlrpc API(通过 Python)404

python - 我需要在 Python 中生成 1000 个唯一的名字

java - 如何从文本文件中提取数字

python - Tensorflow 向量的形状为 (col,)

python - 什么 tensorflow 分布来表示分类数据列表

c++ - 支持 vector 机在 matlab 中有效,在 c++ 中无效

python - 用于在磁盘上加载字符串和文件的函数?

python - Pyramid 和 Cassandra 无法正常工作

c# - 如何在使用 C# 绘制图像时缩放文本

java - 从 BLOB 数据类型转义特殊字符