psyke 0.10.1.dev3__py3-none-any.whl → 0.10.1.dev6__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 CHANGED
@@ -396,8 +396,8 @@ class Extractor(EvaluableModel, ABC):
396
396
 
397
397
  @staticmethod
398
398
  def ginger(predictor, features: Iterable[str], sigmas: Iterable[float], max_slices: int, min_rules: int = 1,
399
- max_poly: int = 1, alpha: float = 0.5, indpb: float = 0.5, tournsize: int = 3, metric:str = 'R2',
400
- n_gen: int = 50, n_pop: int = 50, threshold=None, valid=None,
399
+ max_poly: int = 1, alpha: float = 0.5, indpb: float = 0.5, tournsize: int = 3, metric: str = 'R2',
400
+ n_gen: int = 50, n_pop: int = 50, threshold=None, valid=None, output=Target.REGRESSION,
401
401
  normalization: dict[str, tuple[float, float]] = None,
402
402
  seed: int = get_default_random_seed()) -> Extractor:
403
403
  """
@@ -405,7 +405,7 @@ class Extractor(EvaluableModel, ABC):
405
405
  """
406
406
  from psyke.extraction.hypercubic.ginger import GInGER
407
407
  return GInGER(predictor, features, sigmas, max_slices, min_rules, max_poly, alpha, indpb, tournsize, metric,
408
- n_gen, n_pop, threshold, valid, normalization, seed)
408
+ n_gen, n_pop, threshold, valid, output, normalization, seed)
409
409
 
410
410
  @staticmethod
