Questions tagged [numpy-slicing]

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

Read more:

518 questions
1
vote
1 answer

Crop out background in an image - NumPy / Python

I have the following image extracted. Background value is 170, I need to extract the polygon with a minimum of 170 values. for the image is like below I would like to have a processed image as below I can implement a nested for loop and iterate…
noone
  • 6,168
  • 2
  • 42
  • 51
1
vote
4 answers

How to change referenced numpy arrays to a slice of itself?

Consider the following operation and slicing applied to two numpy arrays: In [1]: import numpy as np In [2]: a = np.array([1,2,3,4]) …
bmello
  • 1,864
  • 3
  • 18
  • 23
1
vote
1 answer

How do I replace every nth instance in an ndarray?

I have a numpy array atoms.numbers which looks like: array([27, 27, 27, 27, 27, 27, 57, 57, 57, 57, 57, 57, 57, 57, 27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 27, 27, 27,…
1
vote
1 answer

Slicing of a 3d array with use of a 2d array in Numpy

I got a question regarding slicing of a 3d array with use of a 2d array. largearray is the 3d array which I want to slice with values from the 2d smallarray array([[[36.914 , 38.795 , 37.733 , 36.68 , 35.411003, 33.494 ,…
1
vote
2 answers

Slicing N+1d Numpy Array using Nd array

In order to test a wide range of parameters on a single value output, I am creating a multi-dimensional array to store the values. However, in order to update the values accordingly, I need to calculate the positions to be updated. In order to make…
1
vote
2 answers

Finding nearest neighbour =, TypeError: only integer scalar arrays can be converted to a scalar index

I made a function to find the nearest neighbour to a point for a homemade knn classifier. I did the following: Defined a function euclid_dist(x,y) to find the distance between two points on a 2-d plane. Defined a function nearest_neigh(p, points,…
xoxoxoxoxoxo
  • 40
  • 2
  • 7
1
vote
2 answers

Numpy: apply function over arbitrarily oriented slices of array

Is there a way to apply a function over general slices of a multidimensional array? As an example, given a 4D input array representing a color video [frame, y, x, color_channel], we would like to apply a 2D image filter to all 2D slices in [y,…
Hugues
  • 2,865
  • 1
  • 27
  • 39
1
vote
1 answer

Broadcasting/Vectorizing inner and outer for loops in python/NumPy

Purpose I have turned a double for loop into a single for loop using vectorization. I would like to now get rid of the last loop. I want to slice an Nx3 array of coordinates and calculate distances between the sliced portion and the remaining…
1
vote
1 answer

Numpy slicing a fixed length on two axis based on different starting index given by two arrays

For example, I have nparray: a = np.arange(48).reshape((3,4,4)) ''' [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] [[16 17 18 19] [20 21 22 23] [24 25 26 27] [28 29 30 31]] [[32 33 34 35] [36 37 38 39] [40 41 42 43] …
Jason
  • 3,166
  • 3
  • 20
  • 37
1
vote
1 answer

How do we split a numpy array at multiples of a number? (eg: array 1 : 0-33, array 2: 34-66, array 3: 67-100)

I have been trying to split a n dimensional array. I want to split it at multiples of 100//3 (=33) I want to split array of length 100 into 3 groups such that group 1 is from indices 0-33 group 2 is from 34-66 group 3 is from 67-100 This is what I…
1
vote
1 answer

Extract mask from 3D RGB image using a 1D Boolean array

I have a 3D image which is a numpy array of shape (1314, 489, 3) and looks as follows: Now I want to calculate the mean RGB color value of the mask (the cob without the black background). Calculating the RGB value for the whole image is…
Olympia
  • 457
  • 8
  • 19
1
vote
1 answer

How to create a Numpy Index with colons everywhere except at one variable coordinate?

I would like to create an index in the form of [:, :, :, 0, :, :, :, :] where the position of 0 is determined by a variable, say axis to slice a NumPy array. Obviously there are two special cases that are easy to treat: axis = 0 would be…
MikeL
  • 2,369
  • 2
  • 24
  • 38
1
vote
1 answer

How to *actually* delete a column from numpy structured array (so that it won't show up in binary file)

I have a structured array which is loaded from a binary file. In [85]: dx = np.dtype([('op', '
1
vote
1 answer

How to interpret this kind of NumPy indexing: a[2::2, ::2]?

Can any one tell how to interpret this kind of addressing : a[2::2, ::2]? I have no idea how to use this kind of indexing. a = np.random.random((6,6)) print(a) [[0.17948771 0.61436323 0.48768101 0.20540278 0.15331527 0.17766787] [0.40028608…
Flying pig
  • 572
  • 1
  • 10
  • 27
1
vote
1 answer

Taking different columns from each 2D slice of a 3D numpy array

Assume the following 3D numpy array: array([[[4, 1, 3, 5, 0, 1, 5, 4, 3], [2, 3, 3, 2, 1, 0, 5, 5, 4], [5, 3, 0, 2, 2, 2, 5, 3, 2], [0, 3, 1, 0, 2, 4, 1, 1, 5], [2, 0, 0, 1, 4, 0, 3, 5, 3]], [[2, 2, 4, 1, 3,…