You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LinearRegression报'normalize'参数无效:错误关联哪个组件?

问题分析:LinearRegression参数错误排查

问题背景

实现《Python机器学习入门》示例时,运行代码出现参数错误,相关代码与错误信息如下:

模型配置代码

def find_best_model_using_gridsearchcv(x,y):
  algos = {
      'linear regression' : {
          'model': LinearRegression(),
          'params':{ 
              'normalize':[True, False]
          }
      },
      'lasso': {
          'model': Lasso(),
          'params':{
              'alpha':[1,2],
              'selection': ['random','cyclic']
          }
      },
      'decision tree':{
          'model': DecisionTreeRegressor(),
          'params':{
              'criterion':['mse', 'friedman_mse'],
              'splitter':['best', 'random']
          }
      }
  }

网格搜索执行代码

scores = []
  cv = ShuffleSplit(n_splits=5, test_size=0.2, random_state=0)
  for algo_name, config in algos.items():
    gs = GridSearchCV(config['model'], config['params'], cv=cv, return_train_score=False)
    gs.fit(x_train, y_train)
    scores.append({
        'model': algo_name,
        'best_score': gs.best_score_,
        'best_params': gs.best_params_
    })

  return pd.DataFrame(scores, columns=['model', 'best_score', 'best_params'])

find_best_model_using_gridsearchcv(x,y)

错误信息

ValueError: Invalid parameter 'normalize' for estimator LinearRegression().
Valid parameters are: ['copy_X', 'fit_intercept', 'n_jobs', 'positive'].

错误归属

这个错误本质上与LinearRegression直接相关,GridSearchCV只是触发了参数校验的执行环节。

原因说明

新版scikit-learn已经移除了LinearRegression的normalize参数。该参数原本用于在拟合模型前自动对数据做归一化处理,但官方现在推荐通过单独的预处理模块(如StandardScaler)完成数据归一化,再将处理后的数据传入模型训练。代码中给LinearRegression配置了它不支持的normalize参数,导致GridSearchCV在尝试为模型设置该参数时抛出无效参数的错误。

修复方案

  • 方案一:移除LinearRegression的normalize参数配置,调整为模型支持的参数:
'linear regression' : {
    'model': LinearRegression(),
    'params':{ 
        'fit_intercept':[True, False]
    }
},
  • 方案二:如果需要归一化数据,将预处理步骤整合进模型管道:
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

'linear regression' : {
    'model': Pipeline([
        ('scaler', StandardScaler()),
        ('lr', LinearRegression())
    ]),
    'params':{ 
        'lr__fit_intercept':[True, False]
    }
},

内容的提问来源于stack exchange,提问作者sonal jain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 04:52:49