Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downsampling a 2d numpy array in python

I'm self learning python and have found a problem which requires down sampling a feature vector. I need some help understanding how down-sampling a array. in the array each row represents an image by being number from 0 to 255. I was wonder how you apply down-sampling to the array? I don't want to scikit-learn because I want to understand how to apply down-sampling. If you could explain down-sampling too that would be amazing thanks.

the feature vector is 400x250

like image 208
Neo Streets Avatar asked Dec 11 '15 19:12

Neo Streets


People also ask

How do you sort a 2D NumPy array in descending order in python?

Sort the rows of a 2D array in descending order The code axis = 1 indicates that we'll be sorting the data in the axis-1 direction, and by using the negative sign in front of the array name and the function name, the code will sort the rows in descending order.

How do I reduce the size of an array in NumPy?

You can use numpy. squeeze() to remove all dimensions of size 1 from the NumPy array ndarray . squeeze() is also provided as a method of ndarray .


1 Answers

If with downsampling you mean something like this, you can simply slice the array. For a 1D example:

import numpy as np a = np.arange(1,11,1) print(a) print(a[::3]) 

The last line is equivalent to:

print(a[0:a.size:3]) 

with the slicing notation as start:stop:step

Result:

[ 1 2 3 4 5 6 7 8 9 10]

[ 1 4 7 10]

For a 2D array the idea is the same:

b = np.arange(0,100) c = b.reshape([10,10]) print(c[::3,::3]) 

This gives you, in both dimensions, every third item from the original array.

Or, if you only want to down sample a single dimension:

d = np.zeros((400,250)) print(d.shape) e = d[::10,:] print(e.shape)  

(400, 250)

(40, 250)

The are lots of other examples in the Numpy manual

like image 121
Bart Avatar answered Sep 22 '22 00:09

Bart