411
411
  def gridrex(predictor, grid, min_examples: int = 250, threshold: float = 0.1,
@@ -28,12 +28,12 @@ class HyperCubeExtractor(HyperCubePredictor, PedagogicalExtractor, ABC):
28
28
  self._default_surrounding_cube = False
29
29
  self.threshold = None
30
30
 
31
- def _default_cube(self) -> HyperCube | RegressionCube | ClassificationCube:
31
+ def _default_cube(self, dimensions=None) -> HyperCube | RegressionCube | ClassificationCube:
32
32
  if self._output == Target.CONSTANT:
33
- return HyperCube()
33
+ return HyperCube(dimensions)
34
34
  if self._output == Target.REGRESSION:
35
- return RegressionCube()
36
- return ClassificationCube()
35
+ return RegressionCube(dimensions)
36
+ return ClassificationCube(dimensions)
37
37
 
38
38
  @staticmethod
39
39
  def _find_couples(to_split: Iterable[HyperCube], not_in_cache: set[HyperCube],
@@ -3,10 +3,11 @@ from typing import Iterable
3
3
 
4
4
  import numpy as np
5
5
  import pandas as pd
6
+ from sklearn.base import ClassifierMixin
6
7
  from sklearn.preprocessing import PolynomialFeatures
7
8
  from tuprolog.theory import Theory
8
9
 
9
- from psyke import get_default_random_seed
10
+ from psyke import get_default_random_seed, Target
10
11
  from psyke.extraction.hypercubic import HyperCubeExtractor, HyperCube, RegressionCube
11
12
 
12
13
  from deap import base, creator
@@ -20,9 +21,10 @@ class GInGER(HyperCubeExtractor):
20
21
  """
21
22
 
22
23
  def __init__(self, predictor, features, sigmas, max_slices, min_rules=1, max_poly=1, alpha=0.5, indpb=0.5,
23
- tournsize=3, metric='R2', n_gen=50, n_pop=50, threshold=None, valid=None, normalization=None,
24
- seed: int = get_default_random_seed()):
25
- super().__init__(predictor, normalization)
24
+ tournsize=3, metric='R2', n_gen=50, n_pop=50, threshold=None, valid=None,
25
+ output: Target = Target.REGRESSION, normalization=None, seed: int = get_default_random_seed()):
26
+ super().__init__(predictor, output=Target.CLASSIFICATION if isinstance(predictor, ClassifierMixin) else output,
27
+ normalization=normalization)
26
28
  self.threshold = threshold
27
29
  np.random.seed(seed)
28
30
 
@@ -60,8 +62,8 @@ class GInGER(HyperCubeExtractor):
60
62
  for poly in range(self.poly):
61
63
  for slices in list(itertools.product(range(1, self.max_slices + 1), repeat=self.max_features)):
62
64
  gr = GIn((dataframe.iloc[:, :-1], dataframe.iloc[:, -1]), self.valid, self.features, self.sigmas,
63
- slices, min_rules=self.min_rules, poly=poly + 1, alpha=self.alpha,
64
- indpb=self.indpb, tournsize=self.tournsize, metric=self.metric, warm=True)
65
+ slices, min_rules=self.min_rules, poly=poly + 1, alpha=self.alpha, indpb=self.indpb,
66
+ tournsize=self.tournsize, metric=self.metric, output=self._output, warm=True)
65
67
 
66
68
  b, score, _, _ = gr.run(n_gen=self.n_gen, n_pop=self.n_pop)
67
69
  best[(score, poly + 1, slices)] = b
@@ -81,8 +83,8 @@ class GInGER(HyperCubeExtractor):
81
83
 
82
84
  hypercubes = [{f: iv for f, iv in zip(self.features, combo)} for combo in itertools.product(*intervals)]
83
85
  mi_ma = {f: (transformed[f].min(), transformed[f].max()) for f in transformed.columns if f not in self.features}
84
- self._hypercubes = [RegressionCube({feat: h[feat] if feat in self.features else mi_ma[feat]
85
- for feat in transformed.columns[:-1]}) for h in hypercubes]
86
+ self._hypercubes = [self._default_cube({feat: h[feat] if feat in self.features else mi_ma[feat]
87
+ for feat in transformed.columns[:-1]}) for h in hypercubes]
86
88
  self._hypercubes = [c for c in self._hypercubes if c.count(transformed) >= 2]
87
89
  for c in self._hypercubes:
88
90
  for feature in transformed.columns:
@@ -1,17 +1,22 @@
1
+ from statistics import mode
2
+
1
3
  import numpy as np
2
4
  from deap import base, creator, tools, algorithms
3
5
  import random
4
6
  from sklearn.linear_model import LinearRegression
5
- from sklearn.metrics import mean_absolute_error, r2_score
7
+ from sklearn.metrics import mean_absolute_error, r2_score, mean_squared_error, f1_score, accuracy_score
6
8
  from sklearn.preprocessing import PolynomialFeatures
7
9
 
10
+ from psyke import Target
11
+
8
12
 
9
13
  class GIn:
10
14
 
11
15
  def __init__(self, train, valid, features, sigmas, slices, min_rules=1, poly=1, alpha=0.5, indpb=0.5, tournsize=3,
12
- metric='R2', warm=False):
16
+ metric='R2', output=Target.REGRESSION, warm=False):
13
17
  self.X, self.y = train
14
18
  self.valid = valid
19
+ self.output = output
15
20
 
16
21
  self.features = features
17
22
  self.sigmas = sigmas
@@ -42,6 +47,29 @@ class GIn:
42
47
 
43
48
  return regions
44
49
 
50
+ def __output_estimation(self, mask, to_pred):
51
+ if self.output == Target.REGRESSION:
52
+ return LinearRegression().fit(self.poly.fit_transform(self.X)[mask], self.y[mask]).predict(
53
+ self.poly.fit_transform(to_pred))
54
+ if self.output == Target.CONSTANT:
55
+ return np.mean(self.y[mask])
56
+ if self.output == Target.CLASSIFICATION:
57
+ return mode(self.y[mask])
58
+ raise TypeError('Supported outputs are Target.{REGRESSION, CONSTANT, CLASSIFICATION}')
59
+
60
+ def __score(self, true, pred):
61
+ if self.metric == 'R2':
62
+ return r2_score(true, pred)
63
+ if self.metric == 'MAE':
64
+ return -mean_absolute_error(true, pred)
65
+ if self.metric == 'MSE':
66
+ return -mean_squared_error(true, pred)
67
+ if self.metric == 'F1':
68
+ return f1_score(true, pred, average='weighted')
69
+ if self.metric == 'ACC':
70
+ return accuracy_score(true, pred)
71
+ raise NameError('Supported metrics are R2, MAE, MSE, F1, ACC')
72
+
45
73
  def evaluate(self, individual):
46
74
  to_pred, true = self.valid or (self.X, self.y)
47
75
  boundaries = np.cumsum([0] + list(self.slices))
@@ -50,23 +78,24 @@ class GIn:
50
78
  regions = self.region(to_pred, cuts)
51
79
  regionsT = self.region(self.X, cuts)
52
80
 
53
- y_pred = np.zeros(len(to_pred))
81
+ y_pred = np.empty(len(to_pred), dtype=f'U{self.y.str.len().max()}') if self.output == Target.CLASSIFICATION \
82
+ else np.zeros_like(self.y)
54
83
  valid_regions = 0
55
84
 
56
85
  for r in range(np.prod([s + 1 for s in self.slices])):
57
86
  mask = regions == r
58
87
  maskT = regionsT == r
59
88
  if min(mask.sum(), maskT.sum()) < 3:
60
- y_pred[mask] = np.mean(self.y)
89
+ if self.output != Target.CLASSIFICATION:
90
+ y_pred[mask] = np.mean(self.y)
61
91
  continue
62
- y_pred[mask] = LinearRegression().fit(self.poly.fit_transform(self.X)[maskT], self.y[maskT]).predict(
63
- self.poly.fit_transform(to_pred)[mask])
92
+ y_pred[mask] = self.__output_estimation(maskT, to_pred[mask])
64
93
  valid_regions += 1
65
94
 
66
95
  if valid_regions < self.min_rules:
67
96
  return -9999,
68
97
 
69
- return (r2_score if self.metric == 'R2' else -mean_absolute_error)(true, y_pred),
98
+ return self.__score(true, y_pred),
70
99
 
71
100
  def setup(self, warm=False):
72
101
  if not warm:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: psyke
3
- Version: 0.10.1.dev3
3
+ Version: 0.10.1.dev6
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
@@ -1,4 +1,4 @@
1
- psyke/__init__.py,sha256=4GYagtqZnAOBz3VKyOgEp-yS5-16J7x2J9PoOkdQ9-U,23282
1
+ psyke/__init__.py,sha256=p8ssZXquoOZvdwNZgSSpyG94MRJbeph3KYiZuywBaIc,23317
2
2
  psyke/hypercubepredictor.py,sha256=Pg8F2R_NHrNgFHx92s32BorYHMVvaxpEh4GtCsoyB2U,4620
3
3
  psyke/clustering/__init__.py,sha256=LfLZY2UwHY9xlFT4SMGGbyFY5S6sMXndY-UMaJIJtd8,714
4
4
  psyke/clustering/utils.py,sha256=BqMPKJ-r6CdxXwyk-2AvkPV4DBnZF5WUNz2fKiXbhlw,1596
@@ -9,14 +9,14 @@ psyke/extraction/cart/CartPredictor.py,sha256=YhEuaENLWixu379sIXZkFeCNc8GBnxLnR6
9
9
  psyke/extraction/cart/FairTree.py,sha256=mccoLDrSNy6iivqFZ23m33hxIB_kPXa3mNL1ukfb5Ls,7624
10
10
  psyke/extraction/cart/FairTreePredictor.py,sha256=7z4oLqflkRMqqVW_UIlrGsQrvROM4sXUfY7LPQJ662g,2321
11
11
  psyke/extraction/cart/__init__.py,sha256=SsjAJiL4n6q_GNR6H8PNfhTkAZ67Ka7NRvVRxCULBhQ,3191
12
- psyke/extraction/hypercubic/__init__.py,sha256=AxvPJxEQzL9Diyi7l2kX3zZESZ9xbh6RFp7ffs0w2ic,13112
12
+ psyke/extraction/hypercubic/__init__.py,sha256=RN2w-jUweifEX8ds6AxP7jc3dPkQD4lMWFjH-QdArG8,13159
13
13
  psyke/extraction/hypercubic/hypercube.py,sha256=Pz-F6RkAKLT5e86L29khqLjKTJ7k2TZszdRdxddVFtA,26275
14
14
  psyke/extraction/hypercubic/strategy.py,sha256=m9BGSrKc-VadgEQTOPow85hBPFqMIt0J99nCFIh4NUs,1839
15
15
  psyke/extraction/hypercubic/utils.py,sha256=D2FN5CCm_T3h23DmLFoTnIcFo7LvIq__ktl4hjUqkcA,1525
16
16
  psyke/extraction/hypercubic/cosmik/__init__.py,sha256=XQUvOtMFpR0vMHYtwIVl3G626HMqN8Clt6BqNm4nvFs,1880
17
17
  psyke/extraction/hypercubic/creepy/__init__.py,sha256=x8a1ftoYHixGpiDfM3u-6QBEDYmaSlPIRIuAOCx573w,2056
18
18
  psyke/extraction/hypercubic/divine/__init__.py,sha256=ClO8CITKKXoo7nhlBJagR1yAachsxLHYQlqggl-9eGE,3665
19
- psyke/extraction/hypercubic/ginger/__init__.py,sha256=GZOKy_iLmYOuXcRrPhNEDP5ZA2Ez2Cjb6GuervSAD4Y,4476
19
+ psyke/extraction/hypercubic/ginger/__init__.py,sha256=7G8H07d6PyIfRjcQjTQbe1WhwpHce_ky7Sy61JoxbqA,4713
20
20
  psyke/extraction/hypercubic/gridex/__init__.py,sha256=tPPLGRJ-7fCt-OB-qq6W7EV0hqEuQVUGlXs2yyABo98,3161
21
21
  psyke/extraction/hypercubic/gridrex/__init__.py,sha256=h9usK5tFqd6ngBmRydsgkfQ1jlcQKj2uG72Tr1puFHk,595
22
22
  psyke/extraction/hypercubic/hex/__init__.py,sha256=553AZjOT9thfqDGtVDI6WtgYNex2Y6dg53cEyuf7Q80,4805
@@ -26,7 +26,7 @@ psyke/extraction/real/utils.py,sha256=4NNL15Eu7cmkG9b29GBP6CKgMTV1cmiJVS0k1MbWpI
26
26
  psyke/extraction/trepan/__init__.py,sha256=H8F_wpFLPcfyx2tgOOno8FwUomxfVxVl1vxlb0ClP1g,6931
27
27
  psyke/extraction/trepan/utils.py,sha256=iSUJ1ooNQT_VO1KfBZuIUeUsyUbGdQf_pSEE87vMeQg,2320
28
28
  psyke/genetic/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
29
- psyke/genetic/gin/__init__.py,sha256=1_VBAhMYo91gZKrcUQTTaRxkOG4lRCyG9ZhACIDC9yE,4179
29
+ psyke/genetic/gin/__init__.py,sha256=9lwTfz6yrtxzg_BKE-bogEeHNpyTMINZOP4rUvItfQM,5405
30
30
  psyke/schema/__init__.py,sha256=axv4ejZY0ItUwrC9IXb_yAhaQL5f1vwvXXmaIAHJmt0,26063
31
31
  psyke/tuning/__init__.py,sha256=yd_ForFmHeYbtRXltY1fOa-mPJvpE6ijzg50M_8Sdxw,3649
32
32
  psyke/tuning/crash/__init__.py,sha256=zIHEF75EFy_mRIieqzP04qKLG3GLsSc_mYZHpPfkzxU,2623
@@ -38,8 +38,8 @@ psyke/utils/logic.py,sha256=ioP25WMTYNYEzaRDNDe3kGNWqZ6DA_63t19d-ky_2kM,12227
38
38
  psyke/utils/metrics.py,sha256=Oo5BOonOSfo0qYsXWT5dmypZ7jiStByFC2MKEU0uMHg,2250
39
39
  psyke/utils/plot.py,sha256=dE8JJ6tQ0Ezosid-r2jqAisREjFe5LqExRzsVi5Ns-c,7785
40
40
  psyke/utils/sorted.py,sha256=C3CPW2JisND30BRk5c1sAAHs3Lb_wsRB2qZrYFuRnfM,678
41
- psyke-0.10.1.dev3.dist-info/licenses/LICENSE,sha256=G3mPaubObvkBXbsgTTeYGLk_pNEW8tc7HZr4u_wLEpU,11398
42
- psyke-0.10.1.dev3.dist-info/METADATA,sha256=uplKW5O_y2eauv1UsYMQNGYlXItHQYbngkiVJI43JiM,8395
43
- psyke-0.10.1.dev3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
44
- psyke-0.10.1.dev3.dist-info/top_level.txt,sha256=q1HglxOqqoIRukFtyis_ZNHczZg4gANRUPWkD7HAUTU,6
45
- psyke-0.10.1.dev3.dist-info/RECORD,,
41
+ psyke-0.10.1.dev6.dist-info/licenses/LICENSE,sha256=G3mPaubObvkBXbsgTTeYGLk_pNEW8tc7HZr4u_wLEpU,11398
42
+ psyke-0.10.1.dev6.dist-info/METADATA,sha256=PAnBUWaXD3PdGNCpnLbknVdIfJsWJDVXtJcF7_Pyr1s,8395
43
+ psyke-0.10.1.dev6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
44
+ psyke-0.10.1.dev6.dist-info/top_level.txt,sha256=q1HglxOqqoIRukFtyis_ZNHczZg4gANRUPWkD7HAUTU,6
45
+ psyke-0.10.1.dev6.dist-info/RECORD,,