Skip to content

Commit 301061f

Browse files
committed
ENH do not allocate memory for temporary array of 1s
1 parent 919f4ba commit 301061f

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
@@ -553,7 +554,9 @@ def _transform(self, y, class_mapping):
553554
for labels in y:
554555
indices.extend(set(class_mapping[label] for label in labels))
555556
indptr.append(len(indices))
556-
data = np.ones(len(indices), dtype=int)
557+
# virtual array of len(indices) 1s:
558+
data = as_strided(np.array([1], dtype=int), strides=(0,),
559+
shape=(len(indices),))
557560
return sp.csr_matrix((data, indices, indptr),
558561
shape=(len(indptr) - 1, len(class_mapping)))
559562

0 commit comments

Comments
 (0)