learningmachine 2.0.1__tar.gz → 2.2.0__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.
- {learningmachine-2.0.1 → learningmachine-2.2.0}/PKG-INFO +1 -1
- learningmachine-2.2.0/README.md +118 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/learningmachine/base.py +28 -46
- learningmachine-2.2.0/learningmachine/classifier.py +161 -0
- learningmachine-2.2.0/learningmachine/regression.py +124 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/learningmachine.egg-info/PKG-INFO +1 -1
- {learningmachine-2.0.1 → learningmachine-2.2.0}/setup.py +33 -13
- learningmachine-2.0.1/README.md +0 -54
- learningmachine-2.0.1/learningmachine/classifier.py +0 -140
- learningmachine-2.0.1/learningmachine/regression.py +0 -109
- {learningmachine-2.0.1 → learningmachine-2.2.0}/CONTRIBUTING.rst +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/HISTORY.rst +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/LICENSE +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/MANIFEST.in +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/docs/Makefile +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/docs/conf.py +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/docs/contributing.rst +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/docs/history.rst +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/docs/index.rst +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/docs/installation.rst +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/docs/make.bat +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/docs/readme.rst +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/docs/usage.rst +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/learningmachine/__init__.py +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/learningmachine/utils.py +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/learningmachine.egg-info/SOURCES.txt +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/learningmachine.egg-info/dependency_links.txt +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/learningmachine.egg-info/not-zip-safe +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/learningmachine.egg-info/requires.txt +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/learningmachine.egg-info/top_level.txt +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/setup.cfg +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/tests/__init__.py +0 -0
- {learningmachine-2.0.1 → learningmachine-2.2.0}/tests/test_learningmachine.py +0 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# learningmachine
|
|
2
|
+
|
|
3
|
+
 [](https://github.com/thierrymoudiki/learningmachine/blob/master/LICENSE) [](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
|
-
|
|
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
|
-
|
|
218
|
-
show_progress = True
|
|
219
|
-
|
|
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
|
|
238
|
+
assert class_index is not None, "For classifiers, 'class_index' must be provided"
|
|
226
239
|
|
|
227
|
-
self.obj["summary"](X =
|
|
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
|
-
|
|
238
|
-
|
|
239
|
-
self.obj["summary"](X =
|
|
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
|
-
|
|
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,124 @@
|
|
|
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
|
+
|
|
@@ -72,34 +72,50 @@ def install_packages():
|
|
|
72
72
|
try:
|
|
73
73
|
subprocess.run(["Rscript", "-e", "utils::install.packages('remotes', dependencies=TRUE)"])
|
|
74
74
|
subprocess.run(["Rscript", "-e", "utils::install.packages(c('R6', 'Rcpp', 'skimr'), dependencies=TRUE)"])
|
|
75
|
-
#subprocess.run(["Rscript", "-e", "utils::install.packages('learningmachine', repos='https://techtonique.r-universe.dev', dependencies=TRUE)"])
|
|
76
75
|
subprocess.run(["Rscript", "-e", "remotes::install_github('Techtonique/learningmachine')"])
|
|
77
|
-
except:
|
|
76
|
+
except subprocess.CalledProcessError as e:
|
|
77
|
+
print(f"Error occurred: {e}")
|
|
78
|
+
print(f"Return code: {e.returncode}")
|
|
79
|
+
print(f"Output: {e.output}")
|
|
80
|
+
print(f"Stderr: {e.stderr}")
|
|
78
81
|
try:
|
|
79
82
|
subprocess.run(["mkdir", "-p", "r-learningmachine"])
|
|
80
83
|
subprocess.run(["Rscript", "-e", "utils::install.packages('remotes', lib='r-learningmachine', dependencies=TRUE)"])
|
|
81
84
|
subprocess.run(["Rscript", "-e", "utils::install.packages(c('R6', 'Rcpp', 'skimr'), lib='r-learningmachine', dependencies=TRUE)"])
|
|
82
|
-
#subprocess.run(["Rscript", "-e", "utils::install.packages('learningmachine', repos='https://techtonique.r-universe.dev', lib='r-learningmachine', dependencies=TRUE)"])
|
|
83
85
|
subprocess.run(["Rscript", "-e", "remotes::install_github('Techtonique/learningmachine', lib='r-learningmachine')"])
|
|
84
|
-
except:
|
|
86
|
+
except subprocess.CalledProcessError as e:
|
|
87
|
+
print(f"Error occurred: {e}")
|
|
88
|
+
print(f"Return code: {e.returncode}")
|
|
89
|
+
print(f"Output: {e.output}")
|
|
90
|
+
print(f"Stderr: {e.stderr}")
|
|
85
91
|
try:
|
|
86
92
|
subprocess.run(["Rscript", "-e", "utils::install.packages('remotes', dependencies=TRUE)"])
|
|
87
93
|
subprocess.run(["Rscript", "-e", "utils::install.packages(c('R6', 'Rcpp', 'skimr'), dependencies=TRUE)"])
|
|
88
|
-
#subprocess.run(["Rscript", "-e", "utils::install.packages('learningmachine', repos='https://techtonique.r-universe.dev', dependencies=TRUE)"])
|
|
89
94
|
subprocess.run(["Rscript", "-e", "remotes::install_github('Techtonique/learningmachine')"])
|
|
90
|
-
except:
|
|
95
|
+
except subprocess.CalledProcessError as e:
|
|
96
|
+
print(f"Error occurred: {e}")
|
|
97
|
+
print(f"Return code: {e.returncode}")
|
|
98
|
+
print(f"Output: {e.output}")
|
|
99
|
+
print(f"Stderr: {e.stderr}")
|
|
91
100
|
try:
|
|
92
101
|
subprocess.run(["mkdir", "-p", "r-learningmachine"])
|
|
93
102
|
subprocess.run(["Rscript", "-e", "utils::install.packages('remotes', lib='r-learningmachine', dependencies=TRUE)"])
|
|
94
103
|
subprocess.run(["Rscript", "-e", "utils::install.packages(c('R6', 'Rcpp', 'skimr'), lib='r-learningmachine', dependencies=TRUE)"])
|
|
95
|
-
#subprocess.run(["Rscript", "-e", "utils::install.packages('learningmachine', repos='https://techtonique.r-universe.dev', lib='r-learningmachine', dependencies=TRUE)"])
|
|
96
104
|
subprocess.run(["Rscript", "-e", "remotes::install_github('Techtonique/learningmachine', lib='r-learningmachine')"])
|
|
97
|
-
except:
|
|
105
|
+
except subprocess.CalledProcessError as e:
|
|
106
|
+
print(f"Error occurred: {e}")
|
|
107
|
+
print(f"Return code: {e.returncode}")
|
|
108
|
+
print(f"Output: {e.output}")
|
|
109
|
+
print(f"Stderr: {e.stderr}")
|
|
98
110
|
try:
|
|
99
111
|
subprocess.run(["Rscript", "-e", "utils::install.packages('remotes', dependencies=TRUE)"])
|
|
100
112
|
subprocess.run(["Rscript", "-e", "utils::install.packages(c('R6', 'Rcpp', 'skimr'), dependencies=TRUE)"])
|
|
101
113
|
subprocess.run(["Rscript", "-e", "remotes::install_github('Techtonique/learningmachine')"])
|
|
102
|
-
except:
|
|
114
|
+
except subprocess.CalledProcessError as e:
|
|
115
|
+
print(f"Error occurred: {e}")
|
|
116
|
+
print(f"Return code: {e.returncode}")
|
|
117
|
+
print(f"Output: {e.output}")
|
|
118
|
+
print(f"Stderr: {e.stderr}")
|
|
103
119
|
subprocess.run(["mkdir", "-p", "r-learningmachine"])
|
|
104
120
|
subprocess.run(["Rscript", "-e", "utils::install.packages('remotes', lib='r-learningmachine', dependencies=TRUE)"])
|
|
105
121
|
subprocess.run(["Rscript", "-e", "utils::install.packages(c('R6', 'Rcpp', 'skimr'), lib='r-learningmachine', dependencies=TRUE)"])
|
|
@@ -108,10 +124,14 @@ def install_packages():
|
|
|
108
124
|
|
|
109
125
|
# Check if R is installed; if not, install it
|
|
110
126
|
if not check_r_installed():
|
|
111
|
-
|
|
112
|
-
|
|
127
|
+
install_r_prompt = int(input("Try installing R? 1-yes, 2-no"))
|
|
128
|
+
if install_r_prompt == 1:
|
|
129
|
+
print("Installing R...")
|
|
130
|
+
install_r()
|
|
131
|
+
else:
|
|
132
|
+
raise ValueError('Try installing R manually first.')
|
|
113
133
|
else:
|
|
114
|
-
print("No installation needed.")
|
|
134
|
+
print("No R installation needed.")
|
|
115
135
|
|
|
116
136
|
install_packages()
|
|
117
137
|
|
|
@@ -149,6 +169,6 @@ setup(
|
|
|
149
169
|
packages=find_packages(include=["learningmachine", "learningmachine.*"]),
|
|
150
170
|
test_suite="tests",
|
|
151
171
|
url="https://github.com/Techtonique/learningmachine_python",
|
|
152
|
-
version="2.0
|
|
172
|
+
version="2.2.0",
|
|
153
173
|
zip_safe=False,
|
|
154
174
|
)
|
learningmachine-2.0.1/README.md
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
# learningmachine
|
|
2
|
-
|
|
3
|
-
 [](https://github.com/thierrymoudiki/learningmachine/blob/master/LICENSE) [](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,140 +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
|
-
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)})")
|
|
57
|
-
except NotImplementedError:
|
|
58
|
-
self.obj = r(
|
|
59
|
-
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)})"
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
# try:
|
|
63
|
-
# self.load_learningmachine()
|
|
64
|
-
# self.obj = r(
|
|
65
|
-
# 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)})"
|
|
66
|
-
# )
|
|
67
|
-
# except NotImplementedError as e:
|
|
68
|
-
# try:
|
|
69
|
-
# base.suppressWarnings(base.suppressMessages(r.library("learningmachine")))
|
|
70
|
-
# self.obj = r(
|
|
71
|
-
# f"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)})"
|
|
72
|
-
# )
|
|
73
|
-
# except NotImplementedError as e:
|
|
74
|
-
# try:
|
|
75
|
-
# self.obj = r(
|
|
76
|
-
# f"""
|
|
77
|
-
# suppressWarnings(suppressMessages(library(learningmachine)));
|
|
78
|
-
# 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)})
|
|
79
|
-
# """
|
|
80
|
-
# )
|
|
81
|
-
# except NotImplementedError as e:
|
|
82
|
-
# print("R package can't be loaded: ", e)
|
|
83
|
-
|
|
84
|
-
def fit(self, X, y, **kwargs):
|
|
85
|
-
"""
|
|
86
|
-
Fit the model according to the given training data.
|
|
87
|
-
"""
|
|
88
|
-
params_dict = {}
|
|
89
|
-
for k, v in kwargs.items():
|
|
90
|
-
params_dict[k.replace('_', '.')] = v
|
|
91
|
-
self.obj["fit"](r.matrix(FloatVector(X.ravel()),
|
|
92
|
-
byrow=True,
|
|
93
|
-
ncol=X.shape[1],
|
|
94
|
-
nrow=X.shape[0]),
|
|
95
|
-
FactorVector(IntVector(y)),
|
|
96
|
-
**params_dict)
|
|
97
|
-
self.classes_ = np.unique(y) # /!\ do not remove
|
|
98
|
-
return self
|
|
99
|
-
|
|
100
|
-
def predict_proba(self, X):
|
|
101
|
-
"""
|
|
102
|
-
Predict using the model.
|
|
103
|
-
"""
|
|
104
|
-
if self.pi_method == "none":
|
|
105
|
-
res = self.obj["predict_proba"](
|
|
106
|
-
r.matrix(FloatVector(X.ravel()),
|
|
107
|
-
byrow=True,
|
|
108
|
-
ncol=X.shape[1],
|
|
109
|
-
nrow=X.shape[0])
|
|
110
|
-
)
|
|
111
|
-
return np.asarray(res)
|
|
112
|
-
return r_list_to_namedtuple(self.obj["predict_proba"](
|
|
113
|
-
r.matrix(FloatVector(X.ravel()),
|
|
114
|
-
byrow=True,
|
|
115
|
-
ncol=X.shape[1],
|
|
116
|
-
nrow=X.shape[0])
|
|
117
|
-
))
|
|
118
|
-
|
|
119
|
-
def predict(self, X):
|
|
120
|
-
"""
|
|
121
|
-
Predict using the model.
|
|
122
|
-
"""
|
|
123
|
-
if self.pi_method == "none":
|
|
124
|
-
return (
|
|
125
|
-
np.asarray(
|
|
126
|
-
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
|
|
133
|
-
)
|
|
134
|
-
return r_list_to_namedtuple(self.obj["predict"](
|
|
135
|
-
r.matrix(FloatVector(X.ravel()),
|
|
136
|
-
byrow=True,
|
|
137
|
-
ncol=X.shape[1],
|
|
138
|
-
nrow=X.shape[0])
|
|
139
|
-
))
|
|
140
|
-
|
|
@@ -1,109 +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
|
-
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)})")
|
|
48
|
-
except NotImplementedError:
|
|
49
|
-
self.obj = r(
|
|
50
|
-
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)})"
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
# try:
|
|
54
|
-
# self.load_learningmachine()
|
|
55
|
-
# self.obj = r(
|
|
56
|
-
# 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)})"
|
|
57
|
-
# )
|
|
58
|
-
# except NotImplementedError as e:
|
|
59
|
-
# try:
|
|
60
|
-
# base.suppressWarnings(base.suppressMessages(r.library("learningmachine")))
|
|
61
|
-
# self.obj = r(
|
|
62
|
-
# f"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
|
-
# except NotImplementedError as e:
|
|
65
|
-
# try:
|
|
66
|
-
# self.obj = r(
|
|
67
|
-
# f"""
|
|
68
|
-
# suppressWarnings(suppressMessages(library(learningmachine)));
|
|
69
|
-
# 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)})
|
|
70
|
-
# """
|
|
71
|
-
# )
|
|
72
|
-
# except NotImplementedError as e:
|
|
73
|
-
# print("R package can't be loaded: ", e)
|
|
74
|
-
|
|
75
|
-
def fit(self, X, y, **kwargs):
|
|
76
|
-
"""
|
|
77
|
-
Fit the model according to the given training data.
|
|
78
|
-
"""
|
|
79
|
-
params_dict = {}
|
|
80
|
-
for k, v in kwargs.items():
|
|
81
|
-
params_dict[k.replace('_', '.')] = v
|
|
82
|
-
self.obj["fit"](
|
|
83
|
-
r.matrix(FloatVector(X.ravel()),
|
|
84
|
-
byrow=True,
|
|
85
|
-
ncol=X.shape[1],
|
|
86
|
-
nrow=X.shape[0]),
|
|
87
|
-
FloatVector(y),
|
|
88
|
-
**params_dict
|
|
89
|
-
)
|
|
90
|
-
return self
|
|
91
|
-
|
|
92
|
-
def predict(self, X):
|
|
93
|
-
"""
|
|
94
|
-
Predict using the model.
|
|
95
|
-
"""
|
|
96
|
-
if self.pi_method == "none":
|
|
97
|
-
return(np.asarray(self.obj["predict"](
|
|
98
|
-
r.matrix(FloatVector(X.ravel()),
|
|
99
|
-
byrow=True,
|
|
100
|
-
ncol=X.shape[1],
|
|
101
|
-
nrow=X.shape[0])
|
|
102
|
-
)))
|
|
103
|
-
return r_list_to_namedtuple(self.obj["predict"](
|
|
104
|
-
r.matrix(FloatVector(X.ravel()),
|
|
105
|
-
byrow=True,
|
|
106
|
-
ncol=X.shape[1],
|
|
107
|
-
nrow=X.shape[0])
|
|
108
|
-
))
|
|
109
|
-
|
|
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
|
{learningmachine-2.0.1 → learningmachine-2.2.0}/learningmachine.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|