learningmachine 2.2.2__tar.gz → 2.3.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.2.2 → learningmachine-2.3.0}/PKG-INFO +1 -1
- {learningmachine-2.2.2 → learningmachine-2.3.0}/README.md +1 -1
- {learningmachine-2.2.2 → learningmachine-2.3.0}/learningmachine/base.py +68 -42
- learningmachine-2.3.0/learningmachine/classifier.py +188 -0
- learningmachine-2.3.0/learningmachine/regression.py +157 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/learningmachine/utils.py +20 -11
- {learningmachine-2.2.2 → learningmachine-2.3.0}/learningmachine.egg-info/PKG-INFO +1 -1
- {learningmachine-2.2.2 → learningmachine-2.3.0}/setup.py +1 -1
- learningmachine-2.2.2/learningmachine/classifier.py +0 -161
- learningmachine-2.2.2/learningmachine/regression.py +0 -137
- {learningmachine-2.2.2 → learningmachine-2.3.0}/CONTRIBUTING.rst +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/HISTORY.rst +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/LICENSE +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/MANIFEST.in +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/docs/Makefile +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/docs/conf.py +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/docs/contributing.rst +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/docs/history.rst +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/docs/index.rst +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/docs/installation.rst +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/docs/make.bat +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/docs/readme.rst +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/docs/usage.rst +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/learningmachine/__init__.py +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/learningmachine.egg-info/SOURCES.txt +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/learningmachine.egg-info/dependency_links.txt +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/learningmachine.egg-info/not-zip-safe +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/learningmachine.egg-info/requires.txt +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/learningmachine.egg-info/top_level.txt +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/setup.cfg +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/tests/__init__.py +0 -0
- {learningmachine-2.2.2 → learningmachine-2.3.0}/tests/test_learningmachine.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# learningmachine
|
|
2
2
|
|
|
3
|
-
 [](https://github.com/thierrymoudiki/learningmachine/blob/master/LICENSE) [](https://pepy.tech/project/learningmachine)
|
|
3
|
+
 [](https://github.com/thierrymoudiki/learningmachine/blob/master/LICENSE) [](https://pepy.tech/project/learningmachine) [](https://techtonique.github.io/learningmachine/)
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
Machine Learning with uncertainty quantification and interpretability.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import pandas as pd
|
|
1
|
+
import pandas as pd
|
|
2
2
|
import sklearn.metrics as skm
|
|
3
3
|
import subprocess
|
|
4
4
|
from functools import lru_cache
|
|
@@ -32,9 +32,9 @@ class Base(BaseEstimator):
|
|
|
32
32
|
pi_method="kdesplitconformal",
|
|
33
33
|
level=95,
|
|
34
34
|
B=100,
|
|
35
|
-
nb_hidden
|
|
36
|
-
nodes_sim
|
|
37
|
-
activ
|
|
35
|
+
nb_hidden=0,
|
|
36
|
+
nodes_sim="sobol",
|
|
37
|
+
activ="relu",
|
|
38
38
|
params=None,
|
|
39
39
|
seed=123,
|
|
40
40
|
):
|
|
@@ -47,15 +47,23 @@ class Base(BaseEstimator):
|
|
|
47
47
|
self.method = method
|
|
48
48
|
self.pi_method = pi_method
|
|
49
49
|
self.level = level
|
|
50
|
-
self.B = B
|
|
50
|
+
self.B = B
|
|
51
51
|
self.nb_hidden = nb_hidden
|
|
52
|
-
assert nodes_sim in (
|
|
53
|
-
"
|
|
52
|
+
assert nodes_sim in (
|
|
53
|
+
"sobol",
|
|
54
|
+
"halton",
|
|
55
|
+
"unif",
|
|
56
|
+
), "must have nodes_sim in ('sobol', 'halton', 'unif')"
|
|
54
57
|
self.nodes_sim = "sobol"
|
|
55
|
-
assert activ in (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
58
|
+
assert activ in (
|
|
59
|
+
"relu",
|
|
60
|
+
"sigmoid",
|
|
61
|
+
"tanh",
|
|
62
|
+
"leakyrelu",
|
|
63
|
+
"elu",
|
|
64
|
+
"linear",
|
|
65
|
+
), "must have activ in ('relu', 'sigmoid', 'tanh', 'leakyrelu', 'elu', 'linear')"
|
|
66
|
+
self.activ = activ
|
|
59
67
|
self.params = params
|
|
60
68
|
self.seed = seed
|
|
61
69
|
self.obj = None
|
|
@@ -63,8 +71,10 @@ class Base(BaseEstimator):
|
|
|
63
71
|
|
|
64
72
|
def load_learningmachine(self):
|
|
65
73
|
# Install R packages
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
# check "learningmachine" is installed
|
|
75
|
+
commands1_lm = 'base::system.file(package = "learningmachine")'
|
|
76
|
+
# check "learningmachine" is installed locally
|
|
77
|
+
commands2_lm = 'base::system.file("learningmachine_r", package = "learningmachine")'
|
|
68
78
|
exec_commands1_lm = subprocess.run(
|
|
69
79
|
["Rscript", "-e", commands1_lm], capture_output=True, text=True
|
|
70
80
|
)
|
|
@@ -102,7 +112,9 @@ class Base(BaseEstimator):
|
|
|
102
112
|
)
|
|
103
113
|
except: # well, we tried
|
|
104
114
|
try:
|
|
105
|
-
r(
|
|
115
|
+
r(
|
|
116
|
+
"try(suppressWarnings(suppressMessages(library('learningmachine'))), silence=TRUE)"
|
|
117
|
+
)
|
|
106
118
|
except: # well, we tried everything at this point
|
|
107
119
|
r(
|
|
108
120
|
"try(suppressWarnings(suppressMessages(library('learningmachine', lib.loc='learningmachine_r'))), silence=TRUE)"
|
|
@@ -214,43 +226,57 @@ class Base(BaseEstimator):
|
|
|
214
226
|
|
|
215
227
|
return scoring_options[scoring](y, preds, **kwargs)
|
|
216
228
|
|
|
217
|
-
def summary(
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
229
|
+
def summary(
|
|
230
|
+
self,
|
|
231
|
+
X,
|
|
232
|
+
y,
|
|
233
|
+
class_index=None,
|
|
234
|
+
cl=None,
|
|
235
|
+
type_ci="student",
|
|
236
|
+
show_progress=True,
|
|
237
|
+
):
|
|
238
|
+
|
|
239
|
+
if isinstance(X, pd.DataFrame):
|
|
240
|
+
X_r = r.matrix(
|
|
241
|
+
FloatVector(X.values.ravel()),
|
|
242
|
+
byrow=True,
|
|
243
|
+
ncol=X.shape[1],
|
|
244
|
+
nrow=X.shape[0],
|
|
245
|
+
)
|
|
227
246
|
X_r.colnames = StrVector(self.column_names)
|
|
228
247
|
else:
|
|
229
|
-
X_r = r.matrix(
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
248
|
+
X_r = r.matrix(
|
|
249
|
+
FloatVector(X.ravel()),
|
|
250
|
+
byrow=True,
|
|
251
|
+
ncol=X.shape[1],
|
|
252
|
+
nrow=X.shape[0],
|
|
253
|
+
)
|
|
254
|
+
|
|
234
255
|
if cl is None:
|
|
235
256
|
|
|
236
257
|
if self.type == "classification":
|
|
237
258
|
|
|
238
|
-
assert
|
|
259
|
+
assert (
|
|
260
|
+
class_index is not None
|
|
261
|
+
), "For classifiers, 'class_index' must be provided"
|
|
239
262
|
|
|
240
|
-
return self.obj["summary"](
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
263
|
+
return self.obj["summary"](
|
|
264
|
+
X=X_r,
|
|
265
|
+
y=FactorVector(IntVector(y)),
|
|
266
|
+
class_index=int(class_index) + 1,
|
|
267
|
+
type_ci=StrVector([type_ci]),
|
|
268
|
+
show_progress=show_progress,
|
|
244
269
|
)
|
|
245
|
-
|
|
270
|
+
|
|
246
271
|
elif self.type == "regression":
|
|
247
|
-
|
|
248
|
-
return self.obj["summary"](
|
|
249
|
-
|
|
250
|
-
|
|
272
|
+
|
|
273
|
+
return self.obj["summary"](
|
|
274
|
+
X=X_r,
|
|
275
|
+
y=FloatVector(y),
|
|
276
|
+
type_ci=StrVector([type_ci]),
|
|
277
|
+
show_progress=show_progress,
|
|
251
278
|
)
|
|
252
|
-
|
|
253
|
-
else: # cl is not None, parallel computing
|
|
254
279
|
|
|
255
|
-
|
|
280
|
+
else: # cl is not None, parallel computing
|
|
256
281
|
|
|
282
|
+
pass
|
|
@@ -0,0 +1,188 @@
|
|
|
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 = (
|
|
58
|
+
"suppressWarnings(suppressMessages(library(learningmachine))); "
|
|
59
|
+
+ "Classifier$new(method = "
|
|
60
|
+
+ str(format_value(self.method))
|
|
61
|
+
+ ", "
|
|
62
|
+
+ "pi_method = "
|
|
63
|
+
+ str(format_value(self.pi_method))
|
|
64
|
+
+ ", "
|
|
65
|
+
+ "level = "
|
|
66
|
+
+ str(format_value(self.level))
|
|
67
|
+
+ ", "
|
|
68
|
+
+ "type_prediction_set = "
|
|
69
|
+
+ str(format_value(self.type_prediction_set))
|
|
70
|
+
+ ", "
|
|
71
|
+
+ "B = "
|
|
72
|
+
+ str(format_value(self.B))
|
|
73
|
+
+ ", "
|
|
74
|
+
+ "nb_hidden = "
|
|
75
|
+
+ str(format_value(self.nb_hidden))
|
|
76
|
+
+ ", "
|
|
77
|
+
+ "nodes_sim = "
|
|
78
|
+
+ str(format_value(self.nodes_sim))
|
|
79
|
+
+ ", "
|
|
80
|
+
+ "activ = "
|
|
81
|
+
+ str(format_value(self.activ))
|
|
82
|
+
+ ", "
|
|
83
|
+
+ "seed = "
|
|
84
|
+
+ str(format_value(self.seed))
|
|
85
|
+
+ ")"
|
|
86
|
+
)
|
|
87
|
+
self.obj = r(r_obj_command)
|
|
88
|
+
except Exception:
|
|
89
|
+
try:
|
|
90
|
+
self.obj = r(
|
|
91
|
+
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)})"
|
|
92
|
+
)
|
|
93
|
+
except Exception:
|
|
94
|
+
self.obj = r(
|
|
95
|
+
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)})"
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
def fit(self, X, y, **kwargs):
|
|
99
|
+
"""
|
|
100
|
+
Fit the model according to the given training data.
|
|
101
|
+
"""
|
|
102
|
+
params_dict = {}
|
|
103
|
+
|
|
104
|
+
for k, v in kwargs.items():
|
|
105
|
+
if k == "lambda_":
|
|
106
|
+
params_dict["lambda"] = v
|
|
107
|
+
elif "__" in k:
|
|
108
|
+
params_dict[k.replace("__", ".")] = v
|
|
109
|
+
else:
|
|
110
|
+
params_dict[k] = v
|
|
111
|
+
|
|
112
|
+
if isinstance(X, pd.DataFrame):
|
|
113
|
+
self.column_names = X.columns
|
|
114
|
+
X_r = r.matrix(
|
|
115
|
+
FloatVector(X.values.ravel()),
|
|
116
|
+
byrow=True,
|
|
117
|
+
ncol=X.shape[1],
|
|
118
|
+
nrow=X.shape[0],
|
|
119
|
+
)
|
|
120
|
+
X_r.colnames = StrVector(self.column_names)
|
|
121
|
+
else:
|
|
122
|
+
X_r = r.matrix(
|
|
123
|
+
FloatVector(X.ravel()),
|
|
124
|
+
byrow=True,
|
|
125
|
+
ncol=X.shape[1],
|
|
126
|
+
nrow=X.shape[0],
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
if isinstance(y, pd.DataFrame) or isinstance(y, pd.Series):
|
|
130
|
+
y = y.values.ravel()
|
|
131
|
+
|
|
132
|
+
self.obj["fit"](X_r, FactorVector(IntVector(y)), **params_dict)
|
|
133
|
+
self.classes_ = np.unique(y) # /!\ do not remove
|
|
134
|
+
return self
|
|
135
|
+
|
|
136
|
+
def predict_proba(self, X):
|
|
137
|
+
"""
|
|
138
|
+
Predict using the model.
|
|
139
|
+
"""
|
|
140
|
+
|
|
141
|
+
if isinstance(X, pd.DataFrame):
|
|
142
|
+
X_r = r.matrix(
|
|
143
|
+
FloatVector(X.values.ravel()),
|
|
144
|
+
byrow=True,
|
|
145
|
+
ncol=X.shape[1],
|
|
146
|
+
nrow=X.shape[0],
|
|
147
|
+
)
|
|
148
|
+
X_r.colnames = StrVector(self.column_names)
|
|
149
|
+
else:
|
|
150
|
+
X_r = r.matrix(
|
|
151
|
+
FloatVector(X.ravel()),
|
|
152
|
+
byrow=True,
|
|
153
|
+
ncol=X.shape[1],
|
|
154
|
+
nrow=X.shape[0],
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
if self.pi_method == "none":
|
|
158
|
+
if isinstance(X, pd.DataFrame):
|
|
159
|
+
res = self.obj["predict_proba"](X_r)
|
|
160
|
+
return np.asarray(res)
|
|
161
|
+
if isinstance(X, pd.DataFrame):
|
|
162
|
+
return r_list_to_namedtuple(self.obj["predict_proba"](X_r))
|
|
163
|
+
|
|
164
|
+
def predict(self, X):
|
|
165
|
+
"""
|
|
166
|
+
Predict using the model.
|
|
167
|
+
"""
|
|
168
|
+
|
|
169
|
+
if isinstance(X, pd.DataFrame):
|
|
170
|
+
X_r = r.matrix(
|
|
171
|
+
FloatVector(X.values.ravel()),
|
|
172
|
+
byrow=True,
|
|
173
|
+
ncol=X.shape[1],
|
|
174
|
+
nrow=X.shape[0],
|
|
175
|
+
)
|
|
176
|
+
X_r.colnames = StrVector(self.column_names)
|
|
177
|
+
else:
|
|
178
|
+
X_r = r.matrix(
|
|
179
|
+
FloatVector(X.ravel()),
|
|
180
|
+
byrow=True,
|
|
181
|
+
ncol=X.shape[1],
|
|
182
|
+
nrow=X.shape[0],
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
if self.pi_method == "none":
|
|
186
|
+
return np.asarray(self.obj["predict"](X_r)) - 1
|
|
187
|
+
|
|
188
|
+
return r_list_to_namedtuple(self.obj["predict"](X_r))
|
|
@@ -0,0 +1,157 @@
|
|
|
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 = (
|
|
49
|
+
"suppressWarnings(suppressMessages(library(learningmachine))); "
|
|
50
|
+
+ "Regressor$new(method = "
|
|
51
|
+
+ str(format_value(self.method))
|
|
52
|
+
+ ", "
|
|
53
|
+
+ "pi_method = "
|
|
54
|
+
+ str(format_value(self.pi_method))
|
|
55
|
+
+ ", "
|
|
56
|
+
+ "level = "
|
|
57
|
+
+ str(format_value(self.level))
|
|
58
|
+
+ ", "
|
|
59
|
+
+ "B = "
|
|
60
|
+
+ str(format_value(self.B))
|
|
61
|
+
+ ", "
|
|
62
|
+
+ "nb_hidden = "
|
|
63
|
+
+ str(format_value(self.nb_hidden))
|
|
64
|
+
+ ", "
|
|
65
|
+
+ "nodes_sim = "
|
|
66
|
+
+ str(format_value(self.nodes_sim))
|
|
67
|
+
+ ", "
|
|
68
|
+
+ "activ = "
|
|
69
|
+
+ str(format_value(self.activ))
|
|
70
|
+
+ ", "
|
|
71
|
+
+ "seed = "
|
|
72
|
+
+ str(format_value(self.seed))
|
|
73
|
+
+ ")"
|
|
74
|
+
)
|
|
75
|
+
self.obj = r(r_obj_command)
|
|
76
|
+
except Exception:
|
|
77
|
+
try:
|
|
78
|
+
self.obj = r(
|
|
79
|
+
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)})"
|
|
80
|
+
)
|
|
81
|
+
except Exception:
|
|
82
|
+
self.obj = r(
|
|
83
|
+
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)})"
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
def fit(self, X, y, **kwargs):
|
|
87
|
+
"""
|
|
88
|
+
Fit the model according to the given training data.
|
|
89
|
+
"""
|
|
90
|
+
params_dict = {}
|
|
91
|
+
|
|
92
|
+
for k, v in kwargs.items():
|
|
93
|
+
if k == "lambda_":
|
|
94
|
+
params_dict["lambda"] = v
|
|
95
|
+
elif "__" in k:
|
|
96
|
+
params_dict[k.replace("__", ".")] = v
|
|
97
|
+
else:
|
|
98
|
+
params_dict[k] = v
|
|
99
|
+
|
|
100
|
+
if isinstance(X, pd.DataFrame):
|
|
101
|
+
self.column_names = X.columns
|
|
102
|
+
X_r = r.matrix(
|
|
103
|
+
FloatVector(X.values.ravel()),
|
|
104
|
+
byrow=True,
|
|
105
|
+
ncol=X.shape[1],
|
|
106
|
+
nrow=X.shape[0],
|
|
107
|
+
)
|
|
108
|
+
X_r.colnames = StrVector(self.column_names)
|
|
109
|
+
else:
|
|
110
|
+
X_r = r.matrix(
|
|
111
|
+
FloatVector(X.ravel()),
|
|
112
|
+
byrow=True,
|
|
113
|
+
ncol=X.shape[1],
|
|
114
|
+
nrow=X.shape[0],
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
if isinstance(y, pd.DataFrame) or isinstance(y, pd.Series):
|
|
118
|
+
y = y.values.ravel()
|
|
119
|
+
|
|
120
|
+
self.obj["fit"](X_r, FloatVector(y), **params_dict)
|
|
121
|
+
return self
|
|
122
|
+
|
|
123
|
+
def predict(self, X):
|
|
124
|
+
"""
|
|
125
|
+
Predict using the model.
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
if isinstance(X, pd.DataFrame):
|
|
129
|
+
X_r = r.matrix(
|
|
130
|
+
FloatVector(X.values.ravel()),
|
|
131
|
+
byrow=True,
|
|
132
|
+
ncol=X.shape[1],
|
|
133
|
+
nrow=X.shape[0],
|
|
134
|
+
)
|
|
135
|
+
X_r.colnames = StrVector(self.column_names)
|
|
136
|
+
else:
|
|
137
|
+
X_r = r.matrix(
|
|
138
|
+
FloatVector(X.ravel()),
|
|
139
|
+
byrow=True,
|
|
140
|
+
ncol=X.shape[1],
|
|
141
|
+
nrow=X.shape[0],
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
if self.pi_method == "none":
|
|
145
|
+
return np.asarray(self.obj["predict"](X_r))
|
|
146
|
+
return r_list_to_namedtuple(self.obj["predict"](X_r))
|
|
147
|
+
|
|
148
|
+
def update(self, newx, newy):
|
|
149
|
+
"""
|
|
150
|
+
update the model.
|
|
151
|
+
"""
|
|
152
|
+
|
|
153
|
+
newx_r = base.as_vector(FloatVector(newx))
|
|
154
|
+
|
|
155
|
+
self.obj["update"](newx_r, base.as_numeric(FloatVector([newy])))
|
|
156
|
+
|
|
157
|
+
return self
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import numpy as np
|
|
1
|
+
import numpy as np
|
|
2
2
|
import subprocess
|
|
3
3
|
from rpy2.robjects import r
|
|
4
4
|
from rpy2.robjects import NULL as rNULL
|
|
@@ -75,21 +75,30 @@ def format_value(value):
|
|
|
75
75
|
return f"{str(value).upper()}"
|
|
76
76
|
if isinstance(value, int) or isinstance(value, float):
|
|
77
77
|
return f"{value}"
|
|
78
|
-
|
|
78
|
+
|
|
79
|
+
|
|
79
80
|
# R list to namedtuple
|
|
80
81
|
def r_list_to_namedtuple(r_list):
|
|
81
|
-
# Extract the names from the R list
|
|
82
|
+
# Extract the names from the R list
|
|
82
83
|
if r_list.names is rNULL:
|
|
83
|
-
|
|
84
|
+
# names = [f'obs{i+1}' for i in range(len(r_list))]
|
|
84
85
|
# Define a namedtuple type based on the names in the R list
|
|
85
|
-
|
|
86
|
+
# DescribeResult = namedtuple('DescribeResult', names)
|
|
86
87
|
# Extract elements from the R list and create a namedtuple
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return tuple(
|
|
88
|
+
# elements = {name: r_list.rx2(i+1) for i, name in enumerate(names)}
|
|
89
|
+
# return DescribeResult(**elements)
|
|
90
|
+
return tuple(
|
|
91
|
+
[
|
|
92
|
+
[
|
|
93
|
+
int(r_list.rx2(i + 1)[j])
|
|
94
|
+
for j in range(len(r_list.rx2(i + 1)))
|
|
95
|
+
]
|
|
96
|
+
for i in range(len(r_list))
|
|
97
|
+
]
|
|
98
|
+
)
|
|
90
99
|
names = r_list.names
|
|
91
100
|
# Define a namedtuple type based on the names in the R list
|
|
92
|
-
DescribeResult = namedtuple(
|
|
101
|
+
DescribeResult = namedtuple("DescribeResult", names)
|
|
93
102
|
# Extract elements from the R list and create a namedtuple
|
|
94
|
-
elements = {name: np.asarray(r_list.rx2(name)) for name in names}
|
|
95
|
-
return DescribeResult(**elements)
|
|
103
|
+
elements = {name: np.asarray(r_list.rx2(name)) for name in names}
|
|
104
|
+
return DescribeResult(**elements)
|
|
@@ -1,161 +0,0 @@
|
|
|
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
|
-
|
|
@@ -1,137 +0,0 @@
|
|
|
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
|
-
|
|
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.2.2 → learningmachine-2.3.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
|