在python中对2d numpy数组进行下采样 [英] Downsampling a 2d numpy array in python

查看:2517
本文介绍了在python中对2d numpy数组进行下采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自学python,发现了一个问题,需要对特征向量进行下采样.我需要一些帮助来了解如何对数组进行下采样.数组中的每一行通过从0255的数字表示图像.我想知道您如何对阵列应用下采样?我不想scikit-learn,因为我想了解如何应用下采样. 如果您也可以解释下采样的话,那就太好了.

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.

特征向量为400x250

the feature vector is 400x250

推荐答案

如果使用缩减采样,则表示",您可以简单地对数组进行切片.对于一维示例:

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])

最后一行等效于:

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

切片符号为start:stop:step

结果:

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

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

[1 4 7 10]

[ 1 4 7 10]

对于2D数组,想法是相同的:

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)

(400, 250)

(40,250)

Numpy手册中还有许多其他示例

这篇关于在python中对2d numpy数组进行下采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