File size: 3,724 Bytes
eb90a8a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | """
Module with base class for analysis of confusion information
"""
import os
from abc import ABC, abstractmethod
import numpy as np
import sklearn.metrics as metrics
class ConfusionAnalysis(ABC):
"""
Base class for analysis of confusion information
It performs intialization common for child classes
and provides utility functions for them
"""
@classmethod
def writeLargeMatrixInt(cls, m, f):
"""
Write large matrix to file
Parameters:
- m -- matrix to write as numpy array of floats
- f -- file to write the matrix
"""
_n = m.shape[0]
_s = " "
for _i in range(_n):
_s += "{:5d} ".format(_i)
f.write(_s + "\n")
for _i in range(_n):
_s = "{:3d} ".format(_i)
for _j in range(_n):
_s += "{:5d} ".format(m[_i, _j])
f.write(_s + "\n")
@classmethod
def writeLargeMatrixPct(cls, m, f, scale):
"""
Write large matrix to file
Parameters:
- m -- matrix to write as numpy array of floats
- f -- file to write the matrix
- scale -- multiplier to scale elements of matrix
"""
_n = m.shape[0]
_s = " "
for _i in range(_n):
_s += "{:4d} ".format(_i)
f.write(_s + "\n")
for _i in range(_n):
_s = "{:3d} ".format(_i)
for _j in range(_n):
_s += "{:4.1f} ".format(scale * m[_i, _j])
f.write(_s + "\n")
def __init__(self, probabilities, labels, solutions,
problems, extreme):
"""
Initialize base class
for analysis and reporting confusion information
Parameters:
- probabilities - classification probabilities as numpy array
- labels - labels of samples as numpy array
- solutions - list of sub-lists of file names of problem solutions
Each sub-list contains sample names of one problem
All sublists are part of the whole list of sample names
- problems - list of names of problems
- extreme - specification of number of cases to be reported
as extreme ones;
* if extreme < 1.0 it is a fraction of number of problems
* if extreme >= 1 it is the number of problems
"""
super(ConfusionAnalysis, self).__init__()
self.probabilities = probabilities
self.labels = labels
self._n_samples = self.labels.shape[0]
self.problems = problems
self._n_problems = len(self.problems)
self.extreme = extreme
#Number of problems to reports as the best or worst
self._n_extreme_cases = int(extreme) if extreme >= 1 else \
max(2, int(extreme * float(self._n_problems)))
self.solutions = solutions
#Directory for writing confusion reports to
self.report_dir = "confusion_report"
if not os.path.exists(self.report_dir):
os.makedirs(self.report_dir)
#Name of file to write large confusion matrices
self._conf_mat_fn = "confusion_matrix.lst"
self.predictions = self.getPredictions()
self.conf_mat = metrics.confusion_matrix(
self.labels, self.predictions)
@abstractmethod
def getPredictions(self):
"""
Abstract method for getting classification predictions
Returns: classification predictions
"""
raise NotImplementedError()
#---------------- End of class ConfusionAnalysis ----------------------
|