使用np.where子集3d数组

时间:2018-08-12 01:50:40

标签: arrays python-2.7 numpy

我目前正在处理卫星图像,并且得到了这样的3D阵列(6464,4064,3)

[[[  3.61944046e+01  -6.91377335e+01  -1.50000001e-09]
  [  3.61942863e+01  -6.91287460e+01   1.32471696e-08]
  [  3.61941681e+01  -6.91197662e+01   9.53853174e-09]
  ..., 
  [  3.11809139e+01  -3.63661194e+01   6.60078259e-09]
  [  3.11785698e+01  -3.63582687e+01   6.60078259e-09]
  [  3.11762199e+01  -3.63504028e+01   6.40588294e-09]]

 [[  3.61873817e+01  -6.91379166e+01  -1.50000001e-09]
  [  3.61872635e+01  -6.91289215e+01   1.43964334e-08]
  [  3.61871490e+01  -6.91199493e+01   1.12178125e-08]
  ..., 
  [  3.11743488e+01  -3.63688583e+01   6.63846089e-09]
  [  3.11720028e+01  -3.63610077e+01   7.23354443e-09]
  [  3.11696529e+01  -3.63531456e+01   7.43190709e-09]]

 [[  3.61803589e+01  -6.91380997e+01  -1.50000001e-09]
  [  3.61802444e+01  -6.91291122e+01   1.69292687e-08]
  [  3.61801338e+01  -6.91201324e+01   1.33426239e-08]
  ..., 
  [  3.11677856e+01  -3.63715935e+01   7.35317940e-09]
  [  3.11654358e+01  -3.63637428e+01   6.95529767e-09]
  [  3.11630821e+01  -3.63558846e+01   7.15423853e-09]]

 ..., 
 [[ -5.02645159e+00  -7.61433792e+01  -1.50000001e-09]
  [ -5.02774668e+00  -7.61361847e+01   3.38870656e-08]
  [ -5.02903891e+00  -7.61290054e+01   3.38870656e-08]
  ..., 
  [ -9.27992916e+00  -4.86378708e+01   9.09282427e-09]
  [ -9.28078461e+00  -4.86308556e+01   9.09282427e-09]
  [ -9.28179646e+00  -4.86225281e+01   7.49361462e-09]]

 [[ -5.03337288e+00  -7.61447067e+01  -1.50000001e-09]
  [ -5.03466558e+00  -7.61375122e+01   3.04580183e-08]
  [ -5.03595591e+00  -7.61303253e+01   3.48006957e-08]
  ..., 
  [ -9.28699970e+00  -4.86376190e+01   8.94025476e-09]
  [ -9.28782177e+00  -4.86308937e+01   8.15083290e-09]
  [ -9.28873920e+00  -4.86233711e+01   8.34818881e-09]]

 [[ -5.04029608e+00  -7.61460190e+01  -1.50000001e-09]
  [ -5.04158545e+00  -7.61388321e+01   3.18825499e-08]
  [ -5.04287243e+00  -7.61316452e+01   3.26812319e-08]
  ..., 
  [ -9.29387188e+00  -4.86390038e+01   8.31999980e-09]
  [ -9.29480457e+00  -4.86313744e+01   8.51963478e-09]
  [ -9.29572582e+00  -4.86238594e+01   8.71926975e-09]]]

[纬度,经度,辐射率] * 6464行* 4064列

我想根据纬度和经度来划分我感兴趣的区域, 所以我用

new= np.where((hh[:,:,0]<=13)& (hh[:,:,0]>=7) & (hh[:,:,1]>=-76) & (hh[:,:,1]<=-64))

(hh是我的3d数组)

得出新数组的形状是(1156142,3), 这意味着它变成了二维数组,丢失了列和行。

我不知道为什么,也不知道如何绘制具有未知行和列的辐射图。

1 个答案:

答案 0 :(得分:0)

np.where仅提供一个参数return the tuple condition.nonzero(), the indices where condition is True.时。

如果仅需要根据纬度和经度的数据点,则可以进行索引切片,more detail

赞:

import numpy as np

hh = np.random.randn(6464, 4064, 3)

# boolean
new_idx = (hh[:, :, 0] <= 0.13)& (hh[:, :, 0] >= 0.07) & (hh[:, :, 1] >= -0.76) & \
        (hh[:, :, 1] <= -0.64)
print(new_idx.shape)    # (6464, 4064)

# Boolean array indexing
# data points (num, latitude, longtitude, radiance)
new_arr = hh[new_idx]
print(new_arr.shape)    # (23175, 3)

# rows, cols
new_where = np.where(new_idx)
print(type(new_where))  # <class 'tuple'>
print([x.shape for x in new_where]) # [(23175,), (23175,)]
相关问题