コード例 #1
0
def estimateGrad(fun, x, delta):
    """ Given a real-valued function fun, estimate its gradient numerically.
     """
    grad = SparseVector({})
    for key in x:
        e = SparseVector({})
        e[key] = 1.0
        grad[key] = (fun(x + delta * e) - fun(x)) / delta
    return grad
コード例 #2
0
 def test_gradTotalLoss(self):
     rdd = self.sc.parallelize([
     (SparseVector.SparseVector({'a': -1, 'b': -1}), -1),
     (SparseVector.SparseVector({'a': -1, 'b': -1}), 1),
     (SparseVector.SparseVector({'a':1, 'b': 1}), 1),
     (SparseVector.SparseVector({'a':1, 'b': 1}), 1),
     (SparseVector.SparseVector({'a':1, 'b': 1}), 1),
     (SparseVector.SparseVector({'a':1, 'b': 1}), 1)
     ])
     beta = SparseVector.SparseVector({'a':2, 'b':3})
     kek = ParallelLogisticRegression.gradTotalLossRDD(rdd, beta)
コード例 #3
0
 def test_test(self):
     data = [
     (SparseVector.SparseVector({'a': -1, 'b': -1}), -1),
     (SparseVector.SparseVector({'a': -1, 'b': -1}), 1),
     (SparseVector.SparseVector({'a':1, 'b': 1}), 1),
     (SparseVector.SparseVector({'a':1, 'b': 1}), 1),
     (SparseVector.SparseVector({'a':1, 'b': 1}), 1),
     (SparseVector.SparseVector({'a':1, 'b': 1}), 1)
     ]
     beta = SparseVector.SparseVector({'a': 2, 'b': 2})
     scores = LogisticRegression.test(data, beta)
コード例 #4
0
    def test_test(self):
        rdd = self.sc.parallelize([
        (SparseVector.SparseVector({'a': -1, 'b': -1}), -1),
        (SparseVector.SparseVector({'a': -1, 'b': -1}), 1),
        (SparseVector.SparseVector({'a':1, 'b': 1}), 1),
        (SparseVector.SparseVector({'a':1, 'b': 1}), 1),
        (SparseVector.SparseVector({'a':1, 'b': 1}), 1),
        (SparseVector.SparseVector({'a':1, 'b': 1}), 1)
        ])
        beta = SparseVector.SparseVector({'a':2, 'b':3})

        kek = ParallelLogisticRegression.test(rdd, beta)
        print kek
コード例 #5
0
 def setUp(self):
     self.x = SparseVector.SparseVector(dict(a=1,b=1))
     self.y = 1
     self.beta = SparseVector.SparseVector(dict(a=3,b=3))
コード例 #6
0
 def test_p_norm(self):
     sparse = SparseVector.SparseVector({'a': 2, 'b': 2})
コード例 #7
0
    def add(self, word, dimension, freq=1):

        if not self.has_key(word):
            self[word] = SparseVector.SparseVector()

        self[word].add(dimension, freq)