psyke 0.9.0.dev1__py3-none-any.whl → 0.9.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of psyke might be problematic. Click here for more details.

@@ -24,28 +24,3 @@ class PedagogicalExtractor(Extractor, ABC):
24
24
 
25
25
  def _extract(self, dataframe: pd.DataFrame) -> Theory:
26
26
  raise NotImplementedError('extract')
27
-
28
-
29
- class FairExtractor(PedagogicalExtractor, ABC):
30
-
31
- def __init__(self, extractor: Extractor, features: Iterable):
32
- super().__init__(extractor.predictor, extractor.discretization, extractor.normalization)
33
- self.features = features
34
- self.extractor = extractor
35
- # self.make_fair()
36
-
37
- # def extract(self, dataframe: pd.DataFrame) -> Theory:
38
- # self.theory = self.extractor.extract(dataframe)
39
- # return self.theory
40
-
41
- # def predict_why(self, data: dict[str, float], verbose: bool = True):
42
- # self.extractor.predict_why(data, verbose)
43
-
44
- # def predict_counter(self, data: dict[str, float], verbose: bool = True, only_first: bool = True):
45
- # self.extractor.predict_counter(data, verbose, only_first)
46
-
47
- # def _predict(self, dataframe: pd.DataFrame) -> Iterable:
48
- # return self.extractor.predict(dataframe)
49
-
50
- # def _brute_predict(self, dataframe: pd.DataFrame, criterion: str = 'corner', n: int = 2) -> Iterable:
51
- # return self.extractor.brute_predict(dataframe, criterion, n)
@@ -72,12 +72,20 @@ class FairTree:
72
72
  right = self._grow_tree(X[right_idxs], y[right_idxs], depth + 1)
73
73
  return Node(best_feature, best_threshold, left, right)
74
74
 
75
+ @staticmethod
76
+ def generate_thresholds(X, y):
77
+ sorted_indices = np.argsort(X)
78
+ X = np.array(X)[sorted_indices]
79
+ y = np.array(y)[sorted_indices]
80
+ return np.array([(X[i] + X[i - 1]) / 2.0 for i in range(1, len(X)) if y[i] != y[i - 1]])
81
+
75
82
  def _best_split(self, X, y):
76
83
  best_gain = -float('inf')
77
84
  split_idx, split_threshold = None, None
78
85
 
79
86
  for feature in [feature for feature in X.columns if feature not in self.protected_attr]:
80
- for threshold in np.unique(np.quantile(X[feature], np.linspace(0, 1, num=25))):
87
+ # for threshold in np.unique(np.quantile(X[feature], np.linspace(0, 1, num=25))):
88
+ for threshold in self.generate_thresholds(X[feature], y):
81
89
  left_idxs = X[feature] <= threshold
82
90
  right_idxs = X[feature] > threshold
83
91
 
@@ -1,15 +1,9 @@
1
1
  import copy
2
- from collections import Iterable
3
2
  from typing import Union, Any
4
- import pandas as pd
5
- from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
6
- from tuprolog.core import clause, Var, Struct
7
- from tuprolog.theory import Theory, mutable_theory
8
3
 
9
4
  from psyke.extraction.cart import FairTreeClassifier, FairTreeRegressor, LeafSequence, LeafConstraints
10
5
  from psyke.extraction.cart.CartPredictor import CartPredictor
11
- from psyke.schema import LessThan, GreaterThan, SchemaException, DiscreteFeature, Value
12
- from psyke.utils.logic import create_variable_list, create_head, create_term
6
+ from psyke.schema import LessThan, GreaterThan, SchemaException, Value
13
7
 
14
8
 
15
9
  class FairTreePredictor(CartPredictor):
@@ -17,7 +11,7 @@ class FairTreePredictor(CartPredictor):
17
11
  A wrapper for fair decision and regression trees of psyke.
