Code Monkey home page Code Monkey logo

confusion-matrix-classification's Introduction

confusion-matrix-classification

Calculate Precision & Recall & F-Measure & Accuracy with Confusion Matrix (TP, TN, FP, FN)

Confusion Matrix ::

Positive Prediction Negative Prediction
Positive Class True Positive (TP) False Negative (FN)
Negative Class False Positive (FP) True Negative (TN)

Precision quantifies Formula :

Precision = TruePositives / (TruePositives + FalsePositives)
Precision = TP / ( TP + FP )

Recall quantifies Formula :

Recall = TruePositives / (TruePositives + FalseNegatives)
Recall = TP / ( TP + FN )

F-Measure quantifies Formula :

F-Measure = (2 * Precision * Recall) / (Precision + Recall)

Accuracy quantifies Formula :

accuracy = (TruePositives + TrueNegative) / (TruePositives + FalsePositive + False Negative + TrueNegative)
accuracy = (TP + TN) / (TP + FP + FN + TN)

Code (python class) :

class confusionMatrixClassification:
	
	# Author : Amir Shokri
	# Email : [email protected]
	# Date : Dec 2021
	# git : https://github.com/amirshnll/confusion-matrix-classification/

	def __init__(self):
		self.tp = 0.0
		self.tf = 0.0
		self.fp = 0.0
		self.fn = 0.0

	def setTP(self, tp):
		self.tp = float(tp)

	def getTP(self):
		return float(self.tp)

	def setTF(self, tf):
		self.tf = float(tf)

	def getTF(self):
		return float(self.tf)

	def setFP(self, fp):
		self.fp = float(fp)

	def getFP(self):
		return float(self.fp)

	def setFN(self, fn):
		self.fn = float(fn)

	def getFN(self):
		return float(self.fn)

	def precision(self):
		try:
			return ( self.getTP() + self.getTF() ) / ( self.getTP() + self.getTF() + self.getFP() + self.getFN() )
		except:
			return -1 # error
			
	def accuracy(self):
		try:
			return self.getTP() / ( self.getTP() + self.getFP() )
		except:
			return -1 # error

	def recall(self):
		try:	
			return self.getTP() / ( self.getTP() + self.getFN() )
		except:
			return -1 # error
		
	def fmeasure(self):
		precision_value = self.precision()
		recall_value = self.recall()

		if precision_value == -1 or recall_value == -1:
			return -1 # error
		
		try:
			return ( 2 * precision_value ) / ( precision_value + recall_value )
		except:
			return -1 # error

Code (usage) :

cmc = confusionMatrixClassification()
cmc.setTP(90)
cmc.setFP(30)
cmc.setFN(10)

print("Accuracy : " + str(cmc.accuracy())) # 0.75
print("Precision : " + str(cmc.precision())) # 0.6923076923076923
print("Recall : " + str(cmc.recall())) # 0.9
print("F-Measure : " + str(cmc.fmeasure())) # 0.8695652173913042

confusion-matrix-classification's People

Contributors

amirshnll avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.