Python Forum
N-Dim array manipulation in a loop, getting IndexError: too many indices for array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
N-Dim array manipulation in a loop, getting IndexError: too many indices for array
#1
Hello people, good morning!!

In my implementation I`m trying to manipulation N-Dim. array on a looping to calculate a covariance between 2 variables from this N-Dim. array.

To explain better, I have one raster file containing 4 bands (R, G, B, Nir). On my implementation I read raster file, extract rows, columns and number of bands. After that I read the raster file as array, according code below.

My goal on this algorithm is to calculate the covariance for R, G, B bands in relation Nir band in the same looping. For example:
Cov_1 = np.cov(R, Nir)

Cov_2 = np.cov(G, Nir)

Cov_3 = np.cov(B, Nir)

The algorithm has implemented in python is:

from osgeo import gdal import numpy as np ds = gdal.Open('Sun_Glint_Sample_.tif') rows = ds.RasterYSize cols = ds.RasterXSize bands = ds.RasterCount cov = [] for i in range(bands): i += 1 data_ds = ds.GetRasterBand(i).ReadAsArray(0, 0, cols, rows).ravel() cov = np.cov(data_ds[:, i], data_ds[:,4], bias = True) print('Dimension: ', data_ds.ndim) print('Shape: ', data_ds.shape) print('Array: ', data_ds[:, i]) print('Covariance: ', cov)

Below the information printed before to calculate the covariance:


Output:
runfile('D:/CESAR_PHD/10.Lyzenga/Sun_glint_removal_v04.py', wdir='D:/CESAR_PHD/10.Lyzenga') Dimension: 1 Shape: (417944,) Array: [135 123 94 ... 31 57 77] Dimension: 1 Shape: (417944,) Array: [191 181 139 ... 49 93 128] Dimension: 1 Shape: (417944,) Array: [176 164 126 ... 42 78 107] Dimension: 1 Shape: (417944,) Array: [91 98 69 ... 12 19 33]
Below the error printed when I tried calculated the covariance:
Error:
File "D:/CESAR_PHD/10.Lyzenga/Sun_glint_removal_v04.py", line 22, in <module> cov = np.cov(data_ds[:, i], data_ds[:,4], bias = True) IndexError: too many indices for array
Thank you for help me!!
Reply
#2
Try the following code:

nir_flattened = ds.GetRasterBand(bands - 1).ReadAsArray(0, 0, cols, rows).ravel() for i in range(bands - 1): data_ds_flattened = ds.GetRasterBand(i).ReadAsArray(0, 0, cols, rows).ravel() cov = np.cov(data_ds_flattened, nir_flattened, bias=True) print('Covariance: ', cov) # expected cov is 2 x 2 matrix
But, I am not sure that: 1) bands counting starts from 0, 2) would it possible to read bands in arbitrary order, as I did; probably something like .seek method should be applied first.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Big Grin [solved] how to associate value in an array? paul18fr 9 3,709 Aug-26-2025, 05:44 AM
Last Post: paul18fr
  Numpy, array(2d,bool), flipping regions. MvGulik 2 1,583 Oct-27-2024, 11:06 AM
Last Post: MvGulik
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 2,208 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  ValueError: could not broadcast input array from shape makingwithheld 1 5,376 Jul-06-2024, 03:02 PM
Last Post: paul18fr
  python code to calculate mean of an array of numbers using numpy viren 3 2,083 May-29-2024, 04:49 PM
Last Post: Gribouillis
  Writing a cycle to find the nearest point from the array Tysrusko 0 1,292 May-10-2024, 11:49 AM
Last Post: Tysrusko
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 4,746 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 1,868 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 12,702 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 1,435 Jan-14-2024, 11:35 PM
Last Post: Gribouillis

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.