0

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

1
  • 1
    what did you have tried so far? how ever this is not evening here! :) Commented Nov 23, 2017 at 15:50

3 Answers 3

1
new_pred = [int(pred) if pred > 0 else 0 for pred in np.nditer(predict)]
print(new_pred)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.