Questions tagged [numpy-slicing]

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

Read more:

518 questions
3
votes
2 answers

Numpy python array slicing

I have an array as shown below, [[240.66666667 171.22222222 158.33333333] [218.66666667 134.77777778 143.33333333] [197.33333333 118.55555556 128.44444444] [195.22222222 119.33333333 126.11111111] [196.77777778 118.55555556 123.77777778] …
3
votes
1 answer

Whether slicing of DataFrame in python return copy or reference to the original DataFrame

import numpy as np import pandas as pd from numpy.random import randn np.random.seed(101) print(randn(5, 4)) df = pd.DataFrame( randn(5, 4), ['A', 'B', 'C', 'D', 'F'], ['W', 'X', 'Y', 'Z'] ) tmp_df = df['X'] print(type(tmp_df)) # Here type is…
3
votes
1 answer

What is the purpose of this 'a' in this array slicing in Python ( W[: , : , : , a] )?

Here is the code example: weights = W[:,:,:,a] Here, a is an integer number In array slicing, I need a good explanation (references are a plus) on Python's slice notation. I don't understand what is the purpose of this 'a'. We know that a 3D array…
Samrat Alam
  • 558
  • 3
  • 19
3
votes
1 answer

Construct a ndarray from ndarray according to list of coordinate

I want to build a ndarray taget from src ndarray according to some coordinates. Here is an example src = np.arange(12).reshape(3,4) coordinates = [[[0,0],[0,1],[0,3]], [[2,1],[1,1],[0,1]]] target = src.SOME_API(coordinates) # expect…
Qinsheng Zhang
  • 1,113
  • 1
  • 11
  • 19
3
votes
1 answer

Differences in using range() or slice() to slice a 2-d ndarray

When I try to learn how slice() works, I came across some interesting results when using range() vs. slice(). I don't know how to explain the mechanism. Any help will be appreciated. For example: given an np array: a =…
shubow
  • 33
  • 2
3
votes
5 answers

Reversing an arbitrary slice in Python

I'm looking for a general method on how to reverse a slice in Python. I read this comprehensive post, which has several nice explanations about how slicing works: Understanding Python's slice notation Yet I cannot figure out a generalized rule on…
eaglesear
  • 480
  • 4
  • 12
3
votes
2 answers

Why NumPy creates a view for x[[slice(None), 1, 2]]

In NumPy documentation for advanced indexing, it is mentioned that Also recognize that x[[1, 2, 3]] will trigger advanced indexing, whereas x[[1, 2, slice(None)]] will trigger basic slicing. A matrix is stored sequentially into the memory. I…
Yousof
  • 53
  • 4
3
votes
3 answers

Slicing of ndarrays over array boundaries

Question: given a ndarray: In [2]: a Out[2]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) I look for a routine giving me: array([7, 8, 9, 0, 1]) Ex.: Starting at index 8, crossing the array boundarie and stopping at index 2 (included) If I use slicing, I…
Markus Dutschke
  • 9,341
  • 4
  • 63
  • 58
2
votes
2 answers

How can I slice a numpy array into another numpy array of different size?

I'm trying to broadcast the contents of one array into another array like this: A = np.array([[1, 3], [2, 4]]) A_broadcast = np.array([[1, 0, 3, 0], [0, 2, 0, 4], [1, 2, 3, 4]]) My current approach is by initializing A_broadcast with np.zeros((3,…
2
votes
0 answers

Split Numpy array by specific dimension and merge in python

How to split multidimensional numpy array? for example) suppose splitinig 4-dimensional x array by 4th dimension (whose shape is a×b×c×d) into d number arrays (whoes shape are a×b×c) and merge this d number arrays into original shape (a×b×c×d)? for…
2
votes
1 answer

NumPy one-liner equivalent to this loop, condition changes according to index

In the code below I want to replace the loop in a compact NumPy one-liner equivalent. I think the code is self-explanatory but here is a short explanation: in the array of prediction, I one to threshold the prediction according to a threshold…
Triceratops
  • 741
  • 1
  • 6
  • 15
2
votes
1 answer

np.array index slicing bethween conditions

I need to slice an array's index from where a first condition is true to where a second condition is true, these conditions are never true at the same time, but one can be true more than one time before the other occurs. I try to…
2
votes
0 answers

Numpy slicing + fancy indexing shape

I'm a bit confused as why the following two slicing and index scenarios yield different shapes. Especially when x[:, [1], :, [1]] creates a shape of (1, 2, 2), as the slicing : at dim 0 should reserve the dimensions (2)? How was the shape determined…
Alan
  • 117
  • 2
  • 8
2
votes
1 answer

Partitioning np.array into sub-arrays with no np.nan values

Say I have a np.array, e.g. a = np.array([np.nan, 2., 3., 4., 5., np.nan, np.nan, np.nan, 8., 9., 10., np.nan, 14., np.nan, 16.]). I want to obtain all sub-arrays with no np.nan value, i.e. my desired output is: sub_arrays_list = [array([2., 3., 4.,…
FcmC
  • 143
  • 9
2
votes
0 answers

Value error for shape size of array with slicing during a loop in python

I am facing an error indicating that there are different array sizes when trying to insert values ​​into a predefined 0 values array using the slice method and for loop. In the three examples array [start:stop], stop-start=5. But a problem occurs…
bennu777
  • 21
  • 2
1 2
3
34 35