Skip to content

Commit b29e2bc

Browse files
committed
Remove scipy dependencies
1 parent e32eb40 commit b29e2bc

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

python/pyspark/mllib/util.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@
1818
import sys
1919
import numpy as np
2020
import warnings
21-
try:
22-
import scipy.sparse
23-
_have_scipy = True
24-
except:
25-
# No SciPy in environment, but that's okay
26-
_have_scipy = False
2721

2822
if sys.version > '3':
2923
xrange = range
3024

3125
from pyspark.mllib.common import callMLlibFunc, inherit_doc
32-
from pyspark.mllib.linalg import Vector, Vectors, SparseVector, _convert_to_vector
26+
from pyspark.mllib.linalg import Vectors, SparseVector, _convert_to_vector
3327

3428

3529
class MLUtils(object):
@@ -183,11 +177,11 @@ def appendBias(data):
183177
"""
184178
vec = _convert_to_vector(data)
185179
if isinstance(vec, SparseVector):
186-
l = scipy.sparse.csc_matrix(np.append(vec.toArray(), 1.0))
187-
return _convert_to_vector(l.T)
188-
elif isinstance(vec, Vector):
189-
vec = vec.toArray()
190-
return _convert_to_vector(np.append(vec, 1.0).tolist())
180+
entries = dict(zip(vec.indices, vec.values))
181+
entries[len(vec)] = 1.0
182+
return SparseVector(len(vec) + 1, entries)
183+
else:
184+
return _convert_to_vector(np.append(vec.toArray(), 1.0))
191185

192186
@staticmethod
193187
def loadVectors(sc, path):

0 commit comments

Comments
 (0)