Exemplo n.º 1
0
def test_end_to_end_logistic_regression():
    pos, neg = logistic.generate_well_separable(100, 0.50)

    #graph_pos_neg(pos, neg)

    X = logistic.vstack([pos, neg])
    y = logistic.hstack([np.array([1] * len(pos)), 
                   np.array([0] * len(neg)),])
    data = logistic.generate_random_points(100, 
                                           center=np.array([2,2]), 
                                           scale=np.array([5,5]))

    #theta = logistic.logistic_gradient_descent(X, y)

    thetaC = logistic.fast_logistic_gradient_descent(X, y)
    theta = thetaC
    #assert np.allclose(theta, thetaC)

    labels = logistic.label_data(data, theta, binarize=True)
    assert len([l for l in labels if l == 0]) > 10
    assert len([l for l in labels if l == 1]) > 10
    labels = logistic.label_data(data, thetaC, binarize=True)
    assert len([l for l in labels if l == 0]) > 10
    assert len([l for l in labels if l == 1]) > 10

    small_data = np.array([[-1, -1], [11, 11]])
    labels2 = logistic.label_data(small_data, theta, binarize=True)
    assert np.allclose([0, 1], labels2)
    assert not np.allclose([1, 1], labels2)
    labels2 = logistic.label_data(small_data, thetaC, binarize=True)
    assert np.allclose([0, 1], labels2)
    assert not np.allclose([1, 1], labels2)
        ax.scatter(neg[:,0], neg[:,1], s=6, c='r', marker='o', lw=0)

        delta = 0.01 * speed_multiple
        x, y = np.arange(-8, 8, delta), np.arange(-10, 10, delta)
        X, Y = np.meshgrid(x, y)

        assert X.shape == Y.shape
        shape = X.shape

        data = np.hstack([X.flatten().reshape(-1, 1), Y.flatten().reshape(-1,1)])
        assert data.shape[0] == (shape[0] * shape[1]) and data.shape[1] == 2
        data = add_x2_y2(data)
        scaled_data = scaler.transform(data)

        # plot the LR on the true labels
        labels = logistic.label_data(data, thetaTrue, normalizer=0.0, binarize=False)
        labels.shape = shape
        CS = pyplot.contour(X, Y, labels, [0.50,], colors='#0000FF')
        CS.collections[0].set_label('LR True Labels')
        
        labels = logistic.label_data(data, theta, normalizer=0.0, binarize=False)
        labels.shape = shape
        CS = pyplot.contour(X, Y, labels, [0.10,], colors='#AAAAFF')
        CS.collections[0].set_label('LR Pos-only Labels')

        labels = posonly.predict_proba(scaled_data)[:,1]
        labels.shape = shape
        CS = pyplot.contour(X, Y, labels, [0.50,], colors='#00FF00')
        CS.collections[0].set_label('POLR Pos-only Labels')

        print 'b: ', b
Exemplo n.º 3
0
 def predict_proba(self, X):
     a = logistic.label_data(X, self.theta_, self.b_**2, binarize=False)
     return np.vstack([1.0 - a, a]).T
Exemplo n.º 4
0
 def predict(self, X):
     return logistic.label_data(X, self.theta_, self.b_**2, binarize=True)
        x, y = np.arange(-8, 8, delta), np.arange(-10, 10, delta)
        X, Y = np.meshgrid(x, y)

        assert X.shape == Y.shape
        shape = X.shape

        data = np.hstack(
            [X.flatten().reshape(-1, 1),
             Y.flatten().reshape(-1, 1)])
        assert data.shape[0] == (shape[0] * shape[1]) and data.shape[1] == 2
        data = add_x2_y2(data)
        scaled_data = scaler.transform(data)

        # plot the LR on the true labels
        labels = logistic.label_data(data,
                                     thetaTrue,
                                     normalizer=0.0,
                                     binarize=False)
        labels.shape = shape
        CS = pyplot.contour(X, Y, labels, [
            0.50,
        ], colors='#0000FF')
        CS.collections[0].set_label('LR True Labels')

        labels = logistic.label_data(data,
                                     theta,
                                     normalizer=0.0,
                                     binarize=False)
        labels.shape = shape
        CS = pyplot.contour(X, Y, labels, [
            0.10,
        ], colors='#AAAAFF')