Note
Click here to download the full example code
Compute quality index from confusion matrix¶
Compute different quality index (OA, Kappa and F1) directly from confusion matrix.
Import librairies¶
import numpy as np
from museotoolbox.stats import zonal_stats
from museotoolbox.datasets import load_historical_data
Load dataset¶
raster,vector = load_historical_data()
Compute mean and variance per polygon¶
mean,var = zonal_stats(raster,vector,'uniquefid',stats=['mean','var'])
print(mean.shape)
Out:
(17, 3)
Show mean value
print('For polygon 1 : ')
for band_idx,band in enumerate(['blue','green','red']):
print('Mean value in {} band is : {}'.format(band,mean[0,band_idx]))
Out:
For polygon 1 :
Mean value in blue band is : 117.25219743069641
Mean value in green band is : 109.25287356321839
Mean value in red band is : 79.49830966869506
Show variance value
print('For polygon 1 : ')
for band_idx,band in enumerate(['blue','green','red']):
print('Variance value in {} band is : {}'.format(band,var[0,band_idx]))
Out:
For polygon 1 :
Variance value in blue band is : 1286.7051591334339
Variance value in green band is : 1243.2599224390497
Variance value in red band is : 1012.830118846633
You can put in stats, every numpy function
For example here : mean, median, max, min
mean,median,amax,amin = zonal_stats(raster,vector,'uniquefid',stats=['mean','median','max','min'])
print('For polygon 1 : ')
for band_idx,band in enumerate(['blue','green','red']):
print('Min value in {} band is : {}'.format(band,amin[0,band_idx]))
print('Max value in {} band is : {}'.format(band,amax[0,band_idx]))
Out:
For polygon 1 :
Min value in blue band is : 32.0
Max value in blue band is : 196.0
Min value in green band is : 24.0
Max value in green band is : 186.0
Min value in red band is : 1.0
Max value in red band is : 155.0
Total running time of the script: ( 0 minutes 0.122 seconds)