568 questions
            
            
            
                2
            
            votes
        
        
            
                1
            
            answer
        
        
            
                103
            
            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
        
        
            
                56
            
            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
        
        
            
                72
            
            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
        
        
            
                82
            
            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
        
        
            
                61
            
            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
        
        
            
                129
            
            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
        
        
            
                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(...
                
            
       
        
            
                0
            
            votes
        
        
            
                2
            
            answers
        
        
            
                103
            
            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
        
        
            
                78
            
            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
        
        
            
                161
            
            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
        
        
            
                23
            
            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
        
        
            
                55
            
            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_)
...
                
            
       
         
         
         
         
         
        