【问题标题】:how to predict Na's in python using linear regression如何使用线性回归在python中预测Na
【发布时间】:2019-03-11 23:03:07
【问题描述】:

我有一个数据集缺少一些我想预测的 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 的第一天。谢谢。

【问题讨论】:

  • 如果“missing N/A”用 1 表示,而“not missing N/A”为零,你晚上可以尝试在单独的回归中使用它。

标签: python anaconda regression sklearn-pandas


【解决方案1】:

我会使用 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])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 2016-08-26
    • 1970-01-01
    • 2021-07-22
    • 2019-02-01
    • 2017-03-06
    • 1970-01-01
    相关资源
    最近更新 更多