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 1.0 0
F16 0.0 0.0 1.0 0.0 0.0 1.0 1.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1
I want to apply this code:
m = df2.to_numpy()
mask = m * m[:, np.newaxis, :].transpose((0,2,1))
bol = mask.any(axis=1)
final = m | bol
But at the final
step I get this error:
TypeError: ufunc 'bitwise_or' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Please tell me where is the mistake?
CodePudding user response:
m
is an array of floats, you cannot directly perform a A | B
operation if not both arrays are of boolean type.
Assuming you want to consider 0
/1
as False
/True
, use:
m = df2.to_numpy().astype(bool)
or
final = m.astype(bool)|bol
If you only need final
and not the intermediates, it is more efficient to convert to boolean early.
Output as integer:
final = (m.astype(bool)|bol).astype(int)
or, if early conversion of m
to boolean:
# m = df2.to_numpy().astype(bool)
# ...
final = (m|bol).astype(int)
CodePudding user response:
Convert values of array m
to boolean for mapping 1/0
to True/False
:
final = m.astype(bool) | bol
print (final)
[[ True True True True True True True True True True True True
True True True True]
[ True True True True True True True True True True True True
True True True True]
[ True True True True True True True True True True True True
True True True True]
[ True True True True True True True True True True True True
True True True True]
[False True True True True True True True True True True True
True True True True]
[ True True True True True True True True True True True True
True True True True]
[ True True True True True True True True True True True True
True True True True]
[ True True True True True True True True True True True True
True True True True]
[ True True True True True True True True True True True True
True True True True]
[ True True True True True True True True True True True True
True True True True]
[ True True True True True True True True True True True True
True True True True]
[False True True True True True True True True True True True
True True True True]
[ True True True True True True True True True True True True
True False True True]
[ True False True True True True True True True True True True
True True True True]
[ True True True True True True True True True True True True
True True True True]
[False True True True False True True True True True True True
True True True True]]
Last cast to integers:
final = (m.astype(bool) | bol).astype(int)