GralL Asked:2020-04-27 01:17:04 +0000 UTC2020-04-27 01:17:04 +0000 UTC 2020-04-27 01:17:04 +0000 UTC 测试和训练集 772 如何设置矩阵Y_train和Y_test?以及如何摆脱NaN? churn_result = churn_df['TRG_is_churn'] churn_result y = np.where(churn_result == 'True.',1,0) 给出错误消息: TypeError:无效的类型比较 python 1 个回答 Voted Best Answer MaxU - stop genocide of UA 2020-04-30T22:40:51Z2020-04-30T22:40:51Z 具有以下数据框: In [71]: df Out[71]: TRG_is_churn 0 True. 1 False. 2 NaN 3 Nonsense 4 NaN 5 None 6 NaN 7 NaN 8 True. In [72]: df['TRG_is_churn'].isna() Out[72]: 0 False 1 False 2 True 3 False 4 True 5 False 6 True 7 True 8 False Name: TRG_is_churn, dtype: bool 比较'True.'将返回一个布尔系列(系列) In [73]: df['TRG_is_churn'] == 'True.' Out[73]: 0 True 1 False 2 False 3 False 4 False 5 False 6 False 7 False 8 True Name: TRG_is_churn, dtype: bool 可以转换为整数: In [74]: y = (df['TRG_is_churn'] == 'True.').astype(np.int8) In [75]: y Out[75]: 0 1 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 1 Name: TRG_is_churn, dtype: int8 同样以 Numpy 数组的形式出现: In [76]: y = (df['TRG_is_churn'] == 'True.').astype(np.int8).values In [77]: y Out[77]: array([1, 0, 0, 0, 0, 0, 0, 0, 1], dtype=int8) PS 在使用 Pandas/Numpy/Scipy/sklearn/etc 时尽量避免循环。
具有以下数据框:
比较
'True.'将返回一个布尔系列(系列)可以转换为整数:
同样以 Numpy 数组的形式出现:
PS 在使用 Pandas/Numpy/Scipy/sklearn/etc 时尽量避免循环。