learningmachine 2.1.0__tar.gz → 2.2.2__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.
Files changed (33) hide show
  1. {learningmachine-2.1.0 → learningmachine-2.2.2}/PKG-INFO +1 -1
  2. learningmachine-2.2.2/README.md +118 -0
  3. {learningmachine-2.1.0 → learningmachine-2.2.2}/learningmachine/base.py +28 -46
  4. learningmachine-2.2.2/learningmachine/classifier.py +161 -0
  5. learningmachine-2.2.2/learningmachine/regression.py +137 -0
  6. {learningmachine-2.1.0 → learningmachine-2.2.2}/learningmachine.egg-info/PKG-INFO +1 -1
  7. {learningmachine-2.1.0 → learningmachine-2.2.2}/setup.py +1 -1
  8. learningmachine-2.1.0/README.md +0 -54
  9. learningmachine-2.1.0/learningmachine/classifier.py +0 -132
  10. learningmachine-2.1.0/learningmachine/regression.py +0 -100
  11. {learningmachine-2.1.0 → learningmachine-2.2.2}/CONTRIBUTING.rst +0 -0
  12. {learningmachine-2.1.0 → learningmachine-2.2.2}/HISTORY.rst +0 -0
  13. {learningmachine-2.1.0 → learningmachine-2.2.2}/LICENSE +0 -0
  14. {learningmachine-2.1.0 → learningmachine-2.2.2}/MANIFEST.in +0 -0
  15. {learningmachine-2.1.0 → learningmachine-2.2.2}/docs/Makefile +0 -0
  16. {learningmachine-2.1.0 → learningmachine-2.2.2}/docs/conf.py +0 -0
  17. {learningmachine-2.1.0 → learningmachine-2.2.2}/docs/contributing.rst +0 -0
  18. {learningmachine-2.1.0 → learningmachine-2.2.2}/docs/history.rst +0 -0
  19. {learningmachine-2.1.0 → learningmachine-2.2.2}/docs/index.rst +0 -0
  20. {learningmachine-2.1.0 → learningmachine-2.2.2}/docs/installation.rst +0 -0
  21. {learningmachine-2.1.0 → learningmachine-2.2.2}/docs/make.bat +0 -0
  22. {learningmachine-2.1.0 → learningmachine-2.2.2}/docs/readme.rst +0 -0
  23. {learningmachine-2.1.0 → learningmachine-2.2.2}/docs/usage.rst +0 -0
  24. {learningmachine-2.1.0 → learningmachine-2.2.2}/learningmachine/__init__.py +0 -0
  25. {learningmachine-2.1.0 → learningmachine-2.2.2}/learningmachine/utils.py +0 -0
  26. {learningmachine-2.1.0 → learningmachine-2.2.2}/learningmachine.egg-info/SOURCES.txt +0 -0
  27. {learningmachine-2.1.0 → learningmachine-2.2.2}/learningmachine.egg-info/dependency_links.txt +0 -0
  28. {learningmachine-2.1.0 → learningmachine-2.2.2}/learningmachine.egg-info/not-zip-safe +0 -0
  29. {learningmachine-2.1.0 → learningmachine-2.2.2}/learningmachine.egg-info/requires.txt +0 -0
  30. {learningmachine-2.1.0 → learningmachine-2.2.2}/learningmachine.egg-info/top_level.txt +0 -0
  31. {learningmachine-2.1.0 → learningmachine-2.2.2}/setup.cfg +0 -0
  32. {learningmachine-2.1.0 → learningmachine-2.2.2}/tests/__init__.py +0 -0
  33. {learningmachine-2.1.0 → learningmachine-2.2.2}/tests/test_learningmachine.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: learningmachine
3
- Version: 2.1.0
3
+ Version: 2.2.2
4
4
  Summary: Machine Learning with uncertainty quantification and interpretability
5
5
  Home-page: https://github.com/Techtonique/learningmachine_python
6
6
  Author: T. Moudiki
