use np.where to subset 3d array
Clash Royale CLAN TAG#URR8PPP
use np.where to subset 3d array
I am currently working on a satellite image, and I got a 3D array(6464,4064,3) like this
[[[ 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]]]
which is [latitude, longtitude, radiance] * 6464rows * 4064colomns
I want to subset my interested area according to latitude and longtitude,
so I use
new= np.where((hh[:,:,0]<=13)& (hh[:,:,0]>=7) & (hh[:,:,1]>=-76) & (hh[:,:,1]<=-64))
(hh is my 3d array)
and it comes out the shape of new array is (1156142, 3),
which means it becomes a 2d array and lost its colomns and rows.
I don't know how to why and don't know how to plot the radiance figure with unknown rows and colomns.
1 Answer
1
when np.where
feed only one argument, return the tuple condition.nonzero(), the indices where condition is True..
np.where
If you only want data points according to latitude and longtitude, index slicing is OK, more detail
Like:
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,)]
??
np.where(new_idx, hh, 0)
, this fix other as 0
or whatever and keep dim– BugKiller
Aug 12 at 4:37
np.where(new_idx, hh, 0)
0
ohhh, I finally figured out where the problem is!!! but now I have problem with the plot. Can I ask you how to plot the radiance with the x=latitude, y=longitude, and color of grids represent the radiance. I have tried using :plt.scatter(x=new[:,0], y=new[:,1], c=new[:,2]), however, there is no color difference.
– Lee Nola
Aug 12 at 5:00
plt.contourf
or plt.contour
may help.github.com/ageron/handson-ml/blob/… github.com/ageron/handson-ml/blob/…– BugKiller
Aug 12 at 5:58
plt.contourf
plt.contour
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
hi, thanks for answering. But still, the array after subsetting is a 2D array, I want to make it a 3D array, (i.e. np.shape(new_arr) --> (#no. of rows, # no. of cols, 3)), so that I can plot the radiance image using : plt.imshow(hh[,:,:0]). So can I ask for a furthur solution? Thanks a lot
– Lee Nola
Aug 12 at 4:14