568 questions
2
votes
1
answer
109
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 ...
0
votes
0
answers
60
views
Slicing/indexing bottleneck in JAX
Heyo, I am new to JAX and I am trying to make my code jit-compatible so it runs faster on big data arrays. Here are the functions I am trying to write using JAX:
@jax.jit
def logL(p, clustering):
...
1
vote
1
answer
84
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
3
answers
88
views
Numpy Slicing problem: Possible way to reduce array size while retaining the boundaries condition, equal spacing elements and symmetry around 0
I want to finding a general formula or algorithm to determine all possible values of `step` that satisfy the three conditions (boundary, equal spacing, and symmetry) when slicing an array with the ...
1
vote
1
answer
67
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 ...
2
votes
1
answer
61
views
How can I write zeros to a 2D numpy array by both row and column indices
I have a large (90k x 90k) numpy ndarray and I need to zero out a block of it. I have a list of about 30k indices that indicate which rows and columns need to be zero. The indices aren't necessarily ...
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 ...
3
votes
1
answer
144
views
How to extract sub arrays from a larger array with two start and two stop 1-D arrays in Python?
I am looking for a way to vectorize the following code,
# Let cube have shape (N, M, M)
sub_arrays = np.empty(len(cube), 3, 3)
row_start = ... # Shape (N,) and are integers in range [0, M-2]
row_end ...
2
votes
1
answer
124
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(...
0
votes
2
answers
105
views
Demystify numpy indexing/slicing
could you please help demystify the following numpy indexing/slicing behaviours? Thanks!
arr = np.arange(60).reshape(3,4,5)
print(arr[2, :, 4]) #1
print(arr[[2], :, 4]) #2
print(arr[2, :, [4]])...
1
vote
3
answers
83
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 ...
2
votes
1
answer
169
views
Problem with mismatched length when using a mask
I'm writing a code and I have a function that calculates the values that are not fulfilling a condition with the values that are fulfilling the condition, but I'm having a lot of trouble with managing ...
0
votes
0
answers
27
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,...
0
votes
1
answer
40
views
Index a batch of numpy vectors with a batch of numpy index matrices [duplicate]
If I have a vector vec, I can index it with a matrix as follows:
import numpy as np
vec = np.asarray([1,2,3,4]) # Shape (4,)
mat = np.asarray([[0,2],
[3,1]]) # Shape (2,2)
result =...
1
vote
2
answers
58
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
1
answer
65
views
Slice list of 2D points for plotting with matplotlib
I've got a list of two-dimensional points represented in a numpy style array:
lines = np.array([
[[1,1], [2,3]], # line 1 (x,y) -> (x,y)
[[-1,1], [-2,2]], # line 2 (x,y) -> (x,y)
...
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
3
answers
62
views
Insert columns from array into matrices from other array
Suppose I have a numpy array array of matrices
A=numpy.array([[[1, 0, 0],[1, 1, 0],[0, 1, 0],[1, 1, 1]],[[1, 0, 0],[1, 1, 1],[0, 0, 1],[1, 0, 1]]])
and another array
B=np.array([[1,2,3,4],[5,6,7,8]])
...
0
votes
2
answers
206
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 = ...
3
votes
2
answers
117
views
What does '()' mean to numpy apply along axis and how does it differ from 0
I was trying to get a good understanding of numpy apply along axis. Below is the code from the numpy documentation (https://numpy.org/doc/stable/reference/generated/numpy.apply_along_axis.html)
import ...
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 ...
0
votes
2
answers
61
views
Extracting wrapped column data
I have a particular data file that has 7 columns total: 1 column of time and 6 columns of data. As depicted in the image I attached, the first column represents time, while the next four columns ...
1
vote
2
answers
177
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 ...
0
votes
1
answer
50
views
Indexing numpy array of shape `(A, B, C)` with `[[a, b], [c, d], :]` (`0 <= a, b < A`, `0 <= c, d < B`) produces shape `(2, C)` instead of `(2, 2, C)`
Here's the example:
import numpy as np
A = np.random.randint(100)
B = np.random.randint(100)
C = np.random.randint(100)
print(f"{A=}, {B=}, {C=}")
x = np.random.random((A, B, C))
print(f&...
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 ...
1
vote
1
answer
333
views
Simplest equivalent implementation of numpy.ma.notmasked_edges() for use in JAX
I have a square numpy.ndarray and a numpy boolean mask of the same shape. I want to find the first element in each row of the array that is not masked.
My code currently relies on numpy.ma....
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", ...
0
votes
0
answers
64
views
Numpy slicing gives unexpected result
Does anybody have an explanation for the unexpected numpy slicing results dislplayed below ?
Unexpected behavior demo
import torch
import numpy as np
some_array = np.zeros((1, 3, 42))
chooser_mask = ...
5
votes
1
answer
349
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
...
1
vote
1
answer
32
views
Multiple indices in NumPy
The following works:
np.array([[10,20,30,40]])[:,[1,3,2]]
How can I do this kind of slicing for a one-dimensional array? In other words, how can I fix the following to make the indices refer to the ...
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
50
views
Creating a reduced-length resampling of a numpy array [duplicate]
I have a numpy array of a certain length, and want to produce a much shorter array, where each value in the shorter array is aggregated based on a bin of values in the original array. In my specific ...
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....
2
votes
3
answers
442
views
Using list of lists of indices to slice columns and obtain the row-wise vector length
I have an NxM array, as well as an arbitrary list of sets of column indices I'd like to use to slice the array. For example, the 3x3 array
my_arr = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
and ...
0
votes
1
answer
176
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: ...
1
vote
0
answers
25
views
Unpacking a list in numpy[square brackets] [duplicate]
Consider the following small example, dramatically simplified compared to my actual use case:
import numpy as np
z = np.arange(125).reshape(5,5,5)
i = np.where( np.mod(z[0], 3) == 0 )
So that i is a ...
0
votes
4
answers
379
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])
...
2
votes
1
answer
199
views
Syntax error when unpacking in NumPy arrays [duplicate]
Why does the code below raise a SyntaxError on line 5 for Python 3.8, 3.9, and 3.10 (it seems fine for Python 3.11 and 3.12)?
import numpy as np
a = np.zeros(5)
new_dim = 3
a = a[:, *[np.newaxis] * ...
0
votes
1
answer
466
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
0
answers
82
views
Python : appened the value of created_date based on the condition
i am trying to write one function which will create a new column called last_created_date, and appened the value in that column as per condition.
so this is the data.
rounded_geo_lat
rounded_geo_lng
...
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
2
answers
174
views
Select n'th element along m'th axis on numpy array
My goal is to use an numpy array containing integers and use this to select the element in the corresponding row of a 2D array. The 2D array is monotonically increasing along the 1st axis and I am ...
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
117
views
Slice array along axis with list of different indices
I have a 3-dimensional array/tensor of shape (a, b, c), and I have a list of length a of different indices, each in the range [0, b). I want to use the indices to get an array of size (a, c). Right ...
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
3
answers
67
views
Slicing MultiIndex Pandas Dataframe with integer values incorrect?
We have a MultiIndex DataFrame where the top-level index uses integer values. Slicing for a specific value returns all index values up to the requested value, not just the requested value. Is this a ...
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)
...
2
votes
1
answer
96
views
Create an NxM matrix A to an NxMxL matrix B where B[i,j,:] = kronecker_delta(A[i,j])?
Is there a way to convert a NxM matrix A where
all values of A are positive integers
to an NxMxL matrix B where
L = 1 + max(A)
B[i,j,k] = {1 if k==A[i,j] and 0 otherwise}
using loops I have done ...
-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 ...
0
votes
2
answers
165
views
Indexing array by its value to its nearest non-integer step?
My apologies if I haven't phrased this properly. I have an array of ~10^7 monotonically increasing elements:
example_array = np.array([10, 10.5, 13, 15, 20, 35, 37.1, 40, 50])
I'd like to find the ...