psyke 0.8.7__tar.gz → 0.8.9__tar.gz

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.

Files changed (77) hide show
  1. {psyke-0.8.7/psyke.egg-info → psyke-0.8.9}/PKG-INFO +1 -1
  2. psyke-0.8.9/VERSION +1 -0
  3. {psyke-0.8.7 → psyke-0.8.9}/psyke/__init__.py +3 -3
  4. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/cart/__init__.py +33 -15
  5. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/cart/predictor.py +15 -5
  6. {psyke-0.8.7 → psyke-0.8.9}/psyke/schema/__init__.py +12 -5
  7. {psyke-0.8.7 → psyke-0.8.9/psyke.egg-info}/PKG-INFO +1 -1
  8. psyke-0.8.7/VERSION +0 -1
  9. {psyke-0.8.7 → psyke-0.8.9}/LICENSE +0 -0
  10. {psyke-0.8.7 → psyke-0.8.9}/MANIFEST.in +0 -0
  11. {psyke-0.8.7 → psyke-0.8.9}/README.md +0 -0
  12. {psyke-0.8.7 → psyke-0.8.9}/psyke/clustering/__init__.py +0 -0
  13. {psyke-0.8.7 → psyke-0.8.9}/psyke/clustering/cream/__init__.py +0 -0
  14. {psyke-0.8.7 → psyke-0.8.9}/psyke/clustering/exact/__init__.py +0 -0
  15. {psyke-0.8.7 → psyke-0.8.9}/psyke/clustering/utils.py +0 -0
  16. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/__init__.py +0 -0
  17. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/hypercubic/__init__.py +0 -0
  18. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/hypercubic/cosmik/__init__.py +0 -0
  19. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/hypercubic/creepy/__init__.py +0 -0
  20. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/hypercubic/divine/__init__.py +0 -0
  21. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/hypercubic/gridex/__init__.py +0 -0
  22. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/hypercubic/gridrex/__init__.py +0 -0
  23. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/hypercubic/hex/__init__.py +0 -0
  24. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/hypercubic/hypercube.py +0 -0
  25. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/hypercubic/iter/__init__.py +0 -0
  26. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/hypercubic/strategy.py +0 -0
  27. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/hypercubic/utils.py +0 -0
  28. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/real/__init__.py +0 -0
  29. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/real/utils.py +0 -0
  30. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/trepan/__init__.py +0 -0
  31. {psyke-0.8.7 → psyke-0.8.9}/psyke/extraction/trepan/utils.py +0 -0
  32. {psyke-0.8.7 → psyke-0.8.9}/psyke/hypercubepredictor.py +0 -0
  33. {psyke-0.8.7 → psyke-0.8.9}/psyke/tuning/__init__.py +0 -0
  34. {psyke-0.8.7 → psyke-0.8.9}/psyke/tuning/crash/__init__.py +0 -0
  35. {psyke-0.8.7 → psyke-0.8.9}/psyke/tuning/orchid/__init__.py +0 -0
  36. {psyke-0.8.7 → psyke-0.8.9}/psyke/tuning/pedro/__init__.py +0 -0
  37. {psyke-0.8.7 → psyke-0.8.9}/psyke/utils/__init__.py +0 -0
  38. {psyke-0.8.7 → psyke-0.8.9}/psyke/utils/dataframe.py +0 -0
  39. {psyke-0.8.7 → psyke-0.8.9}/psyke/utils/logic.py +0 -0
  40. {psyke-0.8.7 → psyke-0.8.9}/psyke/utils/metrics.py +0 -0
  41. {psyke-0.8.7 → psyke-0.8.9}/psyke/utils/plot.py +0 -0
  42. {psyke-0.8.7 → psyke-0.8.9}/psyke/utils/sorted.py +0 -0
  43. {psyke-0.8.7 → psyke-0.8.9}/psyke.egg-info/SOURCES.txt +0 -0
  44. {psyke-0.8.7 → psyke-0.8.9}/psyke.egg-info/dependency_links.txt +0 -0
  45. {psyke-0.8.7 → psyke-0.8.9}/psyke.egg-info/not-zip-safe +0 -0
  46. {psyke-0.8.7 → psyke-0.8.9}/psyke.egg-info/requires.txt +0 -0
  47. {psyke-0.8.7 → psyke-0.8.9}/psyke.egg-info/top_level.txt +0 -0
  48. {psyke-0.8.7 → psyke-0.8.9}/pyproject.toml +0 -0
  49. {psyke-0.8.7 → psyke-0.8.9}/setup.cfg +0 -0
  50. {psyke-0.8.7 → psyke-0.8.9}/setup.py +0 -0
  51. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/__init__.py +0 -0
  52. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/clustering/__init__.py +0 -0
  53. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/__init__.py +0 -0
  54. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/cart/__init__.py +0 -0
  55. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/cart/test_cart.py +0 -0
  56. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/cart/test_simplified_cart.py +0 -0
  57. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/hypercubic/__init__.py +0 -0
  58. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/hypercubic/gridex/__init__.py +0 -0
  59. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/hypercubic/gridex/test_gridex.py +0 -0
  60. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/hypercubic/iter/__init__.py +0 -0
  61. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/hypercubic/iter/test_iter.py +0 -0
  62. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/hypercubic/test_hypercube.py +0 -0
  63. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/real/__init__.py +0 -0
  64. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/real/test_real.py +0 -0
  65. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/real/test_rule.py +0 -0
  66. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/trepan/__init__.py +0 -0
  67. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/trepan/test_node.py +0 -0
  68. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/trepan/test_split.py +0 -0
  69. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/extraction/trepan/test_trepan.py +0 -0
  70. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/utils/__init__.py +0 -0
  71. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/utils/test_prune.py +0 -0
  72. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/utils/test_simplify.py +0 -0
  73. {psyke-0.8.7 → psyke-0.8.9}/test/psyke/utils/test_simplify_formatter.py +0 -0
  74. {psyke-0.8.7 → psyke-0.8.9}/test/resources/__init__.py +0 -0
  75. {psyke-0.8.7 → psyke-0.8.9}/test/resources/datasets/__init__.py +0 -0
  76. {psyke-0.8.7 → psyke-0.8.9}/test/resources/predictors/__init__.py +0 -0
  77. {psyke-0.8.7 → psyke-0.8.9}/test/resources/tests/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: psyke
