【发布时间】:2021-12-29 14:26:11
【问题描述】:
我正在研究 LinearRegression 模型来填充特征 Rupeepersqft 的空值。当我运行代码时,我收到此错误:
IndexError Traceback (most recent call last)
<ipython-input-20-33d4e6d2998e> in <module>()
1 test_data = data_with_null.iloc[:,:3]
----> 2 Rupeepersqft_predicted['Rupeepersqft'] = pd.DataFrame(linreg.predict(test_data))
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
这是给我错误的代码:
from sklearn.linear_model import LinearRegression
linreg = LinearRegression()
data_with_null = data2[['Price (Lakhs)','Area','Area Type','Rupeepersqft','Condition','Purchase Type','Real Estate Regulation Act']].dropna()
data_without_null = data_with_null.dropna()
train_data_x = data_without_null.iloc[:,:3]
train_data_y = data_without_null.iloc[:,3]
linreg.fit(train_data_x, train_data_y)
test_data = data_with_null.iloc[:,:3]
Rupeepersqft_predicted['Rupeepersqft'] = pd.DataFrame(linreg.predict(test_data))
data_with_null.Rupeepersqft.fillna(Rupeepersqft_predicted, inplace=True)
这是数据的样子:
谁能帮我解决这个问题?
【问题讨论】:
-
Rupeepersqft_predicted['Rupeepersqft']如果从数据框中选择一列(来自dict的值),则可以。但显然它是一个数组,不接受字符串索引(除非它是一个结构化数组)。
标签: python pandas scikit-learn linear-regression index-error