33import math
44
55import pandas as pd
6- import json
76
87from statsmodels .stats .proportion import proportion_confint
9- import scipy .stats as st
108from sklearn .tree import DecisionTreeClassifier
119from sklearn .base import ClassifierMixin
1210
13- import sslearn
14-
1511
1612def safe_division (dividend , divisor , epsilon ):
1713 if divisor == 0 :
1814 return dividend / epsilon
1915 return dividend / divisor
2016
2117
22- def confidence_interval (X , hyp , y , alpha = .95 ):
18+ def confidence_interval (X , hyp , y , alpha = .95 ):
2319 data = hyp .predict (X )
2420
2521 successes = np .count_nonzero (data == y )
2622 trials = X .shape [0 ]
2723 li , hi = proportion_confint (successes , trials , alpha = 1 - alpha , method = "wilson" )
2824 return li , hi
29-
3025
3126
3227def choice_with_proportion (predictions , class_predicted , proportion , extra = 0 ):
@@ -69,6 +64,27 @@ def is_int(x):
6964 return isinstance (x , (int , np .integer )) and not isinstance (x , bool )
7065
7166
67+ def mode (y ):
68+ """Calculate the mode of a list of values
69+
70+ Parameters
71+ ----------
72+ y : array-like of shape (n_samples, n_estimators)
73+ array of values
74+
75+ Returns
76+ -------
77+ mode: array-like of shape (n_samples,)
78+ array of mode of each label
79+ count: array-like of shape (n_samples,)
80+ array of count of the mode of each label
81+ """
82+ array = pd .DataFrame (np .array (y ))
83+ mode = array .mode (axis = 0 ).loc [0 , :]
84+ count = array .apply (lambda x : x .value_counts ().max ())
85+ return mode .values , count .values
86+
87+
7288def check_n_jobs (n_jobs ):
7389 """Check `n_jobs` parameter according to the scikit-learn convention.
7490 From sktime: BSD 3-Clause
@@ -101,9 +117,10 @@ def calc_number_per_class(y_label):
101117 number_per_class = dict ()
102118 for c in classes :
103119 number_per_class [c ] = math .ceil (proportion [c ] * factor )
104-
120+
105121 return number_per_class
106122
123+
107124def check_classifier (base_classifier , can_be_list = True , collection_size = None ):
108125
109126 if base_classifier is None :
@@ -114,7 +131,7 @@ def check_classifier(base_classifier, can_be_list=True, collection_size=None):
114131 raise AttributeError (f"base_classifier is a list of classifiers, but its length ({ len (base_classifier )} ) is different from expected ({ collection_size } )" )
115132 for i , bc in enumerate (base_classifier ):
116133 base_classifier [i ] = check_classifier (bc , False )
117- return list (base_classifier ) # Transform to list
134+ return list (base_classifier ) # Transform to list
118135 else :
119136 if not isinstance (base_classifier , ClassifierMixin ):
120137 raise AttributeError (f"base_classifier must be a ClassifierMixin, but found { type (base_classifier )} " )
0 commit comments