基于 pandas 回归的缺失值插补

发布于 2025-01-13 20:47:57 字数 942 浏览 3 评论 0原文

我想根据多元插补输入缺失数据,在下面所附的数据集中,A列有一些缺失值,A列和B列的相关系数为0.70。因此,我想使用回归类型的关系,以便构建 A 列和 B 列之间的关系,并在 Python 中估算缺失值。

注意:我可以使用平均值、中位数和众数来完成此操作,但我想使用另一列中的关系来填充缺失值。

遇到问题如何处理。请提供您的解决方案

import pandas as pd
from sklearn.preprocessing import Imputer
import numpy as np
  

    # assign data of lists.  
    data = {'Date': ['9/19/14', '9/20/14', '9/21/14', '9/21/14','9/19/14', '9/20/14', '9/21/14', '9/21/14','9/19/14', '9/20/14', '9/21/14', '9/21/14', '9/21/14'], 
            'A': [77.13, 39.58, 33.70, np.nan, np.nan,39.66, 64.625, 80.04, np.nan ,np.nan ,19.43, 54.375, 38.41],
            'B': [19.5, 21.61, 22.25, 25.05, 24.20, 23.55, 5.70, 2.675, 2.05,4.06, -0.80, 0.45, -0.90],
            'C':['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c']}  
      
    # Create DataFrame  
    df = pd.DataFrame(data)  
    df["Date"]= pd.to_datetime(df["Date"]) 
    # Print the output.  
    print(df) 

i want to inpute the missing data based on multivariate imputation, in the below-attached data sets, column A has some missing values, and Column A and Column B have the correlation factor of 0.70. So I want to use a regression kind of realationship so that it will build the relation between Column A and Column B and impute the missing values in Python.

N.B.: I can do it using Mean, median, and mode, but I want to use the relationship from another column to fill the missing value.

How to deal the problem. your solution, please

import pandas as pd
from sklearn.preprocessing import Imputer
import numpy as np
  

    # assign data of lists.  
    data = {'Date': ['9/19/14', '9/20/14', '9/21/14', '9/21/14','9/19/14', '9/20/14', '9/21/14', '9/21/14','9/19/14', '9/20/14', '9/21/14', '9/21/14', '9/21/14'], 
            'A': [77.13, 39.58, 33.70, np.nan, np.nan,39.66, 64.625, 80.04, np.nan ,np.nan ,19.43, 54.375, 38.41],
            'B': [19.5, 21.61, 22.25, 25.05, 24.20, 23.55, 5.70, 2.675, 2.05,4.06, -0.80, 0.45, -0.90],
            'C':['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c']}  
      
    # Create DataFrame  
    df = pd.DataFrame(data)  
    df["Date"]= pd.to_datetime(df["Date"]) 
    # Print the output.  
    print(df) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书信已泛黄 2025-01-20 20:47:57

使用:

dfreg = df[df['A'].notna()]
dfimp = df[df['A'].isna()]

from sklearn.neural_network import MLPRegressor    
regr = MLPRegressor(random_state=1, max_iter=200).fit(dfreg['B'].values.reshape(-1, 1), dfreg['A'])
regr.score(dfreg['B'].values.reshape(-1, 1), dfreg['A'])

regr.predict(dfimp['B'].values.reshape(-1, 1))

请注意,在提供的数据中,A 列和 B 列的相关性非常低(小于 0.05)。
用空单元格替换估算值:

s = df[df['A'].isna()]['A'].index
df.loc[s, 'A'] = regr.score(dfreg['B'].values.reshape(-1, 1), dfreg['A'])

输出:

在此处输入图像描述

Use:

dfreg = df[df['A'].notna()]
dfimp = df[df['A'].isna()]

from sklearn.neural_network import MLPRegressor    
regr = MLPRegressor(random_state=1, max_iter=200).fit(dfreg['B'].values.reshape(-1, 1), dfreg['A'])
regr.score(dfreg['B'].values.reshape(-1, 1), dfreg['A'])

regr.predict(dfimp['B'].values.reshape(-1, 1))

Note that in the provided data correlation of the A and B columns are very low (less than .05).
For replacing the imputed values with empty cells:

s = df[df['A'].isna()]['A'].index
df.loc[s, 'A'] = regr.score(dfreg['B'].values.reshape(-1, 1), dfreg['A'])

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文