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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
|
# cython: cdivision=True
# cython: boundscheck=False
# cython: wraparound=False
#------------------------------------------------------------------------------
"""
Dataset abstractions for sequential data access.
WARNING: Do not edit .pyx file directly, it is generated from .pyx.tp
"""
cimport cython
from libc.limits cimport INT_MAX
cimport numpy as np
import numpy as np
np.import_array()
from ._random cimport our_rand_r
cdef class SequentialDataset64:
"""Base class for datasets with sequential data access.
SequentialDataset is used to iterate over the rows of a matrix X and
corresponding target values y, i.e. to iterate over samples.
There are two methods to get the next sample:
- next : Iterate sequentially (optionally randomized)
- random : Iterate randomly (with replacement)
Attributes
----------
index : np.ndarray
Index array for fast shuffling.
index_data_ptr : int
Pointer to the index array.
current_index : int
Index of current sample in ``index``.
The index of current sample in the data is given by
index_data_ptr[current_index].
n_samples : Py_ssize_t
Number of samples in the dataset.
seed : np.uint32_t
Seed used for random sampling.
"""
cdef void next(self, double **x_data_ptr, int **x_ind_ptr,
int *nnz, double *y, double *sample_weight) nogil:
"""Get the next example ``x`` from the dataset.
This method gets the next sample looping sequentially over all samples.
The order can be shuffled with the method ``shuffle``.
Shuffling once before iterating over all samples corresponds to a
random draw without replacement. It is used for instance in SGD solver.
Parameters
----------
x_data_ptr : double**
A pointer to the double array which holds the feature
values of the next example.
x_ind_ptr : np.intc**
A pointer to the int array which holds the feature
indices of the next example.
nnz : int*
A pointer to an int holding the number of non-zero
values of the next example.
y : double*
The target value of the next example.
sample_weight : double*
The weight of the next example.
"""
cdef int current_index = self._get_next_index()
self._sample(x_data_ptr, x_ind_ptr, nnz, y, sample_weight,
current_index)
cdef int random(self, double **x_data_ptr, int **x_ind_ptr,
int *nnz, double *y, double *sample_weight) nogil:
"""Get a random example ``x`` from the dataset.
This method gets next sample chosen randomly over a uniform
distribution. It corresponds to a random draw with replacement.
It is used for instance in SAG solver.
Parameters
----------
x_data_ptr : double**
A pointer to the double array which holds the feature
values of the next example.
x_ind_ptr : np.intc**
A pointer to the int array which holds the feature
indices of the next example.
nnz : int*
A pointer to an int holding the number of non-zero
values of the next example.
y : double*
The target value of the next example.
sample_weight : double*
The weight of the next example.
Returns
-------
current_index : int
Index of current sample.
"""
cdef int current_index = self._get_random_index()
self._sample(x_data_ptr, x_ind_ptr, nnz, y, sample_weight,
current_index)
return current_index
cdef void shuffle(self, np.uint32_t seed) nogil:
"""Permutes the ordering of examples."""
# Fisher-Yates shuffle
cdef int *ind = self.index_data_ptr
cdef int n = self.n_samples
cdef unsigned i, j
for i in range(n - 1):
j = i + our_rand_r(&seed) % (n - i)
ind[i], ind[j] = ind[j], ind[i]
cdef int _get_next_index(self) nogil:
cdef int current_index = self.current_index
if current_index >= (self.n_samples - 1):
current_index = -1
current_index += 1
self.current_index = current_index
return self.current_index
cdef int _get_random_index(self) nogil:
cdef int n = self.n_samples
cdef int current_index = our_rand_r(&self.seed) % n
self.current_index = current_index
return current_index
cdef void _sample(self, double **x_data_ptr, int **x_ind_ptr,
int *nnz, double *y, double *sample_weight,
int current_index) nogil:
pass
def _shuffle_py(self, np.uint32_t seed):
"""python function used for easy testing"""
self.shuffle(seed)
def _next_py(self):
"""python function used for easy testing"""
cdef int current_index = self._get_next_index()
return self._sample_py(current_index)
def _random_py(self):
"""python function used for easy testing"""
cdef int current_index = self._get_random_index()
return self._sample_py(current_index)
def _sample_py(self, int current_index):
"""python function used for easy testing"""
cdef double* x_data_ptr
cdef int* x_indices_ptr
cdef int nnz, j
cdef double y, sample_weight
# call _sample in cython
self._sample(&x_data_ptr, &x_indices_ptr, &nnz, &y, &sample_weight,
current_index)
# transform the pointed data in numpy CSR array
cdef np.ndarray[double, ndim=1] x_data = np.empty(nnz,
dtype=np.float64)
cdef np.ndarray[int, ndim=1] x_indices = np.empty(nnz, dtype=np.int32)
cdef np.ndarray[int, ndim=1] x_indptr = np.asarray([0, nnz],
dtype=np.int32)
for j in range(nnz):
x_data[j] = x_data_ptr[j]
x_indices[j] = x_indices_ptr[j]
cdef int sample_idx = self.index_data_ptr[current_index]
return (x_data, x_indices, x_indptr), y, sample_weight, sample_idx
cdef class ArrayDataset64(SequentialDataset64):
"""Dataset backed by a two-dimensional numpy array.
The dtype of the numpy array is expected to be ``np.float64`` (double)
and C-style memory layout.
"""
def __cinit__(self, np.ndarray[double, ndim=2, mode='c'] X,
np.ndarray[double, ndim=1, mode='c'] Y,
np.ndarray[double, ndim=1, mode='c'] sample_weights,
np.uint32_t seed=1):
"""A ``SequentialDataset`` backed by a two-dimensional numpy array.
Parameters
----------
X : ndarray, dtype=double, ndim=2, mode='c'
The sample array, of shape(n_samples, n_features)
Y : ndarray, dtype=double, ndim=1, mode='c'
The target array, of shape(n_samples, )
sample_weights : ndarray, dtype=double, ndim=1, mode='c'
The weight of each sample, of shape(n_samples,)
"""
if X.shape[0] > INT_MAX or X.shape[1] > INT_MAX:
raise ValueError("More than %d samples or features not supported;"
" got (%d, %d)."
% (INT_MAX, X.shape[0], X.shape[1]))
# keep a reference to the data to prevent garbage collection
self.X = X
self.Y = Y
self.sample_weights = sample_weights
self.n_samples = X.shape[0]
self.n_features = X.shape[1]
cdef np.ndarray[int, ndim=1, mode='c'] feature_indices = \
np.arange(0, self.n_features, dtype=np.intc)
self.feature_indices = feature_indices
self.feature_indices_ptr = <int *> feature_indices.data
self.current_index = -1
self.X_stride = X.strides[0] / X.itemsize
self.X_data_ptr = <double *>X.data
self.Y_data_ptr = <double *>Y.data
self.sample_weight_data = <double *>sample_weights.data
# Use index array for fast shuffling
cdef np.ndarray[int, ndim=1, mode='c'] index = \
np.arange(0, self.n_samples, dtype=np.intc)
self.index = index
self.index_data_ptr = <int *>index.data
# seed should not be 0 for our_rand_r
self.seed = max(seed, 1)
cdef void _sample(self, double **x_data_ptr, int **x_ind_ptr,
int *nnz, double *y, double *sample_weight,
int current_index) nogil:
cdef long long sample_idx = self.index_data_ptr[current_index]
cdef long long offset = sample_idx * self.X_stride
y[0] = self.Y_data_ptr[sample_idx]
x_data_ptr[0] = self.X_data_ptr + offset
x_ind_ptr[0] = self.feature_indices_ptr
nnz[0] = self.n_features
sample_weight[0] = self.sample_weight_data[sample_idx]
cdef class CSRDataset64(SequentialDataset64):
"""A ``SequentialDataset`` backed by a scipy sparse CSR matrix. """
def __cinit__(self, np.ndarray[double, ndim=1, mode='c'] X_data,
np.ndarray[int, ndim=1, mode='c'] X_indptr,
np.ndarray[int, ndim=1, mode='c'] X_indices,
np.ndarray[double, ndim=1, mode='c'] Y,
np.ndarray[double, ndim=1, mode='c'] sample_weights,
np.uint32_t seed=1):
"""Dataset backed by a scipy sparse CSR matrix.
The feature indices of ``x`` are given by x_ind_ptr[0:nnz].
The corresponding feature values are given by
x_data_ptr[0:nnz].
Parameters
----------
X_data : ndarray, dtype=double, ndim=1, mode='c'
The data array of the CSR features matrix.
X_indptr : ndarray, dtype=np.intc, ndim=1, mode='c'
The index pointer array of the CSR features matrix.
X_indices : ndarray, dtype=np.intc, ndim=1, mode='c'
The column indices array of the CSR features matrix.
Y : ndarray, dtype=double, ndim=1, mode='c'
The target values.
sample_weights : ndarray, dtype=double, ndim=1, mode='c'
The weight of each sample.
"""
# keep a reference to the data to prevent garbage collection
self.X_data = X_data
self.X_indptr = X_indptr
self.X_indices = X_indices
self.Y = Y
self.sample_weights = sample_weights
self.n_samples = Y.shape[0]
self.current_index = -1
self.X_data_ptr = <double *>X_data.data
self.X_indptr_ptr = <int *>X_indptr.data
self.X_indices_ptr = <int *>X_indices.data
self.Y_data_ptr = <double *>Y.data
self.sample_weight_data = <double *>sample_weights.data
# Use index array for fast shuffling
cdef np.ndarray[int, ndim=1, mode='c'] idx = np.arange(self.n_samples,
dtype=np.intc)
self.index = idx
self.index_data_ptr = <int *>idx.data
# seed should not be 0 for our_rand_r
self.seed = max(seed, 1)
cdef void _sample(self, double **x_data_ptr, int **x_ind_ptr,
int *nnz, double *y, double *sample_weight,
int current_index) nogil:
cdef long long sample_idx = self.index_data_ptr[current_index]
cdef long long offset = self.X_indptr_ptr[sample_idx]
y[0] = self.Y_data_ptr[sample_idx]
x_data_ptr[0] = self.X_data_ptr + offset
x_ind_ptr[0] = self.X_indices_ptr + offset
nnz[0] = self.X_indptr_ptr[sample_idx + 1] - offset
sample_weight[0] = self.sample_weight_data[sample_idx]
#------------------------------------------------------------------------------
"""
Dataset abstractions for sequential data access.
WARNING: Do not edit .pyx file directly, it is generated from .pyx.tp
"""
cimport cython
from libc.limits cimport INT_MAX
cimport numpy as np
import numpy as np
np.import_array()
from ._random cimport our_rand_r
cdef class SequentialDataset32:
"""Base class for datasets with sequential data access.
SequentialDataset is used to iterate over the rows of a matrix X and
corresponding target values y, i.e. to iterate over samples.
There are two methods to get the next sample:
- next : Iterate sequentially (optionally randomized)
- random : Iterate randomly (with replacement)
Attributes
----------
index : np.ndarray
Index array for fast shuffling.
index_data_ptr : int
Pointer to the index array.
current_index : int
Index of current sample in ``index``.
The index of current sample in the data is given by
index_data_ptr[current_index].
n_samples : Py_ssize_t
Number of samples in the dataset.
seed : np.uint32_t
Seed used for random sampling.
"""
cdef void next(self, float **x_data_ptr, int **x_ind_ptr,
int *nnz, float *y, float *sample_weight) nogil:
"""Get the next example ``x`` from the dataset.
This method gets the next sample looping sequentially over all samples.
The order can be shuffled with the method ``shuffle``.
Shuffling once before iterating over all samples corresponds to a
random draw without replacement. It is used for instance in SGD solver.
Parameters
----------
x_data_ptr : float**
A pointer to the float array which holds the feature
values of the next example.
x_ind_ptr : np.intc**
A pointer to the int array which holds the feature
indices of the next example.
nnz : int*
A pointer to an int holding the number of non-zero
values of the next example.
y : float*
The target value of the next example.
sample_weight : float*
The weight of the next example.
"""
cdef int current_index = self._get_next_index()
self._sample(x_data_ptr, x_ind_ptr, nnz, y, sample_weight,
current_index)
cdef int random(self, float **x_data_ptr, int **x_ind_ptr,
int *nnz, float *y, float *sample_weight) nogil:
"""Get a random example ``x`` from the dataset.
This method gets next sample chosen randomly over a uniform
distribution. It corresponds to a random draw with replacement.
It is used for instance in SAG solver.
Parameters
----------
x_data_ptr : float**
A pointer to the float array which holds the feature
values of the next example.
x_ind_ptr : np.intc**
A pointer to the int array which holds the feature
indices of the next example.
nnz : int*
A pointer to an int holding the number of non-zero
values of the next example.
y : float*
The target value of the next example.
sample_weight : float*
The weight of the next example.
Returns
-------
current_index : int
Index of current sample.
"""
cdef int current_index = self._get_random_index()
self._sample(x_data_ptr, x_ind_ptr, nnz, y, sample_weight,
current_index)
return current_index
cdef void shuffle(self, np.uint32_t seed) nogil:
"""Permutes the ordering of examples."""
# Fisher-Yates shuffle
cdef int *ind = self.index_data_ptr
cdef int n = self.n_samples
cdef unsigned i, j
for i in range(n - 1):
j = i + our_rand_r(&seed) % (n - i)
ind[i], ind[j] = ind[j], ind[i]
cdef int _get_next_index(self) nogil:
cdef int current_index = self.current_index
if current_index >= (self.n_samples - 1):
current_index = -1
current_index += 1
self.current_index = current_index
return self.current_index
cdef int _get_random_index(self) nogil:
cdef int n = self.n_samples
cdef int current_index = our_rand_r(&self.seed) % n
self.current_index = current_index
return current_index
cdef void _sample(self, float **x_data_ptr, int **x_ind_ptr,
int *nnz, float *y, float *sample_weight,
int current_index) nogil:
pass
def _shuffle_py(self, np.uint32_t seed):
"""python function used for easy testing"""
self.shuffle(seed)
def _next_py(self):
"""python function used for easy testing"""
cdef int current_index = self._get_next_index()
return self._sample_py(current_index)
def _random_py(self):
"""python function used for easy testing"""
cdef int current_index = self._get_random_index()
return self._sample_py(current_index)
def _sample_py(self, int current_index):
"""python function used for easy testing"""
cdef float* x_data_ptr
cdef int* x_indices_ptr
cdef int nnz, j
cdef float y, sample_weight
# call _sample in cython
self._sample(&x_data_ptr, &x_indices_ptr, &nnz, &y, &sample_weight,
current_index)
# transform the pointed data in numpy CSR array
cdef np.ndarray[float, ndim=1] x_data = np.empty(nnz,
dtype=np.float32)
cdef np.ndarray[int, ndim=1] x_indices = np.empty(nnz, dtype=np.int32)
cdef np.ndarray[int, ndim=1] x_indptr = np.asarray([0, nnz],
dtype=np.int32)
for j in range(nnz):
x_data[j] = x_data_ptr[j]
x_indices[j] = x_indices_ptr[j]
cdef int sample_idx = self.index_data_ptr[current_index]
return (x_data, x_indices, x_indptr), y, sample_weight, sample_idx
cdef class ArrayDataset32(SequentialDataset32):
"""Dataset backed by a two-dimensional numpy array.
The dtype of the numpy array is expected to be ``np.float32`` (float)
and C-style memory layout.
"""
def __cinit__(self, np.ndarray[float, ndim=2, mode='c'] X,
np.ndarray[float, ndim=1, mode='c'] Y,
np.ndarray[float, ndim=1, mode='c'] sample_weights,
np.uint32_t seed=1):
"""A ``SequentialDataset`` backed by a two-dimensional numpy array.
Parameters
----------
X : ndarray, dtype=float, ndim=2, mode='c'
The sample array, of shape(n_samples, n_features)
Y : ndarray, dtype=float, ndim=1, mode='c'
The target array, of shape(n_samples, )
sample_weights : ndarray, dtype=float, ndim=1, mode='c'
The weight of each sample, of shape(n_samples,)
"""
if X.shape[0] > INT_MAX or X.shape[1] > INT_MAX:
raise ValueError("More than %d samples or features not supported;"
" got (%d, %d)."
% (INT_MAX, X.shape[0], X.shape[1]))
# keep a reference to the data to prevent garbage collection
self.X = X
self.Y = Y
self.sample_weights = sample_weights
self.n_samples = X.shape[0]
self.n_features = X.shape[1]
cdef np.ndarray[int, ndim=1, mode='c'] feature_indices = \
np.arange(0, self.n_features, dtype=np.intc)
self.feature_indices = feature_indices
self.feature_indices_ptr = <int *> feature_indices.data
self.current_index = -1
self.X_stride = X.strides[0] / X.itemsize
self.X_data_ptr = <float *>X.data
self.Y_data_ptr = <float *>Y.data
self.sample_weight_data = <float *>sample_weights.data
# Use index array for fast shuffling
cdef np.ndarray[int, ndim=1, mode='c'] index = \
np.arange(0, self.n_samples, dtype=np.intc)
self.index = index
self.index_data_ptr = <int *>index.data
# seed should not be 0 for our_rand_r
self.seed = max(seed, 1)
cdef void _sample(self, float **x_data_ptr, int **x_ind_ptr,
int *nnz, float *y, float *sample_weight,
int current_index) nogil:
cdef long long sample_idx = self.index_data_ptr[current_index]
cdef long long offset = sample_idx * self.X_stride
y[0] = self.Y_data_ptr[sample_idx]
x_data_ptr[0] = self.X_data_ptr + offset
x_ind_ptr[0] = self.feature_indices_ptr
nnz[0] = self.n_features
sample_weight[0] = self.sample_weight_data[sample_idx]
cdef class CSRDataset32(SequentialDataset32):
"""A ``SequentialDataset`` backed by a scipy sparse CSR matrix. """
def __cinit__(self, np.ndarray[float, ndim=1, mode='c'] X_data,
np.ndarray[int, ndim=1, mode='c'] X_indptr,
np.ndarray[int, ndim=1, mode='c'] X_indices,
np.ndarray[float, ndim=1, mode='c'] Y,
np.ndarray[float, ndim=1, mode='c'] sample_weights,
np.uint32_t seed=1):
"""Dataset backed by a scipy sparse CSR matrix.
The feature indices of ``x`` are given by x_ind_ptr[0:nnz].
The corresponding feature values are given by
x_data_ptr[0:nnz].
Parameters
----------
X_data : ndarray, dtype=float, ndim=1, mode='c'
The data array of the CSR features matrix.
X_indptr : ndarray, dtype=np.intc, ndim=1, mode='c'
The index pointer array of the CSR features matrix.
X_indices : ndarray, dtype=np.intc, ndim=1, mode='c'
The column indices array of the CSR features matrix.
Y : ndarray, dtype=float, ndim=1, mode='c'
The target values.
sample_weights : ndarray, dtype=float, ndim=1, mode='c'
The weight of each sample.
"""
# keep a reference to the data to prevent garbage collection
self.X_data = X_data
self.X_indptr = X_indptr
self.X_indices = X_indices
self.Y = Y
self.sample_weights = sample_weights
self.n_samples = Y.shape[0]
self.current_index = -1
self.X_data_ptr = <float *>X_data.data
self.X_indptr_ptr = <int *>X_indptr.data
self.X_indices_ptr = <int *>X_indices.data
self.Y_data_ptr = <float *>Y.data
self.sample_weight_data = <float *>sample_weights.data
# Use index array for fast shuffling
cdef np.ndarray[int, ndim=1, mode='c'] idx = np.arange(self.n_samples,
dtype=np.intc)
self.index = idx
self.index_data_ptr = <int *>idx.data
# seed should not be 0 for our_rand_r
self.seed = max(seed, 1)
cdef void _sample(self, float **x_data_ptr, int **x_ind_ptr,
int *nnz, float *y, float *sample_weight,
int current_index) nogil:
cdef long long sample_idx = self.index_data_ptr[current_index]
cdef long long offset = self.X_indptr_ptr[sample_idx]
y[0] = self.Y_data_ptr[sample_idx]
x_data_ptr[0] = self.X_data_ptr + offset
x_ind_ptr[0] = self.X_indices_ptr + offset
nnz[0] = self.X_indptr_ptr[sample_idx + 1] - offset
sample_weight[0] = self.sample_weight_data[sample_idx]
|