gpt4 book ai didi

python - 如何使用线性回归在Python中预测Na

转载 作者:太空宇宙 更新时间:2023-11-03 21:43:19 24 4
gpt4 key购买 nike

我有一个数据集缺少一些我想要预测的 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])

关于python - 如何使用线性回归在Python中预测Na,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52682545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com