numpy数组上的逻辑操作,最初是一个pandas数据帧

时间:2015-06-25 16:04:16

标签: python python-2.7 numpy pandas

我想要对元素逻辑运算执行两个变量。但是我收到以下错误:

    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''

以下是我的代码:

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)

1 个答案:

答案 0 :(得分:1)

根据评论,actualpredicted都有dtype float64。 所以问题可以通过

简单地再现
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''

&the bitwise_and operator。它对整数和布尔值有意义;它是浮点值的doesn't make sense

在我们提出修复建议之前,您需要先解释一下您的预期。