python - SelectKBest 与 GaussianNB 结果不精确/一致

标签 python machine-learning scikit-learn feature-extraction feature-selection

我想使用 SelectKBest 选择前 K 个特征并运行 GaussianNB:

selection = SelectKBest(mutual_info_classif, k=300)

data_transformed = selection.fit_transform(data, labels)
new_data_transformed = selection.transform(new_data)

classifier = GaussianNB()
classifier.fit(data_transformed, labels)
y_predicted = classifier.predict(new_data)
acc = accuracy_score(new_data_labels, y_predicted)

但是,对于相同数据,我没有得到一致的准确性结果。 准确度为:

0.61063743402354853
0.60678034916768164 
0.61733658140479086 
0.61652456354039786 
0.64778725131952908 
0.58384084449857898

对于相同的数据。我不进行分割等操作。我只使用两个静态的 datanew_data 集。

为什么结果会有所不同?如何确保相同的数据获得相同的准确性?

最佳答案

这是因为数据或变量具有一定的随机性。这取决于估计器或函数内部使用的随机数生成器,在您的情况下它是 mutual_info_classif您将其传递到 SelectKBest 中。

看看random_state的用法here并在 this answer

作为解决方法,您可以在代码顶部插入以下行。

np.random.seed(some_integer)

这会将 numpy 的种子设置为 some_integer 并且据我所知,scikit 估计器使用 numpy 的随机数生成器。 See this for more details

关于python - SelectKBest 与 GaussianNB 结果不精确/一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42193893/

相关文章:

python - scikit-learn 线性回归索引误差

python - 禁止(未设置 CSRF cookie。)Django

python - 在字符串上使用 int() 函数后如何返回小数?

python - tensorflow : Feeding placeholder from variable

python - 如何在 scikit learn 中使用 GP.fit 进行多维输入?

machine-learning - 支持向量机 : Any sense using 2 opposite sign features?

python - 在 sklearn.preprocessing 模块中,我得到 ValueError : Found array with 0 feature(s)

python - 使用掩码和多重映射替换列值

python - 谷歌应用引擎模块 - 长时间运行的任务 > 10 分钟

Python scikit 学习(指标): difference between r2_score and explained_variance_score?