@@ -0,0 +1,2099 @@
+import warnings%0A%0Afrom scipy.optimize import fmin_l_bfgs_b%0Aimport numpy as np%0Aimport scipy.sparse as sp%0A%0Afrom Orange import classification%0Aimport Orange.data%0A%0A# helper functions%0Adef append_ones(X):%0A    if sp.issparse(X):%0A        return sp.hstack((np.ones((X.shape%5B0%5D, 1)), X)).tocsr()%0A    else:%0A        return np.hstack((np.ones((X.shape%5B0%5D, 1)), X))%0A%0Adef sigmoid(x):%0A    return 1.0 / (1.0 + np.exp(-x))%0A%0Adef cost_grad(theta, X, y, lambda_):%0A    sx = sigmoid(X.dot(theta))%0A%0A    j = -np.log(np.where(y, sx, 1 - sx)).sum()%0A    j += lambda_ * theta.dot(theta) / 2.0%0A    j /= X.shape%5B0%5D%0A%0A    grad = X.T.dot(sx - y)%0A    grad += lambda_ * theta%0A    grad /= X.shape%5B0%5D%0A%0A    return j, grad%0A%0Adef fit(X, y, lambda_):%0A    theta = np.zeros(X.shape%5B1%5D)%0A    theta, _, ret = fmin_l_bfgs_b(cost_grad, theta, args=(X, y, lambda_))%0A    if ret%5B'warnflag'%5D != 0:%0A        warnings.warn('L-BFGS failed to converge')%0A    return theta%0A%0Adef predict(X, theta):%0A    return sigmoid(X.dot(theta))%0A%0A%0A# how it should look like%0Aclass LogisticRegressionSimple:%0A    def __init__(self, lambda_=1):%0A        self.lambda_ = lambda_%0A%0A    def fit(self, X, y):%0A        self.theta = fit(X, y, self.lambda_)%0A%0A    def predict(self, X):%0A        return predict(X, self.theta)%0A%0A%0A# how it would look like in Orange4.0%0Aclass LogisticRegressionLearner(classification.Learner):%0A    def __init__(self, lambda_=1):%0A        self.lambda_ = lambda_%0A%0A    def __call__(self, data):%0A        theta = fit(data.X, data.Y.ravel(), self.lambda_)%0A        return LogisticRegressionClassifier(theta)%0A%0Aclass LogisticRegressionClassifier(classification.Classifier):%0A    def __init__(self, theta):%0A        self.theta = theta%0A%0A    def __call__(self, X):%0A        return predict(X.X, self.theta)%0A        %0A        %0A%0Aif __name__ == '__main__':%0A    iris = Orange.data.Table('../doc/datasets/iris')%0A    iris.Y%5Biris.Y != 0.0%5D = 1.0%0A%0A    m = LogisticRegressionSimple(lambda_=2)%0A    m.fit(iris.X, iris.Y.ravel())%0A    p_simple = m.predict(iris.X)%0A%0A    c = LogisticRegressionLearner(lambda_=2)%0A    l = c(iris)%0A    p_orange = l(iris)%0A%0A    assert np.allclose(p_simple, p_orange)%0A    %0A

Add L2-regularized logistic regression.