568 questions
2
votes
1
answer
106
views
Shape of sliced array from shape of array and slice
If I know the shape of a numpy array like (1000, 50), and I have an arbitrary selection expressed as an IndexExpression, let's say np.s_[:200, :], how can I evaluate the shape of the sliced array (in ...
1
vote
1
answer
82
views
Arbitrary Stencil Slicing in Numpy
Is there a simple syntax for creating references to an arbitrary number of neighbouring array elements in numpy?
The syntax is relatively straightforward when the number of neighbours is hard-coded. A ...
1
vote
1
answer
63
views
removing Nans from a 3D array without reshaping my data
I have a 3D array (121, 512, 1024) made up of frames of 512x1024 images.
The bottom several rows of the images have Nans which mess up my processing. I want to remove these and end up with something ...
0
votes
0
answers
54
views
Efficient way to operate over a list of Numpy arrays of different sizes
I am writing a function to bin points based on their angle in a radial coordinate system. I would like to have the option to perform some nonlinear downsampling of the points in each bin (computing ...
2
votes
1
answer
119
views
Finding all 1-d arrays within a numpy array
Given a numpy array of dimension n with each direction having length m, I would like to iterate through all 1-dimensional arrays of length m.
For example, consider:
import numpy as np
x = np.identity(...
1
vote
3
answers
81
views
Does Numpy return view or copy when combining slicing and advanced indexing?
The following snippet:
import numpy as np
x = np.arange(25).reshape(5, 5)
print(x.base)
y = x[:2, [0, 2]]
print(y.base)
outputs
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
0
votes
0
answers
26
views
Is this expect behavior for NumPy indexing? If so, why? [duplicate]
I am using NumPy version 2.1.3 and Python 3.12.2. Say I define
ones_arr = np.ones((1, 2, 3))
Now I slice
ones_arr[0, :, [0, 1, 2]]
The result has shape (3, 2), but I would expect it to have shape (2,...
1
vote
2
answers
57
views
Remove specific indices in each row of a numpy ndarray
I have integer arrays of the type:
import numpy as np
seed_idx = np.asarray([[0, 1],
[1, 2],
[2, 3],
[3, 4]], dtype=np.int_)
...
0
votes
0
answers
62
views
quick way to find sub-vector of a cpp vector
Say I have a 4 dimensional C++ std::vector, x. In the numpy's notation, I can easily get access to a sub-vector y=x[:, :, :, 2] using slicing. In cpp, is there a fast way to do this?
A naive way is of ...
0
votes
2
answers
194
views
How to create a tuple of length N in numba for small N (or how to quickly compute bidirectional changes between one and multidimensional indices)
The Question:
Here is a simple function that works with numpy but not numba:
# @numba.jit(nopython=True, fastmath=False, parallel=False)
def testgetvalue(tgvarray, tgvindex):
tgvalue = ...
2
votes
2
answers
64
views
Numpy advanced indexing in multidimensional arrays; not just taking simple rows or columns
I have a three dimensional numpy array. What is the fastest way to get a 3D array that has the largest item of each of final axis of the array without writing a loop.(I will later use CuPy with the ...
1
vote
2
answers
173
views
How to create a mask based on points coordinates?
Starting from this situation:
I would like to create a boolean mask where all external points are considered as True while all internal point are False. Something like this :
The objective would be ...
3
votes
2
answers
95
views
Numpy array slicing with a comma
There are multiple questions on StackOverflow, asking how the comma syntax works, but most of them refer to m[:,n] which refers to the nth column. Similarly, m[n,:] refers to the nth row. I find this ...
0
votes
0
answers
49
views
Keeping a "pointer" to the of the "parent array" from which a "derived array" was produced?
(Aside: my question is equally applicable to numpy structured arrays and non-structured arrays.)
Suppose I have a numpy structured array with the dtype:
EXAMPLE_DTYPE = np.dtype([("alpha", ...
5
votes
1
answer
347
views
Why is unpacking a list in indexing a syntax error in Python 3.8 but not Python 3.12?
The following code
import numpy as np
x = np.arange(32).reshape(2,2,2,2,2)
extra = [1 for _ in range(3)]
print(x[*extra, 0, 0])
prints 28 as expected in Python 3.12 but results in the syntax error
...
0
votes
0
answers
42
views
How to combine non-contiguous numpy slices
I have a numpy array with shape (M, N, N) - it's effectively a bunch (M) of (N,N) covariance matrices. I want to be able to extract submatrices out of this with shape (M, P, P). But I'm trying to ...
0
votes
1
answer
45
views
How to fill an nd array with values from a 1d-array?
The following is a real-world problem in numPy reduced to the essentials, just with smaller dimensions.
Let's say I want to create an n-dimensional array all with dimensions (10, 10, 100):
all = np....
0
votes
1
answer
174
views
I want to flip an image (with three channels RGB) horizontally just using array slicing. How can I do it with python?
I appreciate it if you can provide me with one-line code.
I used np.flip but I want a different approach to make it generalized.
This was my code: np.flip(image, 1)
I also used np.fliplr(image).
Note: ...
0
votes
4
answers
377
views
pytorch split array by list of indices
I want to split a torch array by a list of indices.
For example say my input array is torch.arange(20)
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19])
...
0
votes
1
answer
463
views
i installed numpy 1.26.3 but still not able to use np. method
I installed NUMPY by using pip install NUMPY
and it installed and then I'm still not able to use
np. method .
the NUMPY version is 1.26.3.
the error in "np is not defined".
can somebody ...
0
votes
1
answer
133
views
NumPy Get elements based on starting indices and stride
I am looking for the numpythonic way to accomplish the following:
A = np.arange(1000)
x = np.array([0, 10, 20, 30], dtype=int)
dx = np.array([3, 4, 5, 6], dtype=int)
for x_, dx_ in zip(x, dx):
...
1
vote
1
answer
55
views
Numpy shape function
A = np.array([
[-1, 3],
[3, 2]
], dtype=np.dtype(float))
b = np.array([7, 1], dtype=np.dtype(float))
print(f"Shape of A: {A.shape}")
...
1
vote
1
answer
145
views
How to efficiently slice numpy arrays? (Finite difference method)
I am trying to use the finite difference method with NumPy arrays, but the slicing is incredibly slow. It's not viable to use lists as I am applying math operations. Using matlab, equivalent code is ...
1
vote
1
answer
113
views
How to compute the moving average over 3D array with a step size?
I need to calculate a moving average over a 3D array with a step size set by me. What I am doing right now is
img = np.ones(10,10,50)
img_new = bottleneck.move.move_mean(img, window=5, axis=2)
...
-1
votes
1
answer
146
views
I want to calculate some values for each side of a grid of points
I have a numpy array of n x m values, which may look something like this:
[[ 1, 2, 1, 3, 5],
[ 0, 4, 2, 4, 1],
[ 1, 1, 1, 0, 2]]
I want to calculate the difference and mean from every grid point to ...