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

LinearRegression模型更改random_state后出现KeyError 0问题

问题:LinearRegression模型修改random_state后触发KeyError 0错误

我编写的房价预测LinearRegression模型代码和教程视频内容完全一致;当设置random_state=42时无报错,但将random_state改为任意其他数值时就会触发KeyError 0错误。

代码示例

from sklearn.model_selection import train_test_split  
X = data.drop('SalesPrice', axis = 1)
y = data['SalesPrice']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(X_train, y_train)

predictions = lr.predict(X_test)

print("Actual value of the house: ", y_test[0])
print("Model prediction value: ", predictions[0])

错误信息

KeyError                                  Traceback (most recent call last)
File C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3653, in Index.get_loc(self, key)
   3652 try:
-> 3653     return self._engine.get_loc(casted_key)
   3654 except KeyError as err:

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\_libs\index.pyx:147, in pandas._libs.index.IndexEngine.get_loc()

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\_libs\index.pyx:176, in pandas._libs.index.IndexEngine.get_loc()

File pandas\_libs\hashtable_class_helper.pxi:2606, in pandas._libs.hashtable.Int64HashTable.get_item()

File pandas\_libs\hashtable_class_helper.pxi:2630, in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Cell In[66], line 3
      1 predictions = lr.predict(X_test)
----> 3 print("Actual value of the house: ", y_test[0])
      4 print("Model prediction value: ", predictions[0])

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\series.py:1007, in Series.__getitem__(self, key)
   1004     return self._values[key]
   1006 elif key_is_scalar:
-> 1007     return self._get_value(key)
   1009 if is_hashable(key):
   1010     # Otherwise index.get_value will raise InvalidIndexError
   1011     try:
   1012         # For labels that don't resolve as scalars like tuples and frozensets

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\series.py:1116, in Series._get_value(self, label, takeable)
   1113     return self._values[label]
   1115 # Similar to Index.get_value, but we do not fall back to positional
-> 1116 loc = self.index.get_loc(label)
   1118 if is_integer(loc):
   1119     return self._values[loc]

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3655, in Index.get_loc(self, key)
   3653     return self._engine.get_loc(casted_key)
   3654 except KeyError as err:
-> 3655     raise KeyError(key) from err
   3656 except TypeError:
   3657     # If we have a listlike key, _check_indexing_error will raise
   3658     #  InvalidIndexError. Otherwise we fall through and re-raise
   3659     #  the TypeError.
   3660     self._check_indexing_error(key)

KeyError: 0

问题原因

y_test是pandas Series对象,y_test[0]是按标签索引取值,而非位置索引。当random_state=42时,测试集刚好包含标签为0的样本;修改random_state后,测试集的样本标签不再包含0,因此触发KeyError。

解决方案

有两种可靠的解决方式:

方式1:使用位置索引取值

用iloc方法专门按位置取元素,不受原标签影响:

print("Actual value of the house: ", y_test.iloc[0])
print("Model prediction value: ", predictions[0])

方式2:重置测试集索引

分割数据集后重置y_test的索引,让标签从0开始连续排列:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)
y_test = y_test.reset_index(drop=True)  # 重置索引并丢弃原索引

# 之后可以直接用y_test[0]取值
print("Actual value of the house: ", y_test[0])
print("Model prediction value: ", predictions[0])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 11:05:18