Say I have a multi-dimensional array, for example: And say I have a one-dimensional array (b), with some values which may be seen in my multi-dimensional array (a): Now I want to replace all of the non-intersecting values in array a with zero. I want to get the following result: How could I achieve this? I tried several ways of
Tag: numpy-ndarray
How to get the all columns except last column in 3D numpy array?
I Have a 3D array composed of various columns. I just want to slice the last column. The array looks like the following: I have tried the following code. But it only shows the last column while I want to show all columns values except the last column. Answer IUUC, you can use: Example input: matching output:
How could I generate a 2D array from a known slope and aspect value?
Given a dummy heightmap (or digital elevation model) stored as a Numpy array like this: I can calculate its slope and aspect like this: And visualise it: But how would I go the other way? I’d like to generate a blank 2D Numpy array of a fixed size, then fill it with values that follow a known slope and aspect
Replacing chunks of numpy array on condition
Let’s say I have the following numpy array, of 1’s and 0’s exclusively: I want to group all elements into chunks of 3, and replace the chunks by a single value, based on a condition. Let’s say I want [0,1,1] to become 5, and [0,1,0] to become 10. Thus the desired output would be: All possible combinations of 1’s and
How to access items from ndarray with dtype=object
How can I access data inside a numpy array with dtype=object? The following raises an IndexError. Answer Since you passed in the dict to numpy.array() without putting it in a list, this is a zero-dimensional array. To index into a zero-dimensional array, you can use b.item() to access the element inside. For completeness, to access the data in the “a”
merge columns in numpy matrix
I have a NumPy matrix like this one (it could have several columns, this is just an example: I need to merge all columns in this matrix, replacing nan values with the corresponding non-nan value (if exists). Example output: Is there a way to achieve this with some built-in function in NumPy? EDIT: if there is more than one non-nan
Comparing two Dataframes with diff length to find difference in specific column
i have 2 dataframes have same columns with different len. the reuslt i want to get: and each df have length like this. each dataframe has column named ‘name, id, type, len’ i need to check those columns(name,type,len) in each df to compare ‘id’ column whether it has same value or not. so i tried like this. I have above
Filter a numpy ndarray using another numpy ndarray
I have two numpy ndarrays of the same shape (15081, 56724, 3, 3). What I want to do is as follows: Say we have a cross section of the first array, array1[1, 1, :, :], looks like this: I want to convert it to a boolean in a way that the max of each row is True and the rest
Mapping values between numpy arrays and categorizing it
Novice in python! A numpy array A of 1000 elements consists of fixed values between -100 to +100. These 201 values are nothing but categories. Another numpy array B has the approximations of the values for each category of A. Both A and B are of same length. For example A=[-100, -100, -88, 87, 85, 32, 32, 32] and corresponding
Numpy array assignment along axis and index
I have a 3D volume and I modify slices along different axis. (np.take is equivalent of writing self.volume[idx], self.volume[:, idx] and self.volume[:, :, idx]) Finally, I want to assign a new slice in my volume along the axis : This is where I need some help. I can’t figure out a cleaner way of doing this assignment. (I would like