[numpy]add a value to an array - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: [numpy]add a value to an array (/thread-18594.html) |
[numpy]add a value to an array - elmismo999 - May-23-2019 I have an array created with numpy arr10=np.random.randint(0,10,(4,4))If for example I want to sum to each element the value 10 where the value is > 5 how I do that please ? Thank You - RE: add a value to an array - michalmonday - May-23-2019 Quote:I want to sum to each element the value 10 I don't understand this part. Would you like to add 10 to every value over 5? In such case you could do something like this: >>> import numpy as np >>> arr10=np.random.randint(0,10,(4,4)) >>> arr10 array([[7, 2, 6, 2], [5, 7, 0, 8], [4, 2, 0, 5], [1, 5, 6, 6]]) >>> np.where(arr10 > 5, arr10+10, arr10) array([[17, 2, 16, 2], [ 5, 17, 0, 18], [ 4, 2, 0, 5], [ 1, 5, 16, 16]]) >>> |