8000 PERF Don't allocate space for bias element if there isn't one (#14108) · scikit-learn/scikit-learn@7ce8b21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ce8b21

Browse files
alexhenrierth
authored andcommitted
PERF Don't allocate space for bias element if there isn't one (#14108)
1 parent 1015caf commit 7ce8b21

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

doc/whats_new/v0.22.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ Changelog
6363
of the maximization procedure in :term:`fit`.
6464
:pr:`13618` by :user:`Yoshihiro Uchida <c56pony>`.
6565

66+
- |Efficiency| The 'liblinear' logistic regression solver now consumes less
67+
memory if ``fit_intercept=False`` and ``X`` is a CSR sparse matrix.
68+
:pr:`14108` by :user:`Alex Henrie <alexhenrie>`.
69+
6670
:mod:`sklearn.model_selection`
6771
..................
6872

sklearn/svm/src/liblinear/liblinear_helper.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static struct feature_node **csr_to_sparse(double *values,
7676
{
7777
struct feature_node **sparse, *temp;
7878
int i, j=0, k=0, n;
79+
int have_bias = (bias > 0);
7980

8081
sparse = malloc ((shape_indptr[0]-1)* sizeof(struct feature_node *));
8182
if (sparse == NULL)
@@ -84,7 +85,7 @@ static struct feature_node **csr_to_sparse(double *values,
8485
for (i=0; i<shape_indptr[0]-1; ++i) {
8586
n = indptr[i+1] - indptr[i]; /* count elements in row i */
8687

87-
sparse[i] = malloc ((n+2) * sizeof(struct feature_node));
88+
sparse[i] = malloc ((n+have_bias+1) * sizeof(struct feature_node));
8889
if (sparse[i] == NULL) {
8990
int l;
9091
for (l=0; l<i; l++)
@@ -99,7 +100,7 @@ static struct feature_node **csr_to_sparse(double *values,
99100
++k;
100101
}
101102

102-
if (bias > 0) {
103+
if (have_bias) {
103104
temp[j].value = bias;
104105
temp[j].index = n_features + 1;
105106
++j;

0 commit comments

Comments
 (0)
0