使用 chi2 的 SelectKBest() 的 K 值

如何解决使用 chi2 的 SelectKBest() 的 K 值

我在这里使用了稍微修改过的代码:Ensemble Methods: Tuning a XGBoost model with Scikit-Learn

当我执行它时,我不断收到此错误:

ValueError: k should be >=0,<= n_features = 4; got 10. Use k='all' to return all features.

我有四个功能和一个目标。我已经在 SeleckKBest() 函数的 Pipeline 参数中的下面代码中为 k 尝试了值 1-4,但同样的错误仍然存​​在。

这是我的可重现代码:

import pandas as pd
df = pd.DataFrame({'Number1': [11,12,13,14,15,16,17,18,19,20],'Color1': ['Red','Blue','Green','Yellow','Orange','Red','Orange'],'Number2': [221,222,223,224,225,226,227,228,229,230],'Trait1': ['Jogger','Sedentary','Tennis_Player','Graveyard','Shift_Worker','Jogger','Fulltime_Mom','Couch_Potato','Graveyard_Shift_Worder'],'Target': ['yes','no','yes','no']})

col = pd.Categorical(df['Target'])
df['Target'] = col.codes

from sklearn.base import BaseEstimator,TransformerMixin
from sklearn.preprocessing import MinMaxScaler
class PreprocessTransformer(BaseEstimator,TransformerMixin):
        def __init__(self,cat_features,num_features):
        self.cat_features = cat_features
        self.num_features = num_features
    def fit(self,X,y=None):
        return self
    def transform(self,y=None):
        df = X.copy()
        # Convert columns to categorical
        for name in self.cat_features:
            col = pd.Categorical(df[name])
            df[name] = col.codes
        # Normalize numerical features
        scaler = MinMaxScaler()
        df[self.num_features] = scaler.fit_transform(df[self.num_features])
        return df

from sklearn.model_selection import train_test_split
# Split the dataset into training and testing
X_train,X_test,y_train,y_test = train_test_split(
   df.drop('Target',axis=1),df['Target'],test_size=0.2,random_state=42,shuffle=True,stratify=df['Target']
)               

from sklearn.pipeline import Pipeline
from sklearn.feature_selection import SelectKBest,chi2
import xgboost as xgb
# Get columns list for categorical and numerical
categorical_features = df.select_dtypes('object').columns.tolist()
numerical_features = df.select_dtypes('int64').columns.tolist()
# Create a pipeline
pipe = Pipeline([
  ('preproc',PreprocessTransformer(categorical_features,numerical_features)),('fs',SelectKBest(k=0)),('clf',xgb.XGBClassifier(objective='binary:logistic'))
])

from sklearn.model_selection import KFold,GridSearchCV
from sklearn.metrics import accuracy_score,make_scorer
# Define our search space for grid search
search_space = [
  {
    'clf__n_estimators': [50,100,150,200],'clf__learning_rate': [0.01,0.1,0.2,0.3],'clf__max_depth': range(3,10),'clf__colsample_bytree': [i/10.0 for i in range(1,3)],'clf__gamma': [i/10.0 for i in range(3)],'fs__score_func': [chi2],'fs__k': [10],}
]
# Define cross validation
kfold = KFold(n_splits=8,random_state=42)
# AUC and accuracy as score
scoring = {'AUC':'roc_auc','Accuracy':make_scorer(accuracy_score)}
# Define grid search
grid = GridSearchCV(
  pipe,param_grid=search_space,cv=kfold,scoring=scoring,refit='AUC',verbose=1,n_jobs=-1
)
# Fit grid search
model = grid.fit(X_train,y_train)

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-84-5c30ee0bb39f> in <module>
     28 )
     29 # Fit grid search
---> 30 model = grid.fit(X_train,y_train)

~/anaconda3/envs/python3/lib/python3.6/site-packages/sklearn/model_selection/_search.py in fit(self,y,groups,**fit_params)
    737             refit_start_time = time.time()
    738             if y is not None:
--> 739                 self.best_estimator_.fit(X,**fit_params)
    740             else:
    741                 self.best_estimator_.fit(X,**fit_params)

~/anaconda3/envs/python3/lib/python3.6/site-packages/sklearn/pipeline.py in fit(self,**fit_params)
    348             This estimator
    349         """
--> 350         Xt,fit_params = self._fit(X,**fit_params)
    351         with _print_elapsed_time('Pipeline',352                                  self._log_message(len(self.steps) - 1)):

~/anaconda3/envs/python3/lib/python3.6/site-packages/sklearn/pipeline.py in _fit(self,**fit_params)
    313                 message_clsname='Pipeline',314                 message=self._log_message(step_idx),--> 315                 **fit_params_steps[name])
    316             # Replace the transformer of the step with the fitted
    317             # transformer. This is necessary when loading the transformer

~/anaconda3/envs/python3/lib/python3.6/site-packages/joblib/memory.py in __call__(self,*args,**kwargs)
    353 
    354     def __call__(self,**kwargs):
--> 355         return self.func(*args,**kwargs)
    356 
    357     def call_and_shelve(self,**kwargs):

~/anaconda3/envs/python3/lib/python3.6/site-packages/sklearn/pipeline.py in _fit_transform_one(transformer,weight,message_clsname,message,**fit_params)
    726     with _print_elapsed_time(message_clsname,message):
    727         if hasattr(transformer,'fit_transform'):
--> 728             res = transformer.fit_transform(X,**fit_params)
    729         else:
    730             res = transformer.fit(X,**fit_params).transform(X)

~/anaconda3/envs/python3/lib/python3.6/site-packages/sklearn/base.py in fit_transform(self,**fit_params)
    572         else:
    573             # fit method of arity 2 (supervised transformation)
--> 574             return self.fit(X,**fit_params).transform(X)
    575 
    576 

~/anaconda3/envs/python3/lib/python3.6/site-packages/sklearn/feature_selection/_univariate_selection.py in fit(self,y)
    346                             % (self.score_func,type(self.score_func)))
    347 
--> 348         self._check_params(X,y)
    349         score_func_ret = self.score_func(X,y)
    350         if isinstance(score_func_ret,(list,tuple)):

~/anaconda3/envs/python3/lib/python3.6/site-packages/sklearn/feature_selection/_univariate_selection.py in _check_params(self,y)
    512             raise ValueError("k should be >=0,<= n_features = %d; got %r. "
    513                              "Use k='all' to return all features."
--> 514                              % (X.shape[1],self.k))
    515 
    516     def _get_support_mask(self):

ValueError: k should be >=0,<= n_features = 4; got 10. Use k='all' to return all features.

解决方法

有 4 个特征(Number1Color1Number2Trait1)。

SelectKBest 会从原始集合中选出 K 个最能解释的特征,所以 K 应该是一个大于 0 且小于或等于总数的值功能。

您在此行中将 GridSearch 对象设置为始终使用 10

'fs__k': [10]

在声明期间覆盖您的定义

('fs',SelectKBest(k=0)),

您可以删除 fs__k 行并将声明行更正为您想要的 k,或者在 k 定义中设置您想要的 search_grid

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-