Skip to main content
Filter by
Sorted by
Tagged with
2 votes
1 answer
109 views

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 ...
Kyle's user avatar
  • 364
0 votes
0 answers
60 views

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): ...
Yulia Demutskaia's user avatar
1 vote
1 answer
84 views

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 ...
DavidJ's user avatar
  • 440
1 vote
3 answers
88 views

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 ...
btypoon's user avatar
  • 11
1 vote
1 answer
67 views

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 ...
Spectroscopist1812's user avatar
2 votes
1 answer
61 views

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 ...
mightypile's user avatar
  • 8,130
0 votes
0 answers
54 views

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 ...
Gtingstad's user avatar
3 votes
1 answer
144 views

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 ...
Austin's user avatar
  • 33
2 votes
1 answer
124 views

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(...
Matt's user avatar
  • 155
0 votes
2 answers
105 views

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]])...
ThxAlot's user avatar
  • 124
1 vote
3 answers
83 views

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 ...
Antonios Sarikas's user avatar
2 votes
1 answer
169 views

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 ...
alvarito mendez's user avatar
0 votes
0 answers
27 views

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,...
lmbell's user avatar
  • 43
0 votes
1 answer
40 views

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 =...
Ben's user avatar
  • 539
1 vote
2 answers
58 views

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_) ...
Ben's user avatar
  • 539
0 votes
1 answer
65 views

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) ...
mefiX's user avatar
  • 1,347
0 votes
0 answers
62 views

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 ...
Tony Shi's user avatar
0 votes
3 answers
62 views

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]]) ...
aqclsrt's user avatar
0 votes
2 answers
206 views

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 = ...
Nathan Gabriel's user avatar
3 votes
2 answers
117 views

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 ...
rert588's user avatar
  • 779
2 votes
2 answers
64 views

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 ...
Mikael's user avatar
  • 33
0 votes
2 answers
61 views

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 ...
kowalski's user avatar
  • 151
1 vote
2 answers
177 views

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 ...
Certes's user avatar
  • 159
0 votes
1 answer
50 views

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&...
bzm3r's user avatar
  • 4,664
3 votes
2 answers
95 views

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 ...
Shirsak's user avatar
  • 57
1 vote
1 answer
333 views

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....
Ben's user avatar
  • 539
0 votes
0 answers
49 views

(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", ...
bzm3r's user avatar
  • 4,664
0 votes
0 answers
64 views

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 = ...
n0tis's user avatar
  • 838
5 votes
1 answer
349 views

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 ...
D. Song's user avatar
  • 153
1 vote
1 answer
32 views

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 ...
AlwaysLearning's user avatar
0 votes
0 answers
42 views

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 ...
Tom Johnson's user avatar
  • 2,224
0 votes
1 answer
50 views

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 ...
escapecharacter's user avatar
0 votes
1 answer
45 views

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....
MichaelW's user avatar
  • 1,502
2 votes
3 answers
442 views

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 ...
Snoekoog's user avatar
0 votes
1 answer
176 views

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: ...
Amirparsa Rouhi's user avatar
1 vote
0 answers
25 views

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 ...
evanb's user avatar
  • 180
0 votes
4 answers
379 views

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]) ...
piccolo's user avatar
  • 2,227
2 votes
1 answer
199 views

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] * ...
Tom M. Ragonneau's user avatar
0 votes
1 answer
466 views

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 ...
Mehran Kaj's user avatar
0 votes
0 answers
82 views

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 ...
vish's user avatar
  • 326
0 votes
1 answer
133 views

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): ...
Sterling Butters's user avatar
1 vote
2 answers
174 views

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 ...
Edin's user avatar
  • 13
1 vote
1 answer
55 views

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}") ...
user1965449's user avatar
  • 2,931
1 vote
1 answer
117 views

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 ...
mhenning's user avatar
  • 1,953
1 vote
1 answer
145 views

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 ...
THATS MY QUANT MY QUANTITATIVE's user avatar
1 vote
3 answers
67 views

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 ...
feetwet's user avatar
  • 3,507
1 vote
1 answer
113 views

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) ...
emely_pi's user avatar
  • 701
2 votes
1 answer
96 views

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 ...
Ollie Rosen's user avatar
-1 votes
1 answer
146 views

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 ...
Simon M's user avatar
0 votes
2 answers
165 views

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 ...
Diego's user avatar
  • 60

1
2 3 4 5
12