Source file pyinference.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
module Numpy = struct
let transpose x =
let npy_transpose = Py.Module.get_function (Pyinit.numpy ()) "transpose" in
npy_transpose [|x|]
end
module LinearModel = struct
let assert_matrix_nontrivial (m : Scikit_matrix.t) =
let l, c = Scikit_matrix.shape m in
assert (l <> 0 && c <> 0)
let ridge ~(alpha : float) ?(fit_intercept : bool = false)
~(input : Scikit_matrix.t) ~(output : Scikit_matrix.t) () =
assert_matrix_nontrivial input ;
assert_matrix_nontrivial output ;
let input = Scikit_matrix.to_numpy input in
let output = Scikit_matrix.to_numpy output in
let ridge_object =
Py.Module.get_function_with_keywords
(Pyinit.linear_model ())
"Ridge"
[||]
[
("alpha", Py.Float.of_float alpha);
("fit_intercept", Py.Bool.of_bool fit_intercept);
]
in
let _ =
match Py.Object.get_attr_string ridge_object "fit" with
| None -> Stdlib.failwith "Scikit.LinearModel.ridge: method fit not found"
| Some meth -> Py.Callable.to_function meth [|input; output|]
in
match Py.Object.get_attr_string ridge_object "coef_" with
| None ->
Stdlib.failwith "Scikit.LinearModel.ridge: attribute coef_ not found"
| Some coef -> Scikit_matrix.of_numpy (Numpy.transpose coef)
let lasso ~(alpha : float) ?(fit_intercept : bool = false)
?(positive : bool = false) ~(input : Scikit_matrix.t)
~(output : Scikit_matrix.t) () =
assert_matrix_nontrivial input ;
assert_matrix_nontrivial output ;
let input = Scikit_matrix.to_numpy input in
let output = Scikit_matrix.to_numpy output in
let lasso_object =
Py.Module.get_function_with_keywords
(Pyinit.linear_model ())
"Lasso"
[||]
[
("alpha", Py.Float.of_float alpha);
("fit_intercept", Py.Bool.of_bool fit_intercept);
("positive", Py.Bool.of_bool positive);
]
in
let _ =
match Py.Object.get_attr_string lasso_object "fit" with
| None -> Stdlib.failwith "Scikit.LinearModel.lasso: method fit not found"
| Some meth -> Py.Callable.to_function meth [|input; output|]
in
match Py.Object.get_attr_string lasso_object "coef_" with
| None ->
Stdlib.failwith "Scikit.LinearModel.lasso: attribute coef_ not found"
| Some coef -> Scikit_matrix.of_numpy coef
let nnls ~(input : Scikit_matrix.t) ~(output : Scikit_matrix.t) =
assert_matrix_nontrivial input ;
assert_matrix_nontrivial output ;
let len = Scikit_matrix.dim1 output in
let input = Scikit_matrix.to_numpy input in
let output = Scikit_matrix.to_numpy output in
let output =
Py.Module.get_function
(Pyinit.numpy ())
"reshape"
[|output; Py.Int.of_int len|]
in
let nnls_outcome =
Py.Module.get_function (Pyinit.scipy_optimize ()) "nnls" [|input; output|]
in
let array = Py.Tuple.to_array nnls_outcome in
if Array.length array <> 2 then
Stdlib.failwith "Scikit.nnls: invalid outcome"
else
let res = array.(0) in
Scikit_matrix.of_numpy res
end
let predict_output ~(input : Scikit_matrix.t) ~(weights : Scikit_matrix.t) =
let weights = Scikit_matrix.to_numpy weights in
let input = Scikit_matrix.to_numpy input in
Py.Module.get_function (Pyinit.numpy ()) "matmul" [|input; weights|]
let r2_score ~output ~prediction =
let len = Scikit_matrix.dim1 output in
let output = Scikit_matrix.to_numpy output in
if len <= 1 then
None
else
Py.Module.get_function
(Pyinit.sklearn_metrics ())
"r2_score"
[|output; prediction|]
|> Py.Float.to_float |> Option.some
let rmse_score ~output ~prediction =
let output = Scikit_matrix.to_numpy output in
Py.Module.get_function_with_keywords
(Pyinit.sklearn_metrics ())
"mean_squared_error"
[|output; prediction|]
[("squared", Py.Bool.f)]
|> Py.Float.to_float
let benchmark_score ~input ~output =
let input = Scikit_matrix.to_numpy input in
let output = Scikit_matrix.to_numpy_vector output in
let model =
Py.Module.get_function (Pyinit.statsmodels_api ()) "OLS" [|output; input|]
in
let result =
Py.Module.get_function model "fit" [||]
in
let tvalues =
Py.Object.find_attr_string result "tvalues" |> Scikit_matrix.of_numpy
in
let params =
Py.Object.find_attr_string result "params" |> Scikit_matrix.of_numpy
in
(params, tvalues)