简体   繁体   English

对最初是熊猫数据帧的numpy数组进行逻辑运算

[英]logical operation on numpy array that was originally a pandas data frame

I have two variables that i want to perform on them elementwise logical operations. 我有两个要对它们执行元素逻辑操作的变量。 however I get the following error: 但是我得到以下错误:

    tp = sum(actual & predicted)
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Below is my code: 下面是我的代码:

import pandas as pd
import numpy as np

train = 'train.tsv'
submission = 'submission1234.csv' 

trainSearchStream = pd.read_csv(train,sep='\t')

sample = pd.read_csv(path + 'sampleSubmission.csv')
preds = np.array(pd.read_csv(submission, header = None))
index = sample.ID.values - 1
sample['IsClick'] = preds[index]

actual = np.array(trainSearchStream['IsClick'].dropna())
predicted = np.array(sample['IsClick'])

tp = sum(actual & predicted)

Per the comments, actual and predicted both have dtype float64 . 根据注释, actualpredicted都具有dtype float64 So the problem can be reproduced simply with 所以问题可以简单地用

In [467]: actual = np.random.random(10)

In [468]: predicted = np.random.random(10)

In [469]: actual & predicted
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs
could not be safely coerced to any supported types according to the casting rule
''safe''

& is the bitwise_and operator . &bitwise_and运算符 It makes sense for integers and boolean values; 它对于整数和布尔值有意义; it doesn't make sense for floating point values. 对于浮点值没有意义

You'll need to explain what you expected this to compute before we could suggest a fix. 您可能需要先解释一下预期的计算结果,然后才能提出修复建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM