Python Forum
[numpy]add a value to an array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[numpy]add a value to an array
#1
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 -
Reply
#2
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]])
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 302 Mar-26-2024, 02:18 PM
Last Post: snippsat
  reshaping 2D numpy array paul18fr 3 971 Jan-03-2023, 06:45 PM
Last Post: paul18fr
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,530 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  Numpy array BrianPA 13 4,838 Jan-23-2021, 09:36 AM
Last Post: Serafim
  How to fill datetime64 field in numpy structured array? AlekseyPython 0 2,237 Oct-20-2020, 08:17 AM
Last Post: AlekseyPython
  Adding data in 3D array from 2D numpy array asmasattar 0 2,170 Jul-23-2020, 10:55 AM
Last Post: asmasattar
  converting dataframe to int numpy array glennford49 1 2,290 Apr-04-2020, 06:15 AM
Last Post: snippsat
  Replacing sub array in Numpy array ThemePark 5 4,091 Apr-01-2020, 01:16 PM
Last Post: ThemePark
  How to prepare a NumPy array which include float type array elements subhash 0 1,885 Mar-02-2020, 06:46 AM
Last Post: subhash
  numpy.where array search for string in just one coordinate adetheheat 1 2,251 Jan-09-2020, 07:09 PM
Last Post: paul18fr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020