Advertisement
namemkazaza

Class

Feb 19th, 2021
1,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. class Node:
  2.     def __init__(self):
  3.         self.l = None
  4.         self.r = None
  5.         self.feature = None
  6.         self.threshold = -1
  7.         self.answer = None
  8.        
  9.     def H(self, X):
  10.         # ЭНТРОПИЯ
  11.         c = Counter(X)
  12.         s = 0
  13.         for v in c.values():
  14.             p = v / len(X)
  15.             s += math.log(p) * p
  16.         return -s
  17.        
  18.        
  19.     def Q(self, X, Xl, Xr):
  20.         # ФУНКЦИЯ ПОТЕРЬ
  21.         return self.H(X) - len(Xl) / len(X) * self.H(Xl)  - len(Xr) / len(X) * self.H(Xr)
  22.        
  23.        
  24.     def is_stop(self, y):
  25.         return len(Counter(y).keys()) <= 1
  26.    
  27.     def fit(self, X, y):
  28.         if self.is_stop(y):
  29.             self.answer = Counter(y).most_common()[0][0]
  30.             return
  31.        
  32.         max_q = -1
  33.         max_feature = None
  34.         max_threshold = -1
  35.        
  36.         for feature in X.columns:
  37.             for val in np.unique(X.values):
  38.                 left_cond = X[feature] <= val
  39.                 right_cond = X[feature] > val
  40.                
  41.                 q = self.Q(y, y[left_cond], y[right_cond])
  42.                 if q > max_q:
  43.                     max_q = q
  44.                     max_feature = feature
  45.                     max_threshold = val
  46.        
  47.         self.feature = max_feature
  48.         self.threshold = max_threshold
  49.         print(self.feature, self.threshold)
  50.        
  51.         self.l = Node()
  52.         self.r = Node()
  53.         left_cond = X[self.feature] <= self.threshold
  54.         right_cond = X[self.feature] > self.threshold
  55.         self.l.fit(X[left_cond], y[left_cond])
  56.         self.r.fit(X[right_cond], y[right_cond])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement