Skip to content

Commit 8ce9077

Browse files
jnothmanogrisel
authored andcommitted
ENH do not allocate memory for temporary array of 1s
1 parent bbc3e7e commit 8ce9077

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

sklearn/preprocessing/label.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import array
1111

1212
import numpy as np
13+
from numpy.lib.stride_tricks import as_strided
1314
import scipy.sparse as sp
1415

1516
from ..base import BaseEstimator, TransformerMixin
@@ -552,7 +553,9 @@ def _transform(self, y, class_mapping):
552553
for labels in y:
553554
indices.extend(set(class_mapping[label] for label in labels))
554555
indptr.append(len(indices))
555-
data = np.ones(len(indices), dtype=int)
556+
# virtual array of len(indices) 1s:
557+
data = as_strided(np.array([1], dtype=int), strides=(0,),
558+
shape=(len(indices),))
556559
return sp.csr_matrix((data, indices, indptr),
557560
shape=(len(indptr) - 1, len(class_mapping)))
558561

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy