gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:35:07 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com