I've been going crazy trying to figure out what stupid thing I'm doing wrong here.
I'm using NumPy, and I have specific row indices and specific column indices that I want to select from. Here's the gist of my problem:
import numpy as np
a =…
I have a numpy 2D array, and I would like to select different sized ranges of this array, depending on the column index. Here is the input array a = np.reshape(np.array(range(15)), (5, 3)) example
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]
[12…
Working in NumPy, I understand how to slice 2D arrays from a 3D array using this article.
Depending on the axis I'd want to slice in:
array = [[[0 1 2]
[3 4 5]
[6 7 8]]
[[9 10 11]
[12 13 14]
…
I have a quite large 1d numpy array Xold with given values. These values shall be
replaced according to the rule specified by a 2d numpy array Y:
An example would…
I am trying to truncate 'data' (which is size 112943) to shape (1,15000) with the following line of code:
data = np.reshape(data, (1, 15000))
However, that gives me the following error:
ValueError: cannot reshape array of size 112943 into shape…
I want to split array into array with mask and index
like below
a = array([ 0, 1, 2, 3, 4, 5]))
b = [0,2,3]
into
c = array([[0, 2, 3], [1, 3, 4], [2, 4, 5]])
can I do this without loop?
Edit:
More examples...
Say, we have an array a…
We noticed that the mixed usage of fancy indexing and slicing is so confusing and undocumented for multi-dimensional arrays, for example:
In [114]: x = np.arange(720).reshape((2,3,4,5,6))
In [115]: x[:,:,:,0,[0,1,2,4,5]].shape
Out[115]: (2, 3, 4,…
I came across an example in the numpy docs where there is an example with a complex step size inside a slice (see the 2nd example).
From experiments, I can see what it is doing; it is similar to np.linspace.
In [42]: np.r_[-1:1:1j]
Out[42]:…
I have a tensor x in pytorch let's say of shape (5,3,2,6) and another tensor idx of shape (5,3,2,1) which contain indices for every element in first tensor. I want a slicing of the first tensor with the indices of the second tensor. I tried x=…
I have an image with a missing part that I know has been coloured in green (first image). What is the most pythonic way of generating another "boolean" image that shows white for the missing parts and black for the non-missing parts (second…
Let a be some (not necessarily one-dimensional) NumPy array with n * m elements along its last axis. I wish to "split" this array along its last axis so that I take every n'th element starting from 0 up until n.
To be explicit let a have shape (k, n…
What is the difference between [x][y][:] and [x, y, :] in numpy?
In my example I want to assign a np.ndarray into another and one way works while the other does not. With the same indices, so i really wonder why.
Thanks.
>>>…
I have an array x of the form,
x = [[1,2,3,...,7,8,9],
[1,2,3,...,7,9,8],
...,
[9,8,7,...,3,1,2],
[9,8,7,...,3,2,1]]
I also have an array of non-allowed numbers for each column. I want to select all of the rows which only have allowed characters in…