File tree Expand file tree Collapse file tree 2 files changed +16
-6
lines changed Expand file tree Collapse file tree 2 files changed +16
-6
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1717
1818import sys
1919import numpy as np
20- import scipy .sparse as sp
2120import 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
2328if 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 ()
You can’t perform that action at this time.
0 commit comments