/
regression.pyx
232 lines (200 loc) · 8.16 KB
/
regression.pyx
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#
# Copyright (c) 2019-2023, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# distutils: language = c++
import typing
from cuml.internals.safe_imports import (
cpu_only_import,
gpu_only_import_from,
null_decorator
)
nvtx_annotate = gpu_only_import_from("nvtx", "annotate", alt=null_decorator)
np = cpu_only_import('numpy')
import cuml.internals
from cuml.internals.array import CumlArray
from pylibraft.common.handle cimport handle_t
from pylibraft.common.handle import Handle
from libcpp cimport bool
from libc.stdint cimport uint64_t, uintptr_t
from random import randint
cdef extern from "cuml/datasets/make_regression.hpp" namespace "ML":
void cpp_make_regression "ML::Datasets::make_regression" (
const handle_t& handle,
float* out,
float* values,
long n_rows,
long n_cols,
long n_informative,
float* coef,
long n_targets,
float bias,
long effective_rank,
float tail_strength,
float noise,
bool shuffle,
uint64_t seed)
void cpp_make_regression "ML::Datasets::make_regression" (
const handle_t& handle,
double* out,
double* values,
long n_rows,
long n_cols,
long n_informative,
double* coef,
long n_targets,
double bias,
long effective_rank,
double tail_strength,
double noise,
bool shuffle,
uint64_t seed)
inp_to_dtype = {
'single': np.float32,
'float': np.float32,
'double': np.float64,
np.float32: np.float32,
np.float64: np.float64
}
@nvtx_annotate(message="datasets.make_regression", domain="cuml_python")
@cuml.internals.api_return_generic()
def make_regression(
n_samples=100,
n_features=2,
n_informative=2,
n_targets=1,
bias=0.0,
effective_rank=None,
tail_strength=0.5,
noise=0.0,
shuffle=True,
coef=False,
random_state=None,
dtype='single',
handle=None
) -> typing.Union[typing.Tuple[CumlArray, CumlArray],
typing.Tuple[CumlArray, CumlArray, CumlArray]]:
"""Generate a random regression problem.
See https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_regression.html
Examples
--------
.. code-block:: python
>>> from cuml.datasets.regression import make_regression
>>> from cuml.linear_model import LinearRegression
>>> # Create regression problem
>>> data, values = make_regression(n_samples=200, n_features=12,
... n_informative=7, bias=-4.2,
... noise=0.3, random_state=10)
>>> # Perform a linear regression on this problem
>>> lr = LinearRegression(fit_intercept = True, normalize = False,
... algorithm = "eig")
>>> reg = lr.fit(data, values)
>>> print(reg.coef_) # doctest: +SKIP
[-2.6980877e-02 7.7027252e+01 1.1498465e+01 8.5468025e+00
5.8548538e+01 6.0772545e+01 3.6876743e+01 4.0023815e+01
4.3908358e-03 -2.0275116e-02 3.5066366e-02 -3.4512520e-02]
Parameters
----------
n_samples : int, optional (default=100)
The number of samples.
n_features : int, optional (default=2)
The number of features.
n_informative : int, optional (default=2)
The number of informative features, i.e., the number of features used
to build the linear model used to generate the output.
n_targets : int, optional (default=1)
The number of regression targets, i.e., the dimension of the y output
vector associated with a sample. By default, the output is a scalar.
bias : float, optional (default=0.0)
The bias term in the underlying linear model.
effective_rank : int or None, optional (default=None)
if not None:
The approximate number of singular vectors required to explain most
of the input data by linear combinations. Using this kind of
singular spectrum in the input allows the generator to reproduce
the correlations often observed in practice.
if None:
The input set is well conditioned, centered and gaussian with
unit variance.
tail_strength : float between 0.0 and 1.0, optional (default=0.5)
The relative importance of the fat noisy tail of the singular values
profile if `effective_rank` is not None.
noise : float, optional (default=0.0)
The standard deviation of the gaussian noise applied to the output.
shuffle : boolean, optional (default=True)
Shuffle the samples and the features.
coef : boolean, optional (default=False)
If True, the coefficients of the underlying linear model are returned.
random_state : int, RandomState instance or None (default)
Seed for the random number generator for dataset creation.
dtype: string or numpy dtype (default: 'single')
Type of the data. Possible values: float32, float64, 'single', 'float'
or 'double'.
handle: cuml.Handle
If it is None, a new one is created just for this function call
Returns
-------
out : device array of shape [n_samples, n_features]
The input samples.
values : device array of shape [n_samples, n_targets]
The output values.
coef : device array of shape [n_features, n_targets], optional
The coefficient of the underlying linear model. It is returned only if
coef is True.
""" # noqa: E501
# Set the default output type to "cupy". This will be ignored if the user
# has set `cuml.global_settings.output_type`. Only necessary for array
# generation methods that do not take an array as input
cuml.internals.set_api_output_type("cupy")
if dtype not in ['single', 'float', 'double', np.float32, np.float64]:
raise TypeError("dtype must be either 'float' or 'double'")
else:
dtype = inp_to_dtype[dtype]
if effective_rank is None:
effective_rank = -1
handle = Handle() if handle is None else handle
cdef handle_t* handle_ = <handle_t*><size_t>handle.getHandle()
out = CumlArray.zeros((n_samples, n_features), dtype=dtype, order='C')
cdef uintptr_t out_ptr = out.ptr
values = CumlArray.zeros((n_samples, n_targets), dtype=dtype, order='C')
cdef uintptr_t values_ptr = values.ptr
cdef uintptr_t coef_ptr
coef_ptr = <uintptr_t> NULL
if coef:
coefs = CumlArray.zeros((n_features, n_targets),
dtype=dtype,
order='C')
coef_ptr = coefs.ptr
if random_state is None:
random_state = randint(0, 1e18)
if dtype == np.float32:
cpp_make_regression(handle_[0], <float*> out_ptr,
<float*> values_ptr, <long> n_samples,
<long> n_features, <long> n_informative,
<float*> coef_ptr, <long> n_targets, <float> bias,
<long> effective_rank, <float> tail_strength,
<float> noise, <bool> shuffle,
<uint64_t> random_state)
else:
cpp_make_regression(handle_[0], <double*> out_ptr,
<double*> values_ptr, <long> n_samples,
<long> n_features, <long> n_informative,
<double*> coef_ptr, <long> n_targets,
<double> bias, <long> effective_rank,
<double> tail_strength, <double> noise,
<bool> shuffle, <uint64_t> random_state)
if coef:
return out, values, coefs
else:
return out, values