缺失值填充

# 读取“银行贷款审批数据.xlsx”表,自变量为x1~x15,决策变量为y(1-同意贷款,0-不同意贷款)

# 其中x1~x6为数值变量,x7~x15为名义变量

# 请对x1~x6中存在的缺失值用均值策略填充,x7~x15用最频繁值策略填充

# 最后返回填充处理后的X(即x1~x15),以及决策变量Y(即y)

def return_values():

    import pandas as pd

    import numpy as np

    from sklearn.impute import SimpleImputer

    data=pd.read_excel('银行贷款审批数据.xlsx')

    numerical_vars = data.iloc[:, 0:6]  # x1 到 x6

    categorical_vars = data.iloc[:, 6:]  # x7 到 x15

    # 对数值变量(x1 - x6)使用均值策略填充缺失值

    numeric_imputer = SimpleImputer(strategy='mean')

    numeric_imputer.fit(numerical_vars)

    a1 = numeric_imputer.transform(numerical_vars)

    # 对名义变量(x7 - x15)使用最频繁值策略填充缺失值

    categorical_imputer = SimpleImputer(strategy='most_frequent')

    categorical_imputer.fit(categorical_vars)

    a2 = categorical_imputer.transform(categorical_vars)

    X=np.hstack((a1,a2))

    Y=data.iloc[:,-1]

    return(X,Y)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值