Coverage for src/sensai/naive_bayes.py: 21%

53 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-13 22:17 +0000

1import collections 

2from math import log, exp 

3 

4import numpy as np 

5import pandas as pd 

6 

7from .vector_model import VectorClassificationModel 

8 

9 

10class CategoricalNaiveBayesVectorClassificationModel(VectorClassificationModel): 

11 """ 

12 Naive Bayes with categorical features 

13 """ 

14 def __init__(self, pseudo_count=0.1): 

15 """ 

16 :param pseudo_count: the count to add to each empirical count in order to avoid overfitting 

17 """ 

18 super().__init__() 

19 self.prior = None 

20 self.conditionals = None 

21 self.pseudoCount = pseudo_count 

22 

23 def _fit_classifier(self, x: pd.DataFrame, y: pd.DataFrame): 

24 self.prior = collections.defaultdict(lambda: 0) 

25 self.conditionals = collections.defaultdict(lambda: [collections.defaultdict(lambda: 0) for _ in range(x.shape[1])]) 

26 increment = 1 

27 for idxRow in range(x.shape[0]): 

28 cls = y.iloc[idxRow, 0] 

29 self.prior[cls] += increment 

30 for idxFeature in range(x.shape[1]): 

31 value = x.iloc[idxRow, idxFeature] 

32 self.conditionals[cls][idxFeature][value] += increment 

33 # get rid of defaultdicts, which are not picklable 

34 self.prior = dict(self.prior) 

35 self.conditionals = {k: [dict(d) for d in l] for k, l in self.conditionals.items()} 

36 

37 def _predict_class_probabilities(self, x: pd.DataFrame): 

38 results = [] 

39 for _, features in x.iterrows(): 

40 class_probabilities = np.zeros(len(self._labels)) 

41 for i, cls in enumerate(self._labels): 

42 lp = log(self._probability(self.prior, cls)) 

43 for idx_feature, value in enumerate(features): 

44 lp += log(self._probability(self.conditionals[cls][idx_feature], value)) 

45 class_probabilities[i] = exp(lp) 

46 class_probabilities /= np.sum(class_probabilities) 

47 results.append(class_probabilities) 

48 return pd.DataFrame(results, columns=self._labels) 

49 

50 def _probability(self, counts, value): 

51 value_count = counts.get(value, 0.0) 

52 total_count = sum(counts.values()) 

53 return (value_count + self.pseudoCount) / (total_count + self.pseudoCount) 

54 

55 def _predict(self, x: pd.DataFrame) -> pd.DataFrame: 

56 results = [] 

57 for _, features in x.iterrows(): 

58 best_cls = None 

59 best_lp = None 

60 for cls in self.prior: 

61 lp = log(self._probability(self.prior, cls)) 

62 for idxFeature, value in enumerate(features): 

63 lp += log(self._probability(self.conditionals[cls][idxFeature], value)) 

64 if best_lp is None or lp > best_lp: 

65 best_lp = lp 

66 best_cls = cls 

67 results.append(best_cls) 

68 return pd.DataFrame(results, columns=self.get_predicted_variable_names())