Skip to content
Advertisement

Tag: numpy-ndarray

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

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

Advertisement