Questions tagged [numpy-slicing]

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

Read more:

518 questions
0
votes
0 answers

How to access the first two elements for each product_catogory

I have already sorted the dataframe(dfDogNew) based on product_category and quantity_sold. Now I want to access the first two most sold product in each product category, how can I achieve this? I have written a for loop to access them, but the…
0
votes
1 answer

Iterate over inner axes of an array

I want to iterate over some inner dimensions of an array without knowing in advance how many dimensions to iterate over. Furthermore I only know that the last two dimensions should not be iterated over. For example assume the array has dimension 5…
bayes2021
  • 202
  • 1
  • 7
0
votes
2 answers

How to apply function to each block of a numpy array in python

I have an n x m array and a function 'switch(A,J)' that takes array (A) and integer(J) input and outputs an array of size n x m. I wish to split my n x m array into arrays of dimension c x c and apply the function with a fixed J to each c x c array…
Tofee
  • 1
  • 1
0
votes
1 answer

How to "spread" a numpy array (opposite of slice with step size)?

Is there a way to spread the values of a numpy array? Like an opposite to slicing with a step size > 1: >>> a = np.array([[1, 0, 2], [0, 0, 0], [3, 0, 4]]) >>> a array([[1, 0, 2], [0, 0, 0], [3, 0, 4]]) >>> b = a[::2, ::2] >>>…
trivicious
  • 186
  • 1
  • 8
0
votes
0 answers

Filter and modify values from a numpy array based on multiple conditions

I have a number of images and a list of ids associated with each of those images. For the time being I am storing all the images in a list. For e.g. image_list = [image1, image2, image3, image4, image5, ...] ids = [1, 2, 1, 1, 1, ...] I want to…
Ahmad Javed
  • 544
  • 6
  • 26
0
votes
2 answers

Unexpected results from Numpy r_

When I use ":n" or "m:" as arguments to np.r_, I get unexpected results that I don't understand. Here's my code import numpy as np B = np.arange(180).reshape(6,30) C = B[:, np.r_[10:15, 20:26]] D = C[:, np.r_[0:3,8:11]] Now all of that worked as…
0
votes
1 answer

Get each item while slicing (:) a numpy matrix

I have been looking for a solution to this for a while. I am trying to use as few loops in my code as possible, so I've been trying to set slices of the numpy arrays instead. I have a large array of size (s, s, s, s), but you can think of it as…
0
votes
1 answer

How to get index slicing given subset and original array

import numpy as np origin = np.arange(12).reshape(4, 3) """ [0 1 2 3 4 5 6 7 8 9 10 11] """ subset = origin[1:3, :2] """ [3 4 6 7] """ How can I get index slices [1, 2] and [0, 1] given subset and origin assuming subset are contiguous part of…
LeonM
  • 23
  • 1
  • 6
0
votes
1 answer

Downsampling 3D array with numpy

Given array: A = array([[[1, 2, 3, 1], [4, 5, 6, 2], [7, 8, 9, 3]]]) I obtain the following array in the forward pass with a downsampling factor of k-1: k = 3 B = A[...,::k] #output array([[[1, 1], [4, 2], …
0
votes
1 answer

Applying list of slices representing 2D patches to 3D numpy array?

I have a numpy array representing an image (but for now assume the image only has a single channel). i = np.arange(5*5*1).reshape((5, 5)) array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18,…
KDecker
  • 6,928
  • 8
  • 40
  • 81
0
votes
2 answers

Upsampling using Numpy

I want to upsample a given 1d array by adding 'k-1' zeros between the elements for a given upsampling factor 'k'. k=2 A = np.array([1,2,3,4,5]) B = np.insert(A,np.arange(1,len(A)), values=np.zeros(k-1)) The Above code works for k=2. Output: [1 0 2 0…
0
votes
0 answers

Is there a keyword for colon (:) indexing in python?

Is there a keyword for ":" in python indices similar to "Ellipsis" for "..."? In multidimensional arrays, I need to sometimes access an entire dimension and sometimes a single slice. I do this by defining my indices in variables. However, I can save…
jestubbe
  • 1
  • 1
0
votes
1 answer

Recasting NumPy array slice with astype

The code a = 100. * np.random.randn(200) a = a.astype(int) recasts a as an array of integer numbers. Meanwhile, the code a = 100. * np.random.randn(200) …
JustLearning
  • 1,435
  • 1
  • 1
  • 9
0
votes
2 answers

how to use fromiter and ndnumerate together

I'm currently trying to manually implement a function to represent the KNN graph of a set of points as an incidence matrix, and my idea was to take the rows of an affinity matrix(n x n matrix representing the distance between the n points),…
0
votes
1 answer

How can I use a 3d array of indices for a 2d array slicing in Numpy

I have 2 arrays as input. On array as output. Array a holds the data and is of shape (N,M), while array b holds the indices and is of shape (N,X,2). The resulting array should be of shape (N,X), with the values taken from a. Right now it only works…
Gildur7161
  • 21
  • 6