Reputation: 1945
I have the following dataset as ndarray:
print(predict)
[[[ 7.03453398e+00 1.00585070e+01 5.34464791e-03 4.08430994e-02
1.73265897e-02 1.48283215e-02 -1.95482448e-02 -5.05701825e-02
8.75583757e-03 6.01415014e+00 7.04624176e+00 8.97313499e+00
-1.38850473e-02 -6.31546229e-02 4.99860048e+00 6.01915455e+00 ...
I would like to update this set, printing 0, if the value is < 0, and round each value.
How can I easily implement it in Python 3.?
Thanks
Upvotes: 0
Views: 238
Reputation: 12669
You can try this:
One line solution:
import numpy as np
a=np.array([[[ 7.03453398e+00, 1.00585070e+01 , 5.34464791e-03, 4.08430994e-02,
1.73265897e-02, 1.48283215e-02, -1.95482448e-02, -5.05701825e-02,
8.75583757e-03, 6.01415014e+00, 7.04624176e+00, 8.97313499e+00,
-1.38850473e-02, -6.31546229e-02, 4.99860048e+00, 6.01915455e+00]]])
print([0 if i<0 else int(i) for i in a[0][0] ])
output:
[7, 10, 0, 0, 0, 0, 0, 0, 0, 6, 7, 8, 0, 0, 4, 6]
Detailed solution:
Above list comprehensive is same as:
round=[]
for i in a[0][0]:
if i<0:
round.append(0)
else:
round.append(int(i))
print(round)
Upvotes: 0
Reputation: 340
new_pred = [int(pred) if pred > 0 else 0 for pred in np.nditer(predict)]
print(new_pred)
Upvotes: 1
Reputation: 6103
Since you say it's an ndarray
, I assume that numpy is allowed. Does this work for what you're looking for?
np.round(np.clip(predict, 0, None))
Upvotes: 0