Advertisement
Dimaush

Untitled

May 14th, 2023
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. import numpy as np
  2.  
  3.  
  4. def evaluate_measures(sample):
  5.     n_samples = len(sample)
  6.     n_classes = 0
  7.     classes = dict()
  8.     for c in sample:
  9.         if c in classes:
  10.             classes[c] += 1
  11.         else:
  12.             classes[c] = 1
  13.             n_classes += 1
  14.     probs = np.zeros(n_classes)
  15.     for i, c in zip(range(n_classes), classes):
  16.         probs[i] = classes[c] / n_samples
  17.     gini = 1
  18.     entropy = 0
  19.     error = 1
  20.     for p in probs:
  21.         gini -= p * p
  22.         entropy -= p * np.log(p)
  23.         error = min(error, 1 - p)
  24.     measures = {'gini': gini, 'entropy': entropy, 'error': error}
  25.     return measures
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement