Downsampling a 2d numpy array in python

I am learning python myself and discovered a problem that requires sampling an object vector. I need help to figure out how to flush an array with an array. in the array, each row represents an image numbered from 0to 255. I was wondering how do you apply sampling to an array? I do not want scikit-learn, because I want to understand how to apply sampling. If you could explain the sample too, that would be awesome.

function vector 400x250

+4
source share
1 answer

If downsampling implies something like this , you can just slice the array. For example 1D:

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

The last line is equivalent:

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

with the designation of slicing as start:stop:step

Result:

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

[1 4 7 10]

For a two-dimensional 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 element from the original array.

Or, if you just want to select a sample of one dimension:

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

(400, 250)

(40, 250)

There are many other examples in the Numpy manual.

+10
source

Source: https://habr.com/ru/post/1619556/


All Articles