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

2024-04-27 05:04:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集,它缺少一些我想预测的Y值。因此,为了首先创建一个模型,我放弃了Na,使用下面的代码->;RBall.dropna公司(subset=['nexthpr'],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)

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

现在,我想用我的模型回去预测缺失的Na,我知道这是一个基本的问题,但这是我第一天使用python。非常感谢。你知道吗


Tags: the数据模型importtruemodellmlinear
1条回答
网友
1楼 · 发布于 2024-04-27 05:04:32

我会用NumPy找到nan的索引,然后调用predict。你知道吗

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])

相关问题 更多 >