如何使用线性回归预测 python 中的 Na

how to predict Na's in python using linear regression

我有一个数据集缺少一些我想预测的 Y 值。因此,我放弃了 Na,以便首先创建一个模型,使用此代码 -> RBall.dropna(subset=['NextHPPR'], inplace = True

import statsmodels.api as sm 
from sklearn import linear_model

RBall.dropna(subset=['NextHPPR'], inplace = True)

X = RBall[['ReceivingTargets_x','SnapsPlayedPercentage','RushingAttempts_x', 'RushingAttempts_y']]

Y = RBall['NextHPPR']

lm = linear_model.LinearRegression()
model = lm.fit(X,Y)

这是删除 NA 之前我的数据的屏幕截图。 Note the NA's in NextHPPR, my Y variable in the regression

现在,我想使用我的模型返回并预测缺失的 Na。我知道这是一个初级问题,但这是我第一天使用 python。谢谢。

我会使用 NumPy 查找 NaN 的索引,然后调用预测。

import numpy as np 

X = np.array([432, 234442, 43, 423, 2342, 3434])
Y = np.array([342, np.NaN, 23, 545, np.NaN, 23])

nan_idx = np.argwhere(np.isnan(Y)).flatten()

print(X[nan_idx])
>>>[234442   2342]

predict_NaNs = lm.predict(X[nan_idx])