Skip to content

Commit d6bd416

Browse files
committed
Check existence of scipy.sparse
1 parent 5d555b1 commit d6bd416

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

python/pyspark/mllib/tests.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -835,11 +835,11 @@ def test_append_bias_with_vector(self):
835835

836836
def test_append_bias_with_sp_vector(self):
837837
data = Vectors.sparse(3, {0: 2.0, 2: 2.0})
838-
# Returned value must be scipy.sparse matrix
838+
expected = Vectors.sparse(4, {0: 2.0, 2: 2.0, 3: 1.0})
839+
# Returned value must be SparseVector
839840
ret = MLUtils.appendBias(data)
840-
self.assertEqual(ret.shape, (1, 4))
841-
self.assertEqual(ret.toarray()[0][3], 1.0)
842-
self.assertEqual(type(ret), sp.csc_matrix)
841+
self.assertEqual(ret, expected)
842+
self.assertEqual(type(ret), SparseVector)
843843

844844
def test_load_vectors(self):
845845
import shutil

python/pyspark/mllib/util.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717

1818
import sys
1919
import numpy as np
20-
import scipy.sparse as sp
2120
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
2227

2328
if sys.version > '3':
2429
xrange = range
@@ -178,7 +183,12 @@ def appendBias(data):
178183
"""
179184
vec = _convert_to_vector(data)
180185
if isinstance(vec, SparseVector):
181-
return sp.csc_matrix(np.append(vec.toArray(), 1.0))
186+
if _have_scipy:
187+
l = scipy.sparse.csc_matrix(np.append(vec.toArray(), 1.0))
188+
return _convert_to_vector(l.T)
189+
else:
190+
raise TypeError("Cannot append bias %s into sparce "
191+
"vector because of lack of scipy" % type(vec))
182192
elif isinstance(vec, Vector):
183193
vec = vec.toArray()
184194
return np.append(vec, 1.0).tolist()

0 commit comments

Comments
 (0)