Questions tagged [numpy-slicing]

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

Read more:

518 questions
2
votes
2 answers

how to slice a 2d array based on indices given in another 2d array

I have an MxN array called A which stores the data I want. I have another M x N2 array B which stores array indices, and N2
Physicist
  • 2,848
  • 8
  • 33
  • 62
2
votes
3 answers

Slice an array into segments

Suppose I have an array [1,2,3,4,5,6,7,8], and the array is composed of two samples [1,2,3,4], and [5,6,7,8]. For each sample, I want to do a slicing window with window size n. And if there are not enough elements, pad the result with the last…
tczj
  • 438
  • 4
  • 17
2
votes
4 answers

numpy: slice away 2 columns

Consider the following data: 61 1 1 15.04 14.96 13.17 9.29 13.96 9.87 13.67 10.25 10.83 12.58 18.50 15.04 61 1 2 14.71 16.88 10.83 6.50 12.62 7.67 11.50 10.04 9.79 9.67 17.54 13.83 61 1 3 18.50 16.88 12.33 10.13 11.17 6.17 11.25 8.04 …
2020
  • 2,821
  • 2
  • 23
  • 40
2
votes
2 answers

Trying to understand syntax with index slicing in python matrix

I have been looking over and interpreting some sample code and am having some trouble understanding what is going on with a bit of slicing syntax. For some context I am working on a computational program that uses method of finite differences…
2
votes
1 answer

How is one supposed to use the arange function when indexing an ndarray?

Let's say I want to select a value from a different column for each row. Then, I might do something like this: a = np.arange(12).reshape(3, 4) columns = np.array([1, 2, 0]) a[np.arange(a.shape[0]), columns] It seems a bit 'ugly' to me to need to…
vandenheuvel
  • 349
  • 2
  • 13
2
votes
1 answer

Assigning values to a numpy array using indices and sub indices

This has bitten me a few times, and I can't tell if it's a bug or a feature. nums = np.arange(10) indx1 = np.array([2,4,6,8]) indx2 = np.array([0,3]) It looks like I can index nums either way nums[indx1][indx2], nums[indx1[indx2]] outputs…
I.P. Freeley
  • 885
  • 1
  • 9
  • 19
2
votes
1 answer

Slicing the channels of image and storing the channels into numpy array(same size as image). Plotting the numpy array not giving the original image

I separated the 3 channels of an colour image. I created a new NumPy array of the same size as the image, and stored the 3 channels of the image into 3 slices of the 3D NumPy array. After plotting the NumPy array, the plotted image is not same as…
2
votes
3 answers

Numpy array : NOT select specific rows or columns

I have a simple numpy array. I want to select all rows but 1st and 6th I tried: temp = np.array([1,2,3,4,5,6,7,8,9]) t = temp[~[0,5]] I get the following error: TypeError: bad operand type for unary ~: 'list' What is the correct way to do this?
Anuj Gupta
  • 6,328
  • 7
  • 36
  • 55
2
votes
1 answer

split 3D numpy to 3 diffrent arrays

I have numpy.array pf shape (64 , 64 , 64) I would like to split it on to 3 variables ,so x.shape ==> (64) y.shape ==> (64) z.shape ==> (64) as each dim represent voxels coordinate (x,y,z) , I tried use dsplit() but no luck. any suggestion?
2
votes
1 answer

Selecting a row interval according to a Column value in Pandas

Hi everyone I have a dataset that looks like this transferid value type 5545 100 X 5123 40 A 5566 35 A 5675 700 X 5235 1100 A 5616 350 A 5772 …
Jesus Rincon
  • 136
  • 9
1
vote
1 answer

How to extract chunks of a 2D numpy array that has been flattened

I would like to know the best way of extacting chunks of elements from a 2D numpy array that has been flattened. See example python code below which hopefully explains what I want to do a little better. import numpy as np nx = 5 nz = 7 numGPs =…
n1ck94
  • 41
  • 5
1
vote
2 answers

Selecting numpy array on the last axis

I have a 3D array and a 2D array of indices. How can I select on the last axis? import numpy as np # example array shape = (4,3,2) x = np.random.uniform(0,1, shape) # indices idx = np.random.randint(0,shape[-1], shape[:-1]) Here is a loop that…
guyguyguy12345
  • 539
  • 3
  • 11
1
vote
1 answer

I cannot run python runtests.py -v for testing numpy it giving me version issue

I want to run test for numpy library in my local machine but when I simple testing command like this command python runtests.py -v I am keep getting following error ` Building, see build.log... Traceback (most recent call last): File…
1
vote
1 answer

Summing elements in sliding window after applying function to last n elements

I currently have a dataframe formatted as such: a b c e f 2 3 4 5 1 9 8 6 4 6 1 2 2 2 3 9 8 8 9 1 3 6 8 6 2 . . . . . . . . . . 7 5 4 1 8 My goal is to apply an adaptive weight function over the last n(4) games in a sliding window. On…
1
vote
1 answer

Slicing numpy arrays in a for loop

I have a multidimentional numpy array of elasticities with one of the dimensions being "age-groups" (0-92 years) and the other "income-groups" (low/high income). I would like to create a table with the mean elasticities for each combination of…
Stata_user
  • 562
  • 3
  • 14