psyke 0.8.7__py3-none-any.whl → 0.8.9__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.
- psyke/__init__.py +3 -3
- psyke/extraction/cart/__init__.py +33 -15
- psyke/extraction/cart/predictor.py +15 -5
- psyke/schema/__init__.py +12 -5
- {psyke-0.8.7.dist-info → psyke-0.8.9.dist-info}/METADATA +1 -1
- {psyke-0.8.7.dist-info → psyke-0.8.9.dist-info}/RECORD +9 -9
- {psyke-0.8.7.dist-info → psyke-0.8.9.dist-info}/LICENSE +0 -0
- {psyke-0.8.7.dist-info → psyke-0.8.9.dist-info}/WHEEL +0 -0
- {psyke-0.8.7.dist-info → psyke-0.8.9.dist-info}/top_level.txt +0 -0
psyke/__init__.py
CHANGED
|
@@ -256,14 +256,14 @@ class Extractor(EvaluableModel, ABC):
|
|
|
256
256
|
[Extractor.ClassificationScore.F1])[Extractor.ClassificationScore.F1][-1]
|
|
257
257
|
|
|
258
258
|
@staticmethod
|
|
259
|
-
def cart(predictor, max_depth: int = 3, max_leaves: int = 3,
|
|
259
|
+
def cart(predictor, max_depth: int = 3, max_leaves: int = 3, max_features=None,
|
|
260
260
|
discretization: Iterable[DiscreteFeature] = None, normalization=None, simplify: bool = True) -> Extractor:
|
|
261
261
|
"""
|
|
262
262
|
Creates a new Cart extractor.
|
|
263
263
|
"""
|
|
264
264
|
from psyke.extraction.cart import Cart
|
|
265
|
-
return Cart(predictor, max_depth, max_leaves,
|
|
266
|
-
simplify=simplify)
|
|
265
|
+
return Cart(predictor, max_depth, max_leaves, max_features,
|
|
266
|
+
discretization=discretization, normalization=normalization, simplify=simplify)
|
|
267
267
|
|
|
268
268
|
@staticmethod
|
|
269
269
|
def divine(predictor, k: int = 5, patience: int = 15, close_to_center: bool = True,
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
from abc import ABC
|
|
2
|
+
|
|
1
3
|
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
|
|
2
4
|
|
|
3
5
|
from psyke.extraction import PedagogicalExtractor
|
|
@@ -14,26 +16,28 @@ import pandas as pd
|
|
|
14
16
|
TREE_SEED = get_default_random_seed()
|
|
15
17
|
|
|
16
18
|
|
|
17
|
-
class Cart(PedagogicalExtractor):
|
|
19
|
+
class Cart(PedagogicalExtractor, ABC):
|
|
18
20
|
|
|
19
|
-
def __init__(self, predictor, max_depth: int = 3, max_leaves: int = None,
|
|
21
|
+
def __init__(self, predictor, max_depth: int = 3, max_leaves: int = None, max_features=None,
|
|
20
22
|
discretization: Iterable[DiscreteFeature] = None,
|
|
21
23
|
normalization=None, simplify: bool = True):
|
|
22
24
|
super().__init__(predictor, discretization, normalization)
|
|
23
25
|
self._cart_predictor = CartPredictor(normalization=normalization)
|
|
24
26
|
self.depth = max_depth
|
|
25
27
|
self.leaves = max_leaves
|
|
28
|
+
self.max_features = max_features
|
|
26
29
|
self._simplify = simplify
|
|
27
30
|
|
|
28
|
-
def _create_body(self, variables: dict[str, Var],
|
|
31
|
+
def _create_body(self, variables: dict[str, Var], conditions: LeafConstraints) -> Iterable[Struct]:
|
|
29
32
|
results = []
|
|
30
|
-
for feature_name,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
for feature_name, cond_list in conditions.items():
|
|
34
|
+
for condition in cond_list:
|
|
35
|
+
features = [d for d in self.discretization if feature_name in d.admissible_values]
|
|
36
|
+
feature: DiscreteFeature = features[0] if len(features) > 0 else None
|
|
37
|
+
results.append(create_term(variables[feature_name], condition) if feature is None else
|
|
38
|
+
create_term(variables[feature.name],
|
|
39
|
+
feature.admissible_values[feature_name],
|
|
40
|
+
isinstance(condition, GreaterThan)))
|
|
37
41
|
return results
|
|
38
42
|
|
|
39
43
|
@staticmethod
|
|
@@ -41,11 +45,12 @@ class Cart(PedagogicalExtractor):
|
|
|
41
45
|
simplified = [nodes.pop(0)]
|
|
42
46
|
while len(nodes) > 0:
|
|
43
47
|
first_node = nodes[0][0]
|
|
44
|
-
for
|
|
45
|
-
|
|
46
|
-
|
|
48
|
+
for k, conditions in first_node.items():
|
|
49
|
+
for condition in conditions:
|
|
50
|
+
if all(k in node[0] and condition in node[0][k] for node in nodes):
|
|
51
|
+
[node[0][k].remove(condition) for node in nodes]
|
|
47
52
|
simplified.append(nodes.pop(0))
|
|
48
|
-
return simplified
|
|
53
|
+
return [({k: v for k, v in rule.items() if v != []}, prediction) for rule, prediction in simplified]
|
|
49
54
|
|
|
50
55
|
def _create_theory(self, data: pd.DataFrame) -> Theory:
|
|
51
56
|
new_theory = mutable_theory()
|
|
@@ -66,13 +71,26 @@ class Cart(PedagogicalExtractor):
|
|
|
66
71
|
|
|
67
72
|
def _extract(self, data: pd.DataFrame) -> Theory:
|
|
68
73
|
tree = DecisionTreeClassifier if isinstance(data.iloc[0, -1], str) else DecisionTreeRegressor
|
|
69
|
-
self._cart_predictor.predictor = tree(random_state=TREE_SEED, max_depth=self.depth,
|
|
74
|
+
self._cart_predictor.predictor = tree(random_state=TREE_SEED, max_depth=self.depth,
|
|
75
|
+
max_leaf_nodes=self.leaves, max_features=self.max_features)
|
|
70
76
|
self._cart_predictor.predictor.fit(data.iloc[:, :-1], data.iloc[:, -1])
|
|
71
77
|
return self._create_theory(data)
|
|
72
78
|
|
|
73
79
|
def _predict(self, dataframe: pd.DataFrame) -> Iterable:
|
|
74
80
|
return self._cart_predictor.predict(dataframe)
|
|
75
81
|
|
|
82
|
+
def predict_why(self, data: dict[str, float], verbose=True):
|
|
83
|
+
prediction = None
|
|
84
|
+
conditions = {}
|
|
85
|
+
if self.normalization is not None:
|
|
86
|
+
data = {k: v * self.normalization[k][1] + self.normalization[k][0] if k in self.normalization else v
|
|
87
|
+
for k, v in data.items()}
|
|
88
|
+
for conditions, prediction in self._cart_predictor:
|
|
89
|
+
if all(all(interval.is_in(data[variable]) for interval in intervals)
|
|
90
|
+
for variable, intervals in conditions.items()):
|
|
91
|
+
break
|
|
92
|
+
return prediction, conditions
|
|
93
|
+
|
|
76
94
|
@property
|
|
77
95
|
def n_rules(self) -> int:
|
|
78
96
|
return self._cart_predictor.n_leaves
|
|
@@ -2,9 +2,9 @@ from collections import Iterable
|
|
|
2
2
|
from typing import Union, Any
|
|
3
3
|
import numpy as np
|
|
4
4
|
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
|
|
5
|
-
from psyke.schema import Value, LessThan, GreaterThan
|
|
5
|
+
from psyke.schema import Value, LessThan, GreaterThan, SchemaException
|
|
6
6
|
|
|
7
|
-
LeafConstraints =
|
|
7
|
+
LeafConstraints = dict[str, list[Value]]
|
|
8
8
|
LeafSequence = Iterable[tuple[LeafConstraints, Any]]
|
|
9
9
|
|
|
10
10
|
|
|
@@ -25,8 +25,17 @@ class CartPredictor:
|
|
|
25
25
|
if self.normalization is not None:
|
|
26
26
|
thresholds = [threshold * self.normalization[feature][1] + self.normalization[feature][0]
|
|
27
27
|
for feature, threshold in zip(features, thresholds)]
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
cond_dict = {}
|
|
29
|
+
for feature, condition, threshold in zip(features, conditions, thresholds):
|
|
30
|
+
cond = LessThan(threshold) if condition else GreaterThan(threshold)
|
|
31
|
+
if feature in cond_dict:
|
|
32
|
+
try:
|
|
33
|
+
cond_dict[feature][-1] *= cond
|
|
34
|
+
except SchemaException:
|
|
35
|
+
cond_dict[feature].append(cond)
|
|
36
|
+
else:
|
|
37
|
+
cond_dict[feature] = [cond]
|
|
38
|
+
return cond_dict
|
|
30
39
|
|
|
31
40
|
def __get_leaves(self) -> Iterable[int]:
|
|
32
41
|
return [i for i, (left_child, right_child) in enumerate(zip(
|
|
@@ -39,7 +48,8 @@ class CartPredictor:
|
|
|
39
48
|
else:
|
|
40
49
|
return self._predictor.tree_.value[node]
|
|
41
50
|
|
|
42
|
-
def __path(self, node: int, path=
|
|
51
|
+
def __path(self, node: int, path=None) -> Iterable[(int, bool)]:
|
|
52
|
+
path = [] if path is None else path
|
|
43
53
|
if node == 0:
|
|
44
54
|
return path
|
|
45
55
|
father = list(self._left_children if node in self._left_children else self._right_children).index(node)
|
psyke/schema/__init__.py
CHANGED
|
@@ -3,20 +3,27 @@ import math
|
|
|
3
3
|
from typing import Callable
|
|
4
4
|
from psyke.utils import get_int_precision
|
|
5
5
|
|
|
6
|
+
|
|
7
|
+
class SchemaException(Exception):
|
|
8
|
+
|
|
9
|
+
def __init__(self, message: str):
|
|
10
|
+
super().__init__(message)
|
|
11
|
+
|
|
12
|
+
|
|
6
13
|
_EMPTY_INTERSECTION_EXCEPTION: Callable = lambda x, y: \
|
|
7
|
-
|
|
14
|
+
SchemaException(f"Empty intersection between two Value: {str(x)} and {str(y)}")
|
|
8
15
|
|
|
9
16
|
_NOT_IMPLEMENTED_INTERSECTION: Callable = lambda x, y: \
|
|
10
|
-
|
|
17
|
+
SchemaException(f"Not implemented intersection between: {str(x)} and {str(y)}")
|
|
11
18
|
|
|
12
19
|
_OPERATION_WITH_WRONG_TYPE: Callable = lambda x, y: \
|
|
13
|
-
|
|
20
|
+
SchemaException("Calling method with wrong type argument: " + str(x) + ' and ' + str(y))
|
|
14
21
|
|
|
15
22
|
_EMPTY_UNION_EXCEPTION: Callable = lambda x, y: \
|
|
16
|
-
|
|
23
|
+
SchemaException(f"Empty union between two Value: {str(x)} and {str(y)}")
|
|
17
24
|
|
|
18
25
|
_NOT_IMPLEMENTED_UNION: Callable = lambda x, y: \
|
|
19
|
-
|
|
26
|
+
SchemaException("Not implemented union between: " + str(x) + ' and ' + str(y))
|
|
20
27
|
|
|
21
28
|
PRECISION = get_int_precision()
|
|
22
29
|
STRING_PRECISION = str(PRECISION)
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
psyke/__init__.py,sha256=
|
|
1
|
+
psyke/__init__.py,sha256=V_sOXKt_5fYmNABwpveJd10u7Wy7A6wlLECBJY_ra_E,18634
|
|
2
2
|
psyke/hypercubepredictor.py,sha256=MlSRLky6J1I07qKcH98c9WvEjFHGyBiz_LU9W_oDnqs,4572
|
|
3
3
|
psyke/clustering/__init__.py,sha256=36MokTVwwWR_-o0mesvXHaYEYVTK2pn2m0ZY4G3Y3qU,581
|
|
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
7
|
psyke/extraction/__init__.py,sha256=ziZ8T9eAOZjKipepE5_j1zfZgyFPONjW8MGERSk83nI,743
|
|
8
|
-
psyke/extraction/cart/__init__.py,sha256=
|
|
9
|
-
psyke/extraction/cart/predictor.py,sha256=
|
|
8
|
+
psyke/extraction/cart/__init__.py,sha256=r2mDULg7oTHrIMPw9lqD6MyXhkkymP4r9PoGUKjAWCM,4569
|
|
9
|
+
psyke/extraction/cart/predictor.py,sha256=_YBedfyz4rGIBeNpN7OaL2BKWdF_5nuq8AlXkVfCtvk,3359
|
|
10
10
|
psyke/extraction/hypercubic/__init__.py,sha256=w_NmfSjh8fCWLDXVXpRLiAApq697cvUSPTgju-jtZCA,10620
|
|
11
11
|
psyke/extraction/hypercubic/hypercube.py,sha256=GKjplRl34BegrA3JclvlkrL7hXftdUUMXndmRFFoJic,25697
|
|
12
12
|
psyke/extraction/hypercubic/strategy.py,sha256=X-roIsfcpJyMdo2px5JtbhP7-XE-zUNkaEK7XGXoWA8,1636
|
|
@@ -22,7 +22,7 @@ psyke/extraction/real/__init__.py,sha256=fFqiwgWTpu5Jx9lz5CdSfs1QyqWYFLQDG7tc5M6
|
|
|
22
22
|
psyke/extraction/real/utils.py,sha256=eHGU-Y0inn_8jrk9lMcuRUKXpsTkI-s_myXSWz4bALQ,2190
|
|
23
23
|
psyke/extraction/trepan/__init__.py,sha256=KpZpk0btCWV4bS-DOmpgpYscSQ5FEMyP54ekm7ZedME,6583
|
|
24
24
|
psyke/extraction/trepan/utils.py,sha256=iSUJ1ooNQT_VO1KfBZuIUeUsyUbGdQf_pSEE87vMeQg,2320
|
|
25
|
-
psyke/schema/__init__.py,sha256=
|
|
25
|
+
psyke/schema/__init__.py,sha256=axv4ejZY0ItUwrC9IXb_yAhaQL5f1vwvXXmaIAHJmt0,26063
|
|
26
26
|
psyke/tuning/__init__.py,sha256=yd_ForFmHeYbtRXltY1fOa-mPJvpE6ijzg50M_8Sdxw,3649
|
|
27
27
|
psyke/tuning/crash/__init__.py,sha256=zIHEF75EFy_mRIieqzP04qKLG3GLsSc_mYZHpPfkzxU,2623
|
|
28
28
|
psyke/tuning/orchid/__init__.py,sha256=s64iABbteik27CrRPHSVHNZX25JKlDu7YYjhseOizxw,3618
|
|
@@ -33,8 +33,8 @@ psyke/utils/logic.py,sha256=7bbW6qcKof5PlqoQ0n5Kt3Obcot-KqGAvpE8rMXvEPE,12419
|
|
|
33
33
|
psyke/utils/metrics.py,sha256=Oo5BOonOSfo0qYsXWT5dmypZ7jiStByFC2MKEU0uMHg,2250
|
|
34
34
|
psyke/utils/plot.py,sha256=dE8JJ6tQ0Ezosid-r2jqAisREjFe5LqExRzsVi5Ns-c,7785
|
|
35
35
|
psyke/utils/sorted.py,sha256=C3CPW2JisND30BRk5c1sAAHs3Lb_wsRB2qZrYFuRnfM,678
|
|
36
|
-
psyke-0.8.
|
|
37
|
-
psyke-0.8.
|
|
38
|
-
psyke-0.8.
|
|
39
|
-
psyke-0.8.
|
|
40
|
-
psyke-0.8.
|
|
36
|
+
psyke-0.8.9.dist-info/LICENSE,sha256=KP9K6Hgezf_xdMFW7ORyKz9uA8Y8k52YJn292wcP-_E,11354
|
|
37
|
+
psyke-0.8.9.dist-info/METADATA,sha256=KDPbq3WrDph3KhEDTTNaFuHawsgeDl2XQ24K4lZLx-4,8102
|
|
38
|
+
psyke-0.8.9.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
39
|
+
psyke-0.8.9.dist-info/top_level.txt,sha256=q1HglxOqqoIRukFtyis_ZNHczZg4gANRUPWkD7HAUTU,6
|
|
40
|
+
psyke-0.8.9.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|