Questions tagged [numpy-slicing]

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

Read more:

518 questions
0
votes
1 answer

Reversing order of second column only in for a 2x2 ND-Array

Right now I have a 2x2 ND-array, namely np.array([[93, 95], [84, 100], [99, 87]]). I would like to reverse the second column of the array into, such that I get: np.array([[93, 87], [84, 100], [99, 95]]). I tried the following code: grades =…
Willow
  • 3
  • 1
0
votes
1 answer

Python numpy: Add elements of a numpy array of arrays to elements of another array of arrays initialized to at the specified positions

Suppose we have a numpy array of numpy arrays of zeros as arr1=np.zeros((len(Train),(L)) where Train is a (dataset) numpy array of arrays of integers of fixed length. We also have another 1d numpy array, positions of length as len(Train). Now we…
0
votes
2 answers

Python numpy: Add elements of a list of array to elements of another list of arrays at the specified positions

Given arr1 = [[0,0,0,0],[0,0,0,0],[0,0,0,0] and arr2 = [[1,2],[3,4],[4,5]] and positions = [0,1,2] we wish to add elements of arr2 to elements of elements of arr1 at the indices specified by postions that is our answer should…
0
votes
1 answer

Problem with NumPy Arrays when creating Face Filters

I am currently working on a project where I detect a face with a haar cascade classifier. In the code, I have created Rois with a cv2 rectangle and the goal is to apply images to the ROI. On some images the code works on others it doesn´t, also…
0
votes
0 answers

Why does Python return inverted image channel?

I am trying to extract the K channel of a CMYK image using Python. However, the result is an inverted channel. Here is the code I am using: # Import Packages import numpy as np from PIL import Image # Define method def get_k_channel(filepath): …
0
votes
3 answers

Numpy: How to check if a number is the minimum/maximum among the previous K numbers?

I'm trying to automate a trading strategy which should enter/exit a long position when the current price is the minimum/maximum among the previous k prices. The result should contain 1 if the current number is maximum among previous k numbers, -1 if…
0
votes
0 answers

Numpy, subtract fixed values from all channels in a batch of images

I have 4 sequences of images, each formed by 5 RGB images with shape (10x10). Everything is represented by a numpy array with shape: batch.shape = (4, 5, 10, 10, 3) I need to apply some normalization operations on the array, where for each image, I…
Carlo
  • 1,321
  • 12
  • 37
0
votes
0 answers

Is there a way to access a numpy column by variable name?

I'm trying to generate a bunch of graphs automatically but struggling with correctly using np. Here's my code: var1 = np.array(X.keys()[fs.get_support()])[0] var2 = np.array(X.keys()[fs.get_support()])[1] plt.scatter(new_data_2genre.var1,…
Twee
  • 21
  • 1
0
votes
2 answers

Advanced 3d numpy array slicing with alternation

So, I want to slice my 3d array to skip the first 2 arrays and then return the next two arrays. And I want the slice to keep following this pattern, alternating skipping 2 and giving 2 arrays etc.. I have found a solution, but I was wondering if…
Tea
  • 87
  • 7
0
votes
2 answers

Slicing a huuuge 2D numpy ndarray - How to do this efficiently?

I have a numpy array of size (24, 131000). The first of these 24 columns contains an index corresponding to a number in the range [0, 25], for each of the 131000 rows. I want to slice this array to generate 26 separate arrays, each containing the…
0
votes
3 answers

How to add a 4X4 matrix values into a 6x6 matrix using numpy

suppose i have multiple 4x4 matrices which i want to add to a final 6x6 zero matrix by adding some of the values in the designated coordination. how would i do this. I throughout of adding slices to np.zero 6x6 matrix , but i believe this may be…
Elta
  • 5
  • 2
0
votes
1 answer

How to optimize a looped parameterized function call using numpy?

I am trying to convert a nested loop over a numpy array into a numpy-optimized implementation. The function being called inside the loop takes a 4D vector and a separate parameter, and outputs a 4D vector which is supposed to replace the old 4D…
0
votes
2 answers

What is the most efficient way to handle conversion from full to symmetric second order tensors using numpy?

I am processing symmetric second order tensors (of stress) using numpy. In order to transform the tensors I have to generate a fully populated tensor, do the transformation and then recover the symmetric tensor in the rotated frame. My input is a 2D…
0
votes
0 answers

Index a multi-dimensional numpy array by a variable that contains a list of slices

I have a piece of code that uses a sophisticated way of calculating slices for an array that used to work, but does not any more as of python 3.8.15. Can anyone tell me whats wrong with >>> a=np.zeros((100,100)) >>> b=[slice(5,95),slice(5,95)] >>>…
mfeldt
  • 61
  • 1
  • 3
0
votes
0 answers

Multiplication of 3d array using np.einsum

Hello I have just recently started working with python 3d arrays and I am not too familiar with how to multiply each block in the 3d array with all other blocks in the 3d array. I am working with a np array with shape (31, 22, 16). Let say my numpy…