@@ -0,0 +1,118 @@
1
+ # learningmachine
2
+
3
+ ![PyPI](https://img.shields.io/pypi/v/learningmachine) [![PyPI - License](https://img.shields.io/pypi/l/learningmachine)](https://github.com/thierrymoudiki/learningmachine/blob/master/LICENSE) [![Downloads](https://pepy.tech/badge/learningmachine)](https://pepy.tech/project/learningmachine)
4
+
5
+
6
+ Machine Learning with uncertainty quantification and interpretability.
7
+
8
+ ## Install
9
+
10
+ If R packages are not installed automatically when running `pip`, [install them manually](https://cloud.r-project.org/).
11
+
12
+ **Development version**
13
+
14
+ ```bash
15
+ !pip install git+https://github.com/Techtonique/learningmachine_python.git --verbose
16
+ ```
17
+
18
+ ## Example
19
+
20
+ See also:
21
+ - [this notebook](https://colab.research.google.com/github/Techtonique/learningmachine_python/blob/main/learningmachine/demo/thierrymoudiki_20240401_calib.ipynb)
22
+ - [this notebook](https://colab.research.google.com/github/Techtonique/learningmachine_python/blob/main/learningmachine/demo/thierrymoudiki_20240508_calib.ipynb)
23
+
24
+ ```python
25
+ import learningmachine as lm
26
+ import numpy as np
27
+ import pandas as pd
28
+ from sklearn.datasets import load_diabetes, load_wine
29
+ from sklearn.datasets import load_wine, load_iris, load_breast_cancer
30
+ from sklearn.datasets import fetch_california_housing
31
+ from sklearn.model_selection import train_test_split, cross_val_score
32
+ from rpy2.robjects.vectors import FloatMatrix, FloatVector, StrVector
33
+ from time import time
34
+ from sklearn.metrics import mean_squared_error
35
+ from math import sqrt
36
+
37
+
38
+ # 1. Regression
39
+
40
+ diabetes = load_diabetes()
41
+ X = pd.DataFrame(diabetes.data[:150], columns=diabetes.feature_names)
42
+ y = diabetes.target[:150]
43
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
44
+ random_state=1213)
45
+
46
+ print("\n ----- fitting krr ----- \n")
47
+
48
+ fit_obj2 = lm.Regressor(method="krr", pi_method="none")
49
+ start = time()
50
+ fit_obj2.fit(X_train, y_train, lambda_=0.05) # R's `lambda` is renamed as `lambda_` in Python as `lambda` is reserved
51
+ print("Elapsed time: ", time() - start)
52
+ print(fit_obj2.summary(X=X_test, y=y_test))
53
+
54
+ # 2. Classification
55
+
56
+ datasets = [load_wine(), load_iris(), load_breast_cancer()]
57
+
58
+ print("\n ----- fitting Kernel Ridge Regression ----- \n")
59
+
60
+ for dataset in datasets:
61
+
62
+ print(f"Description: {dataset.DESCR}")
63
+ X = pd.DataFrame(dataset.data, columns=dataset.feature_names)
64
+ y = dataset.target
65
+
66
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
67
+ random_state=123)
68
+
69
+ fit_obj = lm.Classifier(method = "krr",
70
+ pi_method="none")
71
+
72
+ start = time()
73
+ fit_obj.fit(X_train, y_train, reg_lambda = 0.05)
74
+ print("Elapsed time: ", time() - start)
75
+
76
+ ## Compute accuracy
77
+ print(fit_obj.summary(X=X_test, y=y_test,
78
+ class_index=0))
79
+
80
+
81
+ print("\n ----- fitting xgboost ----- \n")
82
+
83
+ for dataset in datasets:
84
+
85
+ print(f"Description: {dataset.DESCR}")
86
+ X = pd.DataFrame(dataset.data, columns=dataset.feature_names)
87
+ y = dataset.target
88
+
89
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
90
+ random_state=123)
91
+
92
+ fit_obj = lm.Classifier(method = "xgboost",
93
+ pi_method="kdesplitconformal",
94
+ type_prediction_set = 'score',
95
+ B=100)
96
+
97
+ print("nb_hidden = 0 -----") # no hidden layer
98
+ start = time()
99
+ fit_obj.fit(X_train, y_train, nrounds=100, eta=0.05, max__depth=4, verbose=0) # dot ('.') in R parameters is replaced by '__'
100
+ print("Elapsed time: ", time() - start)
101
+ print(fit_obj.predict(X_test))
102
+ print(fit_obj.summary(X=X_test, y=y_test,
103
+ class_index=1)) # specify the class whose probability is of interest
104
+
105
+ fit_obj = lm.Classifier(method = "xgboost",
106
+ pi_method="kdesplitconformal",
107
+ type_prediction_set = 'score',
108
+ nb_hidden = 5,
109
+ B=100)
110
+
111
+ print("nb_hidden = 5 -----") # hidden layer with 5 nodes
112
+ start = time()
113
+ fit_obj.fit(X_train, y_train, nrounds=100, eta=0.05, max__depth=4, verbose=0) # dot ('.') in R parameters is replaced by '__'
114
+ print("Elapsed time: ", time() - start)
115
+ print(fit_obj.predict(X_test))
116
+ print(fit_obj.summary(X=X_test, y=y_test,
117
+ class_index=1)) # specify the class whose probability is of interest
118
+ ```
@@ -1,10 +1,11 @@
1
+ import pandas as pd
1
2
  import sklearn.metrics as skm
2
3
  import subprocess
3
4
  from functools import lru_cache
4
5
  from sklearn.base import BaseEstimator
5
6
  from rpy2.robjects.vectors import (
6
7
  StrVector,
7
- FloatMatrix,
8
+ ListVector,
8
9
  FloatVector,
9
10
  IntVector,
10
11
  FactorVector,
@@ -58,6 +59,7 @@ class Base(BaseEstimator):
58
59
  self.params = params
59
60
  self.seed = seed
60
61
  self.obj = None
62
+ self.column_names = None
61
63
 
62
64
  def load_learningmachine(self):
63
65
  # Install R packages
@@ -212,63 +214,43 @@ class Base(BaseEstimator):
212
214
 
213
215
  return scoring_options[scoring](y, preds, **kwargs)
214
216
 
215
- def summary(self, X, y,
216
- class_index = None,
217
- level = 95,
218
- show_progress = True,
219
- cl = None):
217
+ def summary(self, X, y,
218
+ class_index = None,
219
+ cl = None,
220
+ show_progress = True):
221
+
222
+ if isinstance(X, pd.DataFrame):
223
+ X_r = r.matrix(FloatVector(X.values.ravel()),
224
+ byrow=True,
225
+ ncol=X.shape[1],
226
+ nrow=X.shape[0])
227
+ X_r.colnames = StrVector(self.column_names)
228
+ else:
229
+ X_r = r.matrix(FloatVector(X.ravel()),
230
+ byrow=True,
231
+ ncol=X.shape[1],
232
+ nrow=X.shape[0])
220
233
 
221
234
  if cl is None:
222
235
 
223
236
  if self.type == "classification":
224
237
 
225
- assert class_index is not None, "class_index must be provided for classification models"
238
+ assert class_index is not None, "For classifiers, 'class_index' must be provided"
226
239
 
227
- self.obj["summary"](X = r.matrix(FloatVector(X.ravel()),
228
- byrow=True,
229
- ncol=X.shape[1],
230
- nrow=X.shape[0]),
240
+ return self.obj["summary"](X = X_r,
231
241
  y = FactorVector(IntVector(y)),
232
- class_index = class_index + 1,
233
- level = level,
242
+ class_index = int(class_index) + 1,
234
243
  show_progress = show_progress
235
244
  )
236
245
 
237
- else: # regression
238
-
239
- self.obj["summary"](X = r.matrix(FloatVector(X.ravel()),
240
- byrow=True,
241
- ncol=X.shape[1],
242
- nrow=X.shape[0]),
246
+ elif self.type == "regression":
247
+
248
+ return self.obj["summary"](X = X_r,
243
249
  y = FloatVector(y),
244
- level = level,
245
250
  show_progress = show_progress
246
- )
251
+ )
252
+
247
253
  else: # cl is not None, parallel computing
248
254
 
249
- if self.type == "classification":
250
-
251
- assert class_index is not None, "class_index must be provided for classification models"
252
-
253
- self.obj["summary"](X = r.matrix(FloatVector(X.ravel()),
254
- byrow=True,
255
- ncol=X.shape[1],
256
- nrow=X.shape[0]),
257
- y = FactorVector(IntVector(y)),
258
- level = level,
259
- show_progress = show_progress,
260
- class_index = class_index + 1,
261
- cl = cl
262
- )
263
-
264
- else: # regression
255
+ pass
265
256
 
266
- self.obj["summary"](X = r.matrix(FloatVector(X.ravel()),
267
- byrow=True,
268
- ncol=X.shape[1],
269
- nrow=X.shape[0]),
270
- y = FloatVector(y),
271
- level = level,
272
- show_progress = show_progress,
273
- cl = cl
274
- )
@@ -0,0 +1,161 @@
1
+ import numpy as np
2
+ import pandas as pd
3
+ import sklearn.metrics as skm
4
+ from rpy2.robjects import r
5
+ from rpy2.robjects.packages import importr
6
+ from rpy2.robjects.vectors import (
7
+ FloatVector,
8
+ IntVector,
9
+ FactorVector,
10
+ StrVector
11
+ )
12
+ from sklearn.base import ClassifierMixin
13
+ from .base import Base
14
+ from .utils import format_value, r_list_to_namedtuple
15
+
16
+ base = importr("base")
17
+ stats = importr("stats")
18
+ utils = importr("utils")
19
+
20
+
21
+ class Classifier(Base, ClassifierMixin):
22
+ """
23
+ Classifier.
24
+ """
25
+
26
+ def __init__(
27
+ self,
28
+ method="ranger",
29
+ pi_method="none",
30
+ level=95,
31
+ type_prediction_set="score",
32
+ B=100,
33
+ nb_hidden = 0,
34
+ nodes_sim = "sobol",
35
+ activ = "relu",
36
+ seed=123,
37
+ ):
38
+ """
39
+ Initialize the model.
40
+ """
41
+ super().__init__(
42
+ name = "Classifier",
43
+ type = "classification",
44
+ method=method,
45
+ pi_method=pi_method,
46
+ level=level,
47
+ B=B,
48
+ nb_hidden=nb_hidden,
49
+ nodes_sim=nodes_sim,
50
+ activ=activ,
51
+ seed=seed,
52
+ )
53
+
54
+ self.type_prediction_set=type_prediction_set
55
+
56
+ try:
57
+ r_obj_command = 'suppressWarnings(suppressMessages(library(learningmachine))); ' +\
58
+ 'Classifier$new(method = ' + str(format_value(self.method)) + ', ' +\
59
+ 'pi_method = ' + str(format_value(self.pi_method)) + ', ' +\
60
+ 'level = ' + str(format_value(self.level)) + ', ' +\
61
+ 'type_prediction_set = ' + str(format_value(self.type_prediction_set)) + ', ' +\
62
+ 'B = ' + str(format_value(self.B)) + ', ' +\
63
+ 'nb_hidden = ' + str(format_value(self.nb_hidden)) + ', ' +\
64
+ 'nodes_sim = ' + str(format_value(self.nodes_sim)) + ', ' +\
65
+ 'activ = ' + str(format_value(self.activ)) + ', ' +\
66
+ 'seed = ' + str(format_value(self.seed)) + ')'
67
+ self.obj = r(r_obj_command)
68
+ except Exception:
69
+ try:
70
+ self.obj = r(f"suppressWarnings(suppressMessages(library(learningmachine))); Classifier$new(method = {format_value(self.method)}, pi_method = {format_value(self.pi_method)}, level = {format_value(self.level)}, type_prediction_set = {format_value(self.type_prediction_set)}, B = {format_value(self.B)}, nb_hidden = {format_value(self.nb_hidden)}, nodes_sim = {format_value(self.nodes_sim)}, activ = {format_value(self.activ)}, seed = {format_value(self.seed)})")
71
+ except Exception:
72
+ self.obj = r(f"learningmachine::Classifier$new(method = {format_value(self.method)}, pi_method = {format_value(self.pi_method)}, level = {format_value(self.level)}, type_prediction_set = {format_value(self.type_prediction_set)}, B = {format_value(self.B)}, nb_hidden = {format_value(self.nb_hidden)}, nodes_sim = {format_value(self.nodes_sim)}, activ = {format_value(self.activ)}, seed = {format_value(self.seed)})")
73
+
74
+ def fit(self, X, y, **kwargs):
75
+ """
76
+ Fit the model according to the given training data.
77
+ """
78
+ params_dict = {}
79
+
80
+ for k, v in kwargs.items():
81
+ if k == 'lambda_':
82
+ params_dict["lambda"] = v
83
+ elif '__' in k:
84
+ params_dict[k.replace('__', '.')] = v
85
+ else:
86
+ params_dict[k] = v
87
+
88
+ if isinstance(X, pd.DataFrame):
89
+ self.column_names = X.columns
90
+ X_r = r.matrix(FloatVector(X.values.ravel()),
91
+ byrow=True,
92
+ ncol=X.shape[1],
93
+ nrow=X.shape[0])
94
+ X_r.colnames = StrVector(self.column_names)
95
+ else:
96
+ X_r = r.matrix(FloatVector(X.ravel()),
97
+ byrow=True,
98
+ ncol=X.shape[1],
99
+ nrow=X.shape[0])
100
+
101
+ if isinstance(y, pd.DataFrame) or isinstance(y, pd.Series):
102
+ y = y.values.ravel()
103
+
104
+ self.obj["fit"](X_r,
105
+ FactorVector(IntVector(y)),
106
+ **params_dict)
107
+ self.classes_ = np.unique(y) # /!\ do not remove
108
+ return self
109
+
110
+ def predict_proba(self, X):
111
+ """
112
+ Predict using the model.
113
+ """
114
+
115
+ if isinstance(X, pd.DataFrame):
116
+ X_r = r.matrix(FloatVector(X.values.ravel()),
117
+ byrow=True,
118
+ ncol=X.shape[1],
119
+ nrow=X.shape[0])
120
+ X_r.colnames = StrVector(self.column_names)
121
+ else:
122
+ X_r = r.matrix(FloatVector(X.ravel()),
123
+ byrow=True,
124
+ ncol=X.shape[1],
125
+ nrow=X.shape[0])
126
+
127
+ if self.pi_method == "none":
128
+ if isinstance(X, pd.DataFrame):
129
+ res = self.obj["predict_proba"](X_r)
130
+ return np.asarray(res)
131
+ if isinstance(X, pd.DataFrame):
132
+ return r_list_to_namedtuple(self.obj["predict_proba"](X_r))
133
+
134
+ def predict(self, X):
135
+ """
136
+ Predict using the model.
137
+ """
138
+
139
+ if isinstance(X, pd.DataFrame):
140
+ X_r = r.matrix(FloatVector(X.values.ravel()),
141
+ byrow=True,
142
+ ncol=X.shape[1],
143
+ nrow=X.shape[0])
144
+ X_r.colnames = StrVector(self.column_names)
145
+ else:
146
+ X_r = r.matrix(FloatVector(X.ravel()),
147
+ byrow=True,
148
+ ncol=X.shape[1],
149
+ nrow=X.shape[0])
150
+
151
+ if self.pi_method == "none":
152
+ return (
153
+ np.asarray(
154
+ self.obj["predict"](
155
+ X_r
156
+ )
157
+ ) - 1
158
+ )
159
+
160
+ return r_list_to_namedtuple(self.obj["predict"](X_r))
161
+
@@ -0,0 +1,137 @@
1
+ import numpy as np
2
+ import pandas as pd
3
+ from rpy2.robjects import r
4
+ from rpy2.robjects.packages import importr
5
+ from rpy2.robjects.vectors import FloatVector, StrVector
6
+ from sklearn.base import RegressorMixin
7
+ from .base import Base
8
+ from .utils import format_value, r_list_to_namedtuple
9
+
10
+ base = importr("base")
11
+ stats = importr("stats")
12
+ utils = importr("utils")
13
+
14
+
15
+ class Regressor(Base, RegressorMixin):
16
+ """
17
+ Regressor.
18
+ """
19
+
20
+ def __init__(
21
+ self,
22
+ method="ranger",
23
+ pi_method="none",
24
+ level=95,
25
+ B=100,
26
+ nb_hidden = 0,
27
+ nodes_sim = "sobol",
28
+ activ = "relu",
29
+ seed=123,
30
+ ):
31
+ """
32
+ Initialize the model.
33
+ """
34
+ super().__init__(
35
+ name = "Regressor",
36
+ type = "regression",
37
+ method=method,
38
+ pi_method=pi_method,
39
+ level=level,
40
+ B=B,
41
+ nb_hidden=nb_hidden,
42
+ nodes_sim=nodes_sim,
43
+ activ=activ,
44
+ seed=seed,
45
+ )
46
+
47
+ try:
48
+ r_obj_command = 'suppressWarnings(suppressMessages(library(learningmachine))); ' +\
49
+ 'Regressor$new(method = ' + str(format_value(self.method)) + ', ' +\
50
+ 'pi_method = ' + str(format_value(self.pi_method)) + ', ' +\
51
+ 'level = ' + str(format_value(self.level)) + ', ' +\
52
+ 'B = ' + str(format_value(self.B)) + ', ' +\
53
+ 'nb_hidden = ' + str(format_value(self.nb_hidden)) + ', ' +\
54
+ 'nodes_sim = ' + str(format_value(self.nodes_sim)) + ', ' +\
55
+ 'activ = ' + str(format_value(self.activ)) + ', ' +\
56
+ 'seed = ' + str(format_value(self.seed)) + ')'
57
+ self.obj = r(r_obj_command)
58
+ except Exception:
59
+ try:
60
+ self.obj = r(f"suppressWarnings(suppressMessages(library(learningmachine))); Regressor$new(method = {format_value(self.method)}, pi_method = {format_value(self.pi_method)}, level = {format_value(self.level)}, B = {format_value(self.B)}, nb_hidden = {format_value(self.nb_hidden)}, nodes_sim = {format_value(self.nodes_sim)}, activ = {format_value(self.activ)}, seed = {format_value(self.seed)})")
61
+ except Exception:
62
+ self.obj = r(f"learningmachine::Regressor$new(method = {format_value(self.method)}, pi_method = {format_value(self.pi_method)}, level = {format_value(self.level)}, B = {format_value(self.B)}, nb_hidden = {format_value(self.nb_hidden)}, nodes_sim = {format_value(self.nodes_sim)}, activ = {format_value(self.activ)}, seed = {format_value(self.seed)})")
63
+
64
+ def fit(self, X, y, **kwargs):
65
+ """
66
+ Fit the model according to the given training data.
67
+ """
68
+ params_dict = {}
69
+
70
+ for k, v in kwargs.items():
71
+ if k == 'lambda_':
72
+ params_dict["lambda"] = v
73
+ elif '__' in k:
74
+ params_dict[k.replace('__', '.')] = v
75
+ else:
76
+ params_dict[k] = v
77
+
78
+ if isinstance(X, pd.DataFrame):
79
+ self.column_names = X.columns
80
+ X_r = r.matrix(FloatVector(X.values.ravel()),
81
+ byrow=True,
82
+ ncol=X.shape[1],
83
+ nrow=X.shape[0])
84
+ X_r.colnames = StrVector(self.column_names)
85
+ else:
86
+ X_r = r.matrix(FloatVector(X.ravel()),
87
+ byrow=True,
88
+ ncol=X.shape[1],
89
+ nrow=X.shape[0])
90
+
91
+ if isinstance(y, pd.DataFrame) or isinstance(y, pd.Series):
92
+ y = y.values.ravel()
93
+
94
+ self.obj["fit"](X_r,
95
+ FloatVector(y),
96
+ **params_dict
97
+ )
98
+ return self
99
+
100
+ def predict(self, X):
101
+ """
102
+ Predict using the model.
103
+ """
104
+
105
+ if isinstance(X, pd.DataFrame):
106
+ X_r = r.matrix(FloatVector(X.values.ravel()),
107
+ byrow=True,
108
+ ncol=X.shape[1],
109
+ nrow=X.shape[0])
110
+ X_r.colnames = StrVector(self.column_names)
111
+ else:
112
+ X_r = r.matrix(FloatVector(X.ravel()),
113
+ byrow=True,
114
+ ncol=X.shape[1],
115
+ nrow=X.shape[0])
116
+
117
+ if self.pi_method == "none":
118
+ return(np.asarray(self.obj["predict"](
119
+ X_r
120
+ )))
121
+ return r_list_to_namedtuple(self.obj["predict"](
122
+ X_r
123
+ ))
124
+
125
+ def update(self, newx, newy):
126
+ """
127
+ update the model.
128
+ """
129
+
130
+ newx_r = base.as_vector(FloatVector(newx))
131
+
132
+ self.obj["update"](newx_r,
133
+ base.as_numeric(FloatVector([newy]))
134
+ )
135
+
136
+ return self
137
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: learningmachine
3
- Version: 2.1.0
3
+ Version: 2.2.2
4
4
  Summary: Machine Learning with uncertainty quantification and interpretability
5
5
  Home-page: https://github.com/Techtonique/learningmachine_python
6
6
  Author: T. Moudiki
@@ -169,6 +169,6 @@ setup(
169
169
  packages=find_packages(include=["learningmachine", "learningmachine.*"]),
170
170
  test_suite="tests",
171
171
  url="https://github.com/Techtonique/learningmachine_python",
172
- version="2.1.0",
172
+ version="2.2.2",
173
173
  zip_safe=False,
174
174
  )
@@ -1,54 +0,0 @@
1
- # learningmachine
2
-
3
- ![PyPI](https://img.shields.io/pypi/v/learningmachine) [![PyPI - License](https://img.shields.io/pypi/l/learningmachine)](https://github.com/thierrymoudiki/learningmachine/blob/master/LICENSE) [![Downloads](https://pepy.tech/badge/learningmachine)](https://pepy.tech/project/learningmachine)
4
-
5
-
6
- Machine Learning with uncertainty quantification and interpretability.
7
-
8
- ## Install
9
-
10
- If R packages are not installed automatically when running `pip`, [install it manually](https://cloud.r-project.org/).
11
-
12
- **Development version**
13
-
14
- ```bash
15
- !pip install git+https://github.com/Techtonique/learningmachine_python.git --verbose
16
- ```
17
-
18
- **Stable version**
19
-
20
- ```bash
21
- !pip install learningmachine --verbose
22
- ```
23
-
24
- ## Example
25
-
26
- See also:
27
- - [this notebook](https://colab.research.google.com/github/Techtonique/learningmachine_python/blob/main/learningmachine/demo/thierrymoudiki_20240401_calib.ipynb)
28
- - [this notebook](https://colab.research.google.com/github/Techtonique/learningmachine_python/blob/main/learningmachine/demo/thierrymoudiki_20240508_calib.ipynb)
29
-
30
- ```python
31
- import learningmachine as lm
32
-
33
- from sklearn.datasets import load_diabetes
34
- from sklearn.model_selection import train_test_split
35
- from time import time
36
- from sklearn.metrics import mean_squared_error
37
-
38
- # Regression (linear)
39
- fit_obj = lm.BaseRegressor()
40
- diabetes = load_diabetes()
41
- X = diabetes.data[:150]
42
- y = diabetes.target[:150]
43
-
44
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
45
- random_state=1213)
46
-
47
- start = time()
48
- fit_obj.fit(X_train, y_train)
49
- print("Elapsed time: ", time() - start)
50
-
51
- ## Compute RMSE
52
- rms1 = sqrt(mean_squared_error(y_test, fit_obj.predict(X_test), squared=False))
53
- print(rms1)
54
- ```
@@ -1,132 +0,0 @@
1
- import numpy as np
2
- import sklearn.metrics as skm
3
- from rpy2.robjects import r
4
- from rpy2.robjects.packages import importr
5
- from rpy2.robjects.vectors import (
6
- FloatVector,
7
- IntVector,
8
- FactorVector,
9
- ListVector
10
- )
11
- from sklearn.base import ClassifierMixin
12
- from .base import Base
13
- from .utils import format_value, r_list_to_namedtuple
14
-
15
- base = importr("base")
16
- stats = importr("stats")
17
- utils = importr("utils")
18
-
19
-
20
- class Classifier(Base, ClassifierMixin):
21
- """
22
- Classifier.
23
- """
24
-
25
- def __init__(
26
- self,
27
- method="ranger",
28
- pi_method="none",
29
- level=95,
30
- type_prediction_set="score",
31
- B=100,
32
- nb_hidden = 0,
33
- nodes_sim = "sobol",
34
- activ = "relu",
35
- seed=123,
36
- ):
37
- """
38
- Initialize the model.
39
- """
40
- super().__init__(
41
- name = "Classifier",
42
- type = "classification",
43
- method=method,
44
- pi_method=pi_method,
45
- level=level,
46
- B=B,
47
- nb_hidden=nb_hidden,
48
- nodes_sim=nodes_sim,
49
- activ=activ,
50
- seed=seed,
51
- )
52
-
53
- self.type_prediction_set=type_prediction_set
54
-
55
- try:
56
- r_obj_command = (
57
- 'suppressWarnings(suppressMessages(library(learningmachine))); '
58
- 'Classifier$new(method = ' + str(format_value(self.method)) + ', '
59
- 'pi_method = ' + str(format_value(self.pi_method)) + ', '
60
- 'level = ' + str(format_value(self.level)) + ', '
61
- 'type_prediction_set = ' + str(format_value(self.type_prediction_set)) + ', '
62
- 'B = ' + str(format_value(self.B)) + ', '
63
- 'nb_hidden = ' + str(format_value(self.nb_hidden)) + ', '
64
- 'nodes_sim = ' + str(format_value(self.nodes_sim)) + ', '
65
- 'activ = ' + str(format_value(self.activ)) + ', '
66
- 'seed = ' + str(format_value(self.seed)) + ')')
67
- self.obj = r(r_obj_command)
68
- except Exception:
69
- try:
70
- self.obj = r(f"suppressWarnings(suppressMessages(library(learningmachine))); Classifier$new(method = {format_value(self.method)}, pi_method = {format_value(self.pi_method)}, level = {format_value(self.level)}, type_prediction_set = {format_value(self.type_prediction_set)}, B = {format_value(self.B)}, nb_hidden = {format_value(self.nb_hidden)}, nodes_sim = {format_value(self.nodes_sim)}, activ = {format_value(self.activ)}, seed = {format_value(self.seed)})")
71
- except Exception:
72
- self.obj = r(
73
- f"learningmachine::Classifier$new(method = {format_value(self.method)}, pi_method = {format_value(self.pi_method)}, level = {format_value(self.level)}, type_prediction_set = {format_value(self.type_prediction_set)}, B = {format_value(self.B)}, nb_hidden = {format_value(self.nb_hidden)}, nodes_sim = {format_value(self.nodes_sim)}, activ = {format_value(self.activ)}, seed = {format_value(self.seed)})"
74
- )
75
-
76
- def fit(self, X, y, **kwargs):
77
- """
78
- Fit the model according to the given training data.
79
- """
80
- params_dict = {}
81
- for k, v in kwargs.items():
82
- params_dict[k.replace('_', '.')] = v
83
- self.obj["fit"](r.matrix(FloatVector(X.ravel()),
84
- byrow=True,
85
- ncol=X.shape[1],
86
- nrow=X.shape[0]),
87
- FactorVector(IntVector(y)),
88
- **params_dict)
89
- self.classes_ = np.unique(y) # /!\ do not remove
90
- return self
91
-
92
- def predict_proba(self, X):
93
- """
94
- Predict using the model.
95
- """
96
- if self.pi_method == "none":
97
- res = self.obj["predict_proba"](
98
- r.matrix(FloatVector(X.ravel()),
99
- byrow=True,
100
- ncol=X.shape[1],
101
- nrow=X.shape[0])
102
- )
103
- return np.asarray(res)
104
- return r_list_to_namedtuple(self.obj["predict_proba"](
105
- r.matrix(FloatVector(X.ravel()),
106
- byrow=True,
107
- ncol=X.shape[1],
108
- nrow=X.shape[0])
109
- ))
110
-
111
- def predict(self, X):
112
- """
113
- Predict using the model.
114
- """
115
- if self.pi_method == "none":
116
- return (
117
- np.asarray(
118
- self.obj["predict"](
119
- r.matrix(FloatVector(X.ravel()),
120
- byrow=True,
121
- ncol=X.shape[1],
122
- nrow=X.shape[0])
123
- )
124
- ) - 1
125
- )
126
- return r_list_to_namedtuple(self.obj["predict"](
127
- r.matrix(FloatVector(X.ravel()),
128
- byrow=True,
129
- ncol=X.shape[1],
130
- nrow=X.shape[0])
131
- ))
132
-
@@ -1,100 +0,0 @@
1
- import numpy as np
2
- from rpy2.robjects import r
3
- from rpy2.robjects.packages import importr
4
- from rpy2.robjects.vectors import FloatVector, ListVector
5
- from sklearn.base import RegressorMixin
6
- from .base import Base
7
- from .utils import format_value, r_list_to_namedtuple
8
-
9
- base = importr("base")
10
- stats = importr("stats")
11
- utils = importr("utils")
12
-
13
-
14
- class Regressor(Base, RegressorMixin):
15
- """
16
- Regressor.
17
- """
18
-
19
- def __init__(
20
- self,
21
- method="ranger",
22
- pi_method="none",
23
- level=95,
24
- B=100,
25
- nb_hidden = 0,
26
- nodes_sim = "sobol",
27
- activ = "relu",
28
- seed=123,
29
- ):
30
- """
31
- Initialize the model.
32
- """
33
- super().__init__(
34
- name = "Regressor",
35
- type = "regression",
36
- method=method,
37
- pi_method=pi_method,
38
- level=level,
39
- B=B,
40
- nb_hidden=nb_hidden,
41
- nodes_sim=nodes_sim,
42
- activ=activ,
43
- seed=seed,
44
- )
45
-
46
- try:
47
- r_obj_command = (
48
- 'suppressWarnings(suppressMessages(library(learningmachine))); '
49
- 'Regressor$new(method = ' + str(format_value(self.method)) + ', '
50
- 'pi_method = ' + str(format_value(self.pi_method)) + ', '
51
- 'level = ' + str(format_value(self.level)) + ', '
52
- 'B = ' + str(format_value(self.B)) + ', '
53
- 'nb_hidden = ' + str(format_value(self.nb_hidden)) + ', '
54
- 'nodes_sim = ' + str(format_value(self.nodes_sim)) + ', '
55
- 'activ = ' + str(format_value(self.activ)) + ', '
56
- 'seed = ' + str(format_value(self.seed)) + ')')
57
- self.obj = r(r_obj_command)
58
- except Exception:
59
- try:
60
- self.obj = r(f"suppressWarnings(suppressMessages(library(learningmachine))); Regressor$new(method = {format_value(self.method)}, pi_method = {format_value(self.pi_method)}, level = {format_value(self.level)}, B = {format_value(self.B)}, nb_hidden = {format_value(self.nb_hidden)}, nodes_sim = {format_value(self.nodes_sim)}, activ = {format_value(self.activ)}, seed = {format_value(self.seed)})")
61
- except Exception:
62
- self.obj = r(
63
- f"learningmachine::Regressor$new(method = {format_value(self.method)}, pi_method = {format_value(self.pi_method)}, level = {format_value(self.level)}, B = {format_value(self.B)}, nb_hidden = {format_value(self.nb_hidden)}, nodes_sim = {format_value(self.nodes_sim)}, activ = {format_value(self.activ)}, seed = {format_value(self.seed)})"
64
- )
65
-
66
- def fit(self, X, y, **kwargs):
67
- """
68
- Fit the model according to the given training data.
69
- """
70
- params_dict = {}
71
- for k, v in kwargs.items():
72
- params_dict[k.replace('_', '.')] = v
73
- self.obj["fit"](
74
- r.matrix(FloatVector(X.ravel()),
75
- byrow=True,
76
- ncol=X.shape[1],
77
- nrow=X.shape[0]),
78
- FloatVector(y),
79
- **params_dict
80
- )
81
- return self
82
-
83
- def predict(self, X):
84
- """
85
- Predict using the model.
86
- """
87
- if self.pi_method == "none":
88
- return(np.asarray(self.obj["predict"](
89
- r.matrix(FloatVector(X.ravel()),
90
- byrow=True,
91
- ncol=X.shape[1],
92
- nrow=X.shape[0])
93
- )))
94
- return r_list_to_namedtuple(self.obj["predict"](
95
- r.matrix(FloatVector(X.ravel()),
96
- byrow=True,
97
- ncol=X.shape[1],
98
- nrow=X.shape[0])
99
- ))
100
-
File without changes