18
12
  """
19
13
 
20
- def __init__(self, predictor: Union[FairTreeClassifier, FairTreeRegressor] = DecisionTreeClassifier(),
14
+ def __init__(self, predictor: Union[FairTreeClassifier, FairTreeRegressor] = FairTreeClassifier(),
21
15
  discretization=None, normalization=None):
22
16
  super().__init__(predictor, discretization, normalization)
23
17
 
@@ -19,6 +19,7 @@ class REAL(PedagogicalExtractor):
19
19
 
20
20
  def __init__(self, predictor, discretization: Iterable[DiscreteFeature]):
21
21
  super().__init__(predictor, discretization)
22
+ self._ignore_feature = []
22
23
  self._ruleset: IndexedRuleSet = IndexedRuleSet()
23
24
 
24
25
  @property
@@ -87,13 +88,20 @@ class REAL(PedagogicalExtractor):
87
88
  return self._create_ruleset(dataset)
88
89
 
89
90
  def _internal_predict(self, sample: pd.Series):
90
- x = [index for index, rule in self._ruleset.flatten() if REAL._rule_from_example(sample) in rule]
91
+ x = [index for index, rule in self._ruleset.flatten() if self._rule_from_example(sample) in rule]
91
92
  return x[0] if x else None
92
93
 
93
- @staticmethod
94
- def _rule_from_example(sample: pd.Series) -> Rule:
94
+ def make_fair(self, features: Iterable[str]):
95
+ self._ignore_feature = [list(i.admissible_values.keys()) for i in self.discretization if i.name in features] \
96
+ if self.discretization else [features]
97
+ self._ignore_feature = [feature for features in self._ignore_feature for feature in features]
98
+ self._get_or_set.cache_clear()
99
+
100
+ def _rule_from_example(self, sample: pd.Series) -> Rule:
95
101
  true_predicates, false_predicates = [], []
96
102
  for feature, value in sample.items():
103
+ if feature in self._ignore_feature:
104
+ continue
97
105
  true_predicates.append(str(feature)) if value == 1 else false_predicates.append(str(feature))
98
106
  return Rule(true_predicates, false_predicates)
99
107
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: psyke
3
- Version: 0.9.0.dev1
3
+ Version: 0.9.1
4
4
  Summary: Python-based implementation of PSyKE, i.e. a Platform for Symbolic Knowledge Extraction
5
5
  Home-page: https://github.com/psykei/psyke-python
6
6
  Author: Matteo Magnini
@@ -4,10 +4,10 @@ psyke/clustering/__init__.py,sha256=36MokTVwwWR_-o0mesvXHaYEYVTK2pn2m0ZY4G3Y3qU,
4
4
  psyke/clustering/utils.py,sha256=S0YwCKyHVYp9qUAQVzCMrTwcQFPJ5TD14Jwn10DE-Z4,1616
5
5
  psyke/clustering/cream/__init__.py,sha256=W6k7vdjuUdA_azYA4vb5JtpWrofhDJ0DbM2jsnRKzfw,2994
6
6
  psyke/clustering/exact/__init__.py,sha256=s4MPvGZ6gle3X9WH3YFHOEdinGcXIXh-7EFRcElWzsQ,5275
7
- psyke/extraction/__init__.py,sha256=wZxazCtNfJXIebsjma8aAk0dZPyiKHmzeVcqTfZDyFQ,1978
7
+ psyke/extraction/__init__.py,sha256=Q0i6wMzCdU7CkxhzWoD8H_a6XId6bfEx6LZbSJmTqm0,936
8
8
  psyke/extraction/cart/CartPredictor.py,sha256=YhEuaENLWixu379sIXZkFeCNc8GBnxLnR6TPCQR7sps,5743
9
- psyke/extraction/cart/FairTree.py,sha256=69JtbODGi9HN4AhcjuaF3e9csgBOrQoNaf1n6I0YnOY,7187
10
- psyke/extraction/cart/FairTreePredictor.py,sha256=kGHNLQ_HL2fWMe7g7pye7E0EmfPKPPFILklDHguJGvc,2640
9
+ psyke/extraction/cart/FairTree.py,sha256=49ciVmqgSa6hNL2axYi-oN4DSsqrHdewH76TTYiR8x0,7529
10
+ psyke/extraction/cart/FairTreePredictor.py,sha256=7z4oLqflkRMqqVW_UIlrGsQrvROM4sXUfY7LPQJ662g,2321
11
11
  psyke/extraction/cart/__init__.py,sha256=SsjAJiL4n6q_GNR6H8PNfhTkAZ67Ka7NRvVRxCULBhQ,3191
12
12
  psyke/extraction/hypercubic/__init__.py,sha256=SK-I9IPQEdpYVTkFGa8No803QMwYSqgTTzinry4KLew,10896
13
13
  psyke/extraction/hypercubic/hypercube.py,sha256=s1fuGOZfN2ZE21C7f6-b1T3Ta_934c4rwDLD_pBWwFk,25847
@@ -20,7 +20,7 @@ psyke/extraction/hypercubic/gridex/__init__.py,sha256=_g_JC6eFKLeg_CtkQawsUpVUAV
20
20
  psyke/extraction/hypercubic/gridrex/__init__.py,sha256=h9usK5tFqd6ngBmRydsgkfQ1jlcQKj2uG72Tr1puFHk,595
21
21
  psyke/extraction/hypercubic/hex/__init__.py,sha256=553AZjOT9thfqDGtVDI6WtgYNex2Y6dg53cEyuf7Q80,4805
22
22
  psyke/extraction/hypercubic/iter/__init__.py,sha256=bb0neiPcNlyyr-OUUjgw4vdkehnAsoyJzVJ88jAHtQ8,10233
23
- psyke/extraction/real/__init__.py,sha256=t3wrY4dEmZK3PhGexxHh1bhlCr4vvZ1IS6vDZoHyZjw,5379
23
+ psyke/extraction/real/__init__.py,sha256=zAE_syurDqmFiopD5oLeIs9bROiuXy06wxoHmVqAhCA,5836
24
24
  psyke/extraction/real/utils.py,sha256=4NNL15Eu7cmkG9b29GBP6CKgMTV1cmiJVS0k1MbWpIs,2148
25
25
  psyke/extraction/trepan/__init__.py,sha256=H8F_wpFLPcfyx2tgOOno8FwUomxfVxVl1vxlb0ClP1g,6931
26
26
  psyke/extraction/trepan/utils.py,sha256=iSUJ1ooNQT_VO1KfBZuIUeUsyUbGdQf_pSEE87vMeQg,2320
@@ -35,8 +35,8 @@ psyke/utils/logic.py,sha256=ioP25WMTYNYEzaRDNDe3kGNWqZ6DA_63t19d-ky_2kM,12227
35
35
  psyke/utils/metrics.py,sha256=Oo5BOonOSfo0qYsXWT5dmypZ7jiStByFC2MKEU0uMHg,2250
36
36
  psyke/utils/plot.py,sha256=dE8JJ6tQ0Ezosid-r2jqAisREjFe5LqExRzsVi5Ns-c,7785
37
37
  psyke/utils/sorted.py,sha256=C3CPW2JisND30BRk5c1sAAHs3Lb_wsRB2qZrYFuRnfM,678
38
- psyke-0.9.0.dev1.dist-info/licenses/LICENSE,sha256=KP9K6Hgezf_xdMFW7ORyKz9uA8Y8k52YJn292wcP-_E,11354
39
- psyke-0.9.0.dev1.dist-info/METADATA,sha256=YQQ4BWoNe-23RnTWikpxeITjxypGFYQC6pWZmVd1XDA,8394
40
- psyke-0.9.0.dev1.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
41
- psyke-0.9.0.dev1.dist-info/top_level.txt,sha256=q1HglxOqqoIRukFtyis_ZNHczZg4gANRUPWkD7HAUTU,6
42
- psyke-0.9.0.dev1.dist-info/RECORD,,
38
+ psyke-0.9.1.dist-info/licenses/LICENSE,sha256=KP9K6Hgezf_xdMFW7ORyKz9uA8Y8k52YJn292wcP-_E,11354
39
+ psyke-0.9.1.dist-info/METADATA,sha256=wVhKaa9Y2XGpbwfjuuDFtLCB0LxrK916GmhkmzbLHHU,8389
40
+ psyke-0.9.1.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
41
+ psyke-0.9.1.dist-info/top_level.txt,sha256=q1HglxOqqoIRukFtyis_ZNHczZg4gANRUPWkD7HAUTU,6
42
+ psyke-0.9.1.dist-info/RECORD,,