3
- Version: 0.8.7
3
+ Version: 0.8.9
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
psyke-0.8.9/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.8.9
@@ -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, discretization=discretization, normalization=normalization,
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], constraints: LeafConstraints) -> Iterable[Struct]:
31
+ def _create_body(self, variables: dict[str, Var], conditions: LeafConstraints) -> Iterable[Struct]:
29
32
  results = []
30
- for feature_name, constraint, value in constraints:
31
- features = [d for d in self.discretization if feature_name in d.admissible_values]
32
- feature: DiscreteFeature = features[0] if len(features) > 0 else None
33
- results.append(create_term(variables[feature_name], constraint) if feature is None else
34
- create_term(variables[feature.name],
35
- feature.admissible_values[feature_name],
36
- isinstance(constraint, GreaterThan)))
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 condition in first_node:
45
- if all([condition in [node[0] for node in nodes][i] for i in range(len(nodes))]):
46
- [node[0].remove(condition) for node in nodes]
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, max_leaf_nodes=self.leaves)
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 = list[tuple[str, Value, bool]]
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
- return [(feature, LessThan(threshold) if condition else GreaterThan(threshold), condition)
29
- for feature, condition, threshold in zip(features, conditions, thresholds)]
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=[]) -> Iterable[(int, bool)]:
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)
@@ -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
- Exception(f"Empty intersection between two Value: {str(x)} and {str(y)}")
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
- Exception("Not implemented intersection between: " + str(x) + ' and ' + str(y))
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
- Exception("Calling method with wrong type argument: " + str(x) + ' and ' + str(y))
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
- Exception(f"Empty union between two Value: {str(x)} and {str(y)}")
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
- Exception("Not implemented union between: " + str(x) + ' and ' + str(y))
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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: psyke
3
- Version: 0.8.7
3
+ Version: 0.8.9
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
psyke-0.8.7/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.8.7
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes