Questions tagged [numpy-slicing]

questions related to numpy indexing routines, in particular, slicing, indexing and concatenation

Read more:

518 questions
1
vote
2 answers

How to slice every element of nparray and store it in new nparray?

If this is the nparray i have arr_str = np.array(['10', '100', '301', '23423']) I want to convert into like this using slicing get the last char in the str a[-1] where a is element in nparray arr_slice = np.array(['0', '0', '1', '3']) just like…
abhinit21
  • 330
  • 1
  • 4
  • 13
1
vote
1 answer

What is mechanism of using sliced Numpy arrays in a tuple assignment (Python)?

I know that tuple assignment in Python such as the following a, b = b, a works by first packing b and a into a tuple (b, a) and then unpacking it to a and b, so as to achieve swapping two names in one statement. But I found that if a and b are…
ihdv
  • 1,927
  • 2
  • 13
  • 29
1
vote
3 answers

Properties of numpy array

Suppose we have a numpy array. a = np.array([[1,2,3],[4,5,6], [7,8,9]]) Now, if I want to extract Columns 0 and 2, it would require doing something like b = a[:, [0, 2]] However, if we try to find properties of b by executing b.flags, we…
1
vote
1 answer

Getting indices of different lengths to slice a multidimensional numpy array

Given these arrays, R = np.array([[ 5., 3., 2., 7., 3., 6., 8., 9., 10., 55.], [ 5., 4., 2., 7., 3., 6., 8., 10., 10., 55.]]) F = np.array([[ 0.2 , 0.4 , 0.1 , 0.3 , 0.25, 0.25, 0.2 , 0.1 , 0.1 , 0.1 ], …
PJORR
  • 71
  • 5
1
vote
1 answer

Vectorizing for loop using splicing in NumPy

I have this for loop: blockSize = 5 ds = np.arange(20) ds = np.reshape(ds, (1, len(ds)) counts = np.zeros(len(ds[0]/blockSize)) for i in range(len(counts[0])): counts[0, i] = np.floor(np.sum(ds[0, i*blockSize:i*blockSize+blockSize])) I am…
1
vote
2 answers

How to mark data as anomalies based on specific condition in each interval

I try to search for this problem many places and couldn't find the right tools. I have a simple time series data, print(anom) 0 0 1 0 2 0 3 0 4 0 .. 52777 1 52778 1 52779 0 52780 1 For any…
T PHo
  • 89
  • 1
  • 13
1
vote
2 answers

batched tensor slice, slice B x N x M with B x 1

I have an B x M x N tensor, X, and I have and B x 1 tensor, Y, which corresponds to the index of tensor X at dimension=1 that I want to keep. What is the shorthand for this slice so that I can avoid a loop? Essentially I want to do this: Z =…
Megan Hardy
  • 397
  • 4
  • 12
1
vote
1 answer

Bad performance of numpy slicing in function

I have example code like import numpy as np nbins = 1301 init = np.ones(nbins*2+1)*(-1) init[0] = 0 init[1::2] = 0 z0 = np.array(init, dtype=np.complex128, ndmin=1) initn = z0.view(np.float64) deltasf = np.linspace(-10, 10, nbins) gsf =…
wa4557
  • 953
  • 3
  • 13
  • 25
1
vote
1 answer

view of numpy with 2D slicing

Numpy uses a view object to minimize memory copying. The code below slices the original ndarray using index list. The result is None, which means arr[ [0, 3] ] is allocated its own memory. arr = np.arange(5) print(arr[ [0, 3] ].base) In case of…
1
vote
1 answer

Numpy slicing with list

I'm trying to slice an Ndarray a with a list b. But the behaviour is not as I would expect it. What do I have to change to get the wanted result? a = np.arange(27).reshape(3,3,3) b = [2,2] Actual behaviour: a[:,b] array([[[ 6, 7, 8], […
michmest
  • 25
  • 4
1
vote
1 answer

Slice Non-Contiguous and Contiguous Columns in Pandas to the Last Column in DataFrame

I am fairly new to python and want to get non-contiguous columns in pandas, but can seem to figure it out. I know in R it could be done like df[:, c(1, 3:)] to select columns 1, 3 to end of columns when using indexing. Just want to know how that is…
GSA
  • 751
  • 8
  • 12
1
vote
0 answers

A question about time taken in numpy array assignment with and without slices

I was trying to understand the assignment of values to slices of NumPy arrays. In particular, if sliced assignments are slower than 'less sliced' assignments. Example: a[0,:10] = 5 vs a[0:1,:10] = 5 This inadvertently landed me on a situation where…
figs_and_nuts
  • 4,870
  • 2
  • 31
  • 56
1
vote
1 answer

Using the colon operator to slice columns in numpy when it might be a vector or might be a matrix

I have two general functions Estability3 and Lstability3 where I would like to evaluate both two dimensional slices of arrays and one dimensional ranges of vectors. I have explored the error outside the functions in a jupyter notebook with some of…
StevenJD
  • 77
  • 6
1
vote
1 answer

slicing python list beyond its length

I have to slice a Python list/numpy array from an index to -dx and +dx. where dx is constant. for example: (the position/index that contains 1 only for illustration, as the center index). A = [0, 0, 0, 0, 1, 0, 0, 0, 0] dx=3 print(A[4-dx: 4+dx+1]) …
tetukowski
  • 63
  • 1
  • 8
1
vote
3 answers

In numpy, how do I use a list of indices to slice from the second dimension of a 2D array?

Best explained with an example. The following code gives me the result that I want, but I want to avoid the iteration/list comprehension. import numpy as np foo = np.array([range(100, 105), range(200, 205), range(300, 305)]) print("Data:\n",…
Thomas
  • 392
  • 5
  • 14