Modify specific list of cells of a numpy array

numpy array replace values by index
numpy put 2d array
numpy replace values in 2d array
numpy array append
numpy array replace 0 with 1
numpy replace array with another array
numpy array to list
numpy array indexing

Suppose I have a numpy array and I want to change the values. I have a specific list of (x,y)-coordinates that indicate the cells that should get added an additional term. The code below does the job for me.

>>> import numpy as np

n = 4
M = np.ones([n,n])
x = [0,1,2,3]
y = [1,2,3,0]
xy = list(zip(x,y))

alpha = 0.3

for i in range(n):

    for j in range(n):

        M[i,j] = alpha*M[i,j] + ((i,j) in xy)*(1-alpha)*3

>>> M
Out[1]: 
array([[0.3, 2.4, 0.3, 0.3],
       [0.3, 0.3, 2.4, 0.3],
       [0.3, 0.3, 0.3, 2.4],
       [2.4, 0.3, 0.3, 0.3]])

However, I think that there are less cumbersome ways to achieve the same thing. Could someone help me to get rid of the two for-loops, for example?

n = 4

x = [0,1,2,3]
y = [1,2,3,0]

M = alpha*np.ones([n,n])
M[x,y] += (1-alpha)*3

How to change a single value in a NumPy array?, Is this what you are after? Just index the element and assign a new value. A[2,1]= 150 A Out[345]: array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 150, 11, 12], [13, 14, 15, 16]]). Rows and columns of NumPy arrays can be selected or modified using the square-bracket indexing notation in Python. To select a row in a 2D array, use P[i].For example, P[0] will return the first row of P.

Yes, using np.add.at.

Since np.add is a ufunc, it has a special method at that lets you do easy in-place operations like that.

M = np.ones([n,n]) * alpha

np.add.at(M, (x, y), (1-alpha)*3)

M
Out[]: 
array([[0.3, 2.4, 0.3, 0.3],
       [0.3, 0.3, 2.4, 0.3],
       [0.3, 0.3, 0.3, 2.4],
       [2.4, 0.3, 0.3, 0.3]])

Array manipulation routines - Numpy and Scipy, Replaces specified elements of an array with given values. In 'raise' mode, if an exception occurs the target array may still be modified. Returns a new array with the specified shape. 2: append. Appends the values to the end of an array. 3: insert. Inserts the values along the given axis before the given indices. 4: delete. Returns a new array with sub-arrays along an axis deleted. 5: unique. Finds the unique elements of an array

You can use tuple indexing for this:

M = np.ones((n,n))
N = np.zeros((n, n))

N[x, y] = np.ones(len(x))

M = alpha * M + N * (1 - alpha) * 3

or

N = np.zeros((n, n))
N[x, y] = np.ones(len(x))

M = np.where(N, alpha + (1 - alpha) * 3, alpha)

numpy.put — NumPy v1.19 Manual, Gives a new shape to an array without changing its data. ravel (a[, order]) Roll the specified axis backwards, until it lies in a given position. swapaxes (a, axis1� Replace all elements of Python NumPy Array that are greater than some value: stackoverflow: Replace “zero-columns” with values from a numpy array: stackoverflow: numpy.place: numpy doc: Numpy where function multiple conditions: stackoverflow: Replace NaN's in NumPy array with closest non-NaN value: stackoverflow: numpy.put: numpy doc: numpy

There are many ways and one them is:

M=M*alpha
for i,j in zip(x,y):
  M[i,j] +=(1-alpha)*3

Array manipulation routines — NumPy v1.20.dev0 Manual, Replacing elements in a NumPy array if a condition is met replaces each element in the array that satisfies the given condition with another value. Numpy array Numpy Array has a member variable that tells about the datatype of elements in it i.e. ndarray.dtype. We created the Numpy Array from the list or tuple. While creation numpy.array() will deduce the data type of the elements based on input passed. But we can check the data type of Numpy Array elements i.e.

How to replace elements in a NumPy array if a condition is met in , Python code example 'Replace values in an array' for the package numpy, powered by Kite. In this post, we are going to see the ways in which we can change the dtype of the given numpy array. In order to change the dtype of the given array object, we will use numpy.astype() function. The function takes an argument which is the target data type. The function supports all the generic types and built-in types of data.

numpy - Replace values in an array - Python code example, Write a NumPy program to replace all elements of NumPy array that are greater than specified array. Pictorial Presentation: Python NumPy:� Each cell contains a piece of data. To refer to elements of a cell array, use array indexing. You can index into a cell array using smooth parentheses, (), and into the contents of cells using curly braces, {}. Create a cell array that contains several temperature readings taken on a given date. Specify a date as a character vector, and

NumPy: Replace all elements of numpy array that are greater than , high-level number objects: integers, floating point; containers: lists (costless insertion and extension package to Python for multi-dimensional arrays; closer to hardware Hint: Individual array elements can be accessed similarly to a list, e.g. a[1] or a[1, 2] . When modifying the view, the original array is modified as well:. We can use numpy ndarray tolist() function to convert the array to a list. If the array is multi-dimensional, a nested list is returned. If the array is multi-dimensional, a nested list is returned. For one-dimensional array, a list with the array elements is returned.