实现功能选择

时间:2017-04-18 18:11:48

标签: python machine-learning scikit-learn anomaly-detection

尝试使用所选数据集进行预测时遇到的一个问题是,一旦选择了某些功能,如果您要对测试数据集进行预测,则测试数据集功能将无法对齐,因为培训由于特征选择,数据集将具有较少的特征。如何正确实现功能选择,使测试数据集具有与训练数据集相同的功能?

示例:

 from sklearn.datasets import load_iris
 from sklearn.feature_selection import SelectKBest
 from sklearn.feature_selection import chi2
 iris = load_iris()
 X, y = iris.data, iris.target
 X.shape
(150, 4)
 X_new = SelectKBest(chi2, k=2).fit_transform(X, y)
 X_new.shape
(150, 2)

2 个答案:

答案 0 :(得分:2)

你必须<template> <div id="app"> <work-zone></work-zone> </div> </template> <script> import WorkZone from './components/WorkZone.vue'; export default { name: 'app', components: { 'work-zone': WorkZone }, data() { return {} }, methods: {} }; </script> 你的测试集......并且使用var WorkZone = Vue.extend({ template: ` <div id="work-zone"> <div id="wrapper"></div> </div>`, data() { return { tabItems: [{ title: 'Главная страница', project: 'Текст главная страница', done: false, }], }; }, methods: { createTab: function(newTab) { this.tabItems.push(newTab); } } }); ,但只是transform。这需要您保存fit_transform对象,以便实现以下目的:

transform

答案 1 :(得分:0)

我相信你想通过首先匹配SelectKBest然后transform你的测试数据来创建一个feature_selector对象。像这样:

feature_selector = SelectKBest(chi2, k=2).fit(X_train, y)
X_train_pruned = feature_selector.transform(X_train)
X_test_pruned = feature_selector.transform(X_test)