Python线性回归模型处理缺失值NaN的解决方法
该错误提示是因为数据中存在缺失值NaN,而LinearRegression模型不接受NaN作为输入。解决方法有两种:
- 删除含有缺失值的样本:可以使用dropna()方法删除含有NaN的行或列。
data.dropna(inplace=True)
- 使用Imputer进行缺失值填充:可以使用sklearn.preprocessing.Imputer类来对缺失值进行填充。
from sklearn.preprocessing import Imputer
# 创建Imputer对象,指定填充策略为均值
imputer = Imputer(strategy='mean')
# 对含有缺失值的列进行填充
X = imputer.fit_transform(X)
两种方法选其一即可。
原文地址: https://www.cveoy.top/t/topic/fzNT 著作权归作者所有。请勿转载和采集!