# How can I compute confusion matrix using Python [closed]

My project is about "Extracting water and urban features from satellite images". I have extracted urban and water features using Python code. Now, I want to do accuracy assessment. I know two primary requirements for accuracy are reference data and predicted data. So, as reference data I have GeoTIFF file and as predicted data I have my extracted image (GeoTIFF format). I tried with accuracy assessment using Python but I am getting accuracy as 99% which is not possible. Can somebody suggest how I can achieve this using Python?

``````import numpy as np
import matplotlib.pyplot as plt
import cv2
from PIL import Image
import os
import gdal
import osr

srcband = gtif.GetRasterBand(1)
trans = gtif.GetGeoTransform()
proj = gtif.GetProjection()
#print(proj)
#print("1")
print("urban area")                       #Extracting urban area
for i in range(arr.shape):
for j in range(arr.shape):
if 0.6 <= arr[i,j] <= 1:
arr[i,j] = 1
else:
arr[i,j] = 0
plt.imshow(arr,cmap = 'gray')

cols = arr.shape
rows = arr.shape

driver = gdal.GetDriverByName('GTiff')

if arr.ndim == 2:
band_num = 1
else:
band_num = arr.shape

outRaster.SetGeoTransform(trans)
#srs = osr.SpatialReference()
#s1 = srs.ImportFromEPSG(4326)
#print(srs)

outRaster.SetProjection(proj)
#print(outRaster.GetProjection())

outband = outRaster.GetRasterBand(1)

outband.WriteArray(arr)
#print(data2)
outband.FlushCache()
outRaster=None

#Accuracy Assessment

tp=0
fp=0
tn=0
fn=0

for i in range(arr.shape):
for j in range(arr.shape):
if arr[i][j]==1 and 0.5=<arr1[i][j]<=1:
tp+=1
if arr[i][j]==0 and 0.0=<arr1[i][j]<=0.4:
fp+=1
if arr[i][j]==1 and 0.5=<arr1[i][j]<=1:
tn+=1
if arr[i][j]==0 and 0.0=<arr1[i][j]<=0.4:
fn+=1
print(tp,tn,fp,fn)
acc=(tp+tn)/(tp+tn+fp+fn)
print(acc*100)
``````
• Welcome to GIS SE. As a new user, please take the Tour. You state that you are "facing [an] issue", but not what that issue is. You haven't provided the code you are using that has the issue, or an indication of what code library you are using to access the GeoTIFF data. As it stands, this question is unclear, opinion-based, too broad, and missing a code attempt. Please Edit the question to focus it on the problem. – Vince Jul 13 '19 at 14:36
• Thank you for adding code, but please fix the formatting so that it's readable. There's a code format button in edit mode, or just add 4 spaces at the beginning of each line. – smiller Jul 13 '19 at 16:20
• It looks like you are extracting data from a raster, assigning it to a new raster, then testing against the same original raster using the same original limits (e.g. Value > 0.6). You don't have two sets of data, you just have one set, with tests. What is the known good or somehow verified data to test against? – smiller Jul 16 '19 at 4:21