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

LinearRegression交叉验证代码报错求助:AttributeError与KeyError问题

问题修复方案与CV代码验证

核心错误根源

  1. AttributeError: 'numpy.ndarray' object has no attribute 'iloc':iloc是pandas DataFrame/Series专属方法,你在代码中对numpy数组调用了该方法。LinearRegression的输入输出处理若返回numpy数组,就会和依赖pandas结构的切片逻辑冲突——而DecisionTreeRegressor的运行流程恰好没触发这种格式混用问题。
  2. KeyError:所选索引不在列中:数据格式转换时丢失了列名,或是误将行索引当作列索引使用,导致代码尝试访问不存在的列。

针对性修复步骤

1. 统一数据格式,避免numpy与pandas混用

全程保留pandas结构传递数据集,仅在模型拟合时显式转换为numpy数组:

# 错误示例:转成numpy数组后用iloc
X_train_np = X_train.values
X_train_np.iloc[0]  # 触发AttributeError

# 正确做法:保留DataFrame,拟合时再转numpy
from sklearn.linear_model import LinearRegression
lr_model = LinearRegression()
lr_model.fit(X_train.values, y_train.values)

# 交叉验证直接传入DataFrame
from sklearn.model_selection import cross_val_score
lr_scores = cross_val_score(lr_model, X_train, y_train, cv=5)

2. 修复索引与列名问题

如果数据集已转为numpy数组,重新转回DataFrame并恢复原列名:

import pandas as pd
# 假设original_feature_names是原始特征列名列表
X_train_df = pd.DataFrame(X_train_np, columns=original_feature_names)

手动CV循环时,确保用行索引切片:

from sklearn.model_selection import KFold
kf = KFold(n_splits=5, shuffle=True, random_state=42)
for train_idx, val_idx in kf.split(X_train_df):
    # 用iloc取行,而非列
    X_cv_train = X_train_df.iloc[train_idx]
    X_cv_val = X_train_df.iloc[val_idx]
    y_cv_train = y_train.iloc[train_idx]
    y_cv_val = y_train.iloc[val_idx]
    lr_model.fit(X_cv_train, y_cv_train)

3. CV代码正确性验证

对齐LinearRegression与DecisionTreeRegressor的运行逻辑,确保:

  • 输入的X/y数据结构完全一致(同是DataFrame或同是numpy数组)
  • 交叉验证的划分规则相同(如shuffle、random_state参数一致)
  • 评估指标统一(默认均为R²,避免指定不兼容指标)

完整验证代码示例:

import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import cross_val_score

# 假设X_train、y_train为pandas DataFrame/Series格式
# 线性回归交叉验证
lr = LinearRegression()
lr_cv_scores = cross_val_score(lr, X_train, y_train, cv=5, scoring='r2')
print(f"线性回归CV R²分数: {lr_cv_scores}")
print(f"平均R²: {lr_cv_scores.mean():.4f}")

# 决策树交叉验证(对比基准)
dt = DecisionTreeRegressor(random_state=42)
dt_cv_scores = cross_val_score(dt, X_train, y_train, cv=5, scoring='r2')
print(f"决策树CV R²分数: {dt_cv_scores}")
print(f"平均R²: {dt_cv_scores.mean():.4f}")

额外坑点排查

  • 交叉验证时,标准化等预处理操作要在每个折内单独拟合,避免数据泄露
  • 确保y是Series格式,转numpy数组后会丢失索引信息,易引发切片错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 00:18:24