def test_serialize(self): sv = SparseVector(4, {1: 1, 3: 2}) dv = array([1., 2., 3., 4.]) lst = [1, 2, 3, 4] self.assertTrue(sv is _convert_vector(sv)) self.assertTrue(dv is _convert_vector(dv)) self.assertTrue(array_equal(dv, _convert_vector(lst))) self.assertEquals(sv, _deserialize_double_vector(_serialize_double_vector(sv))) self.assertTrue(array_equal(dv, _deserialize_double_vector(_serialize_double_vector(dv)))) self.assertTrue(array_equal(dv, _deserialize_double_vector(_serialize_double_vector(lst))))
def test_serialize(self): from scipy.sparse import lil_matrix lil = lil_matrix((4, 1)) lil[1, 0] = 1 lil[3, 0] = 2 sv = SparseVector(4, {1: 1, 3: 2}) self.assertEquals(sv, _convert_vector(lil)) self.assertEquals(sv, _convert_vector(lil.tocsc())) self.assertEquals(sv, _convert_vector(lil.tocoo())) self.assertEquals(sv, _convert_vector(lil.tocsr())) self.assertEquals(sv, _convert_vector(lil.todok())) self.assertEquals(sv, _deserialize_double_vector(_serialize_double_vector(lil))) self.assertEquals(sv, _deserialize_double_vector(_serialize_double_vector(lil.tocsc()))) self.assertEquals(sv, _deserialize_double_vector(_serialize_double_vector(lil.tocsr()))) self.assertEquals(sv, _deserialize_double_vector(_serialize_double_vector(lil.todok())))
def _convert_labeled_point_to_libsvm(p): """Converts a LabeledPoint to a string in LIBSVM format.""" items = [str(p.label)] v = _convert_vector(p.features) if type(v) == np.ndarray: for i in xrange(len(v)): items.append(str(i + 1) + ":" + str(v[i])) elif type(v) == SparseVector: nnz = len(v.indices) for i in xrange(nnz): items.append(str(v.indices[i] + 1) + ":" + str(v.values[i])) else: raise TypeError("_convert_labeled_point_to_libsvm needs either ndarray or SparseVector" " but got " % type(v)) return " ".join(items)