File tree Expand file tree Collapse file tree 1 file changed +6
-12
lines changed Expand file tree Collapse file tree 1 file changed +6
-12
lines changed Original file line number Diff line number Diff line change 1818import sys
1919import numpy as np
2020import 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
2822if sys .version > '3' :
2923 xrange = range
3024
3125from 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
3529class 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 ):
You can’t perform that action at this time.
0 commit comments