为了账号安全,请及时绑定邮箱和手机立即绑定

在python中输入二进制值

在python中输入二进制值

吃鸡游戏 2022-07-12 10:28:03
我有一个缺少值的数据框,其中可能的选项是 True 或 False,因为在 NaN 情况下,pandas 将该列作为浮点数,并且在输入该列并获取值之后:0、0.5 和 1如何添加约束以仅获得 0 和 1?目前我正在使用 missingpy 库from missingpy import MissForest
查看完整描述

2 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

你介意用一些你使用的数据的例子和给你问题的代码来更新你的问题 - 它会让你得到更好的答案!


从您的说法看来,适合的模型正在考虑您的目标变量是连续的而不是分类的(布尔值本质上是分类的 0 或 1)。MissForest 上的 API 文档说:


第一步涉及用初始猜测填充剩余的非候选列的任何缺失值,这是表示数值变量的列的列平均值和表示分类变量的列的列模式。请注意,分类变量需要在 imputer 的 fit() 方法调用期间明确标识(有关更多信息,请参阅 API)。


这意味着您应该cat_vars在拟合阶段指定:


fit(self, X, y=None, cat_vars=None):在 X 上拟合 imputer。


Parameters

----------

X : {array-like}, shape (n_samples, n_features)

    Input data, where ``n_samples`` is the number of samples and

    ``n_features`` is the number of features.


cat_vars : int or array of ints, optional (default = None)

    An int or an array containing column indices of categorical

    variable(s)/feature(s) present in the dataset X.

    ``None`` if there are no categorical variables in the dataset.


Returns

-------

self : object

    Returns self.

参考这里。


这意味着将使用类别而不是连续值进行估算。


查看完整回答
反对 回复 2022-07-12
?
慕妹3242003

TA贡献1824条经验 获得超6个赞

您有几种处理策略nan,让我们考虑一下这个玩具df:


import pandas as pd

import numpy as np



df = pd.DataFrame(

    {

        'column': [np.nan, True, np.nan]

    }

)

print(df['column'])


>>> 

0     NaN

1    True

2     NaN

Name: column, dtype: object

bool如果您负担得起使用损坏的数据(不建议),您可以简单地将列强制为一种类型:


print(df['column'].astype(bool))


>>> 

0    True

1    True

2    True

Name: column, dtype: bool

您可以删除包含nan(最佳方法)的行:


print(df['column'].dropna())


>>>

1    True

Name: column, dtype: object

或者您可以将它们替换nan为默认值:


print(df['column'].fillna(False))


>>>

0    False

1     True

2    False

Name: column, dtype: bool


查看完整回答
反对 回复 2022-07-12
  • 2 回答
  • 0 关注
  • 307 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号