Beispiel #1
0
class lasso_transformer:
    def __init__(self,r,groups_ids):
        self.lasso = GroupLasso(    groups=groups_ids,    group_reg=r,     l1_reg=r,    n_iter = 1000,
                                       scale_reg="None",       supress_warning=True,    tol=1e-04,
    )
    def fit(self,X,y):
        self.lasso.fit(X,y.values.reshape(-1, 1))

    def transform(self,X,y=None):
        return self.lasso.transform(X)


    def fit_transform(self,X,y):
        self.fit(X,y)
        return self.transform(X,y)
    )
    print("Starting fit")
    gl.LOG_LOSSES = True
    gl.fit(X, y)

    for i in range(w.shape[1]):
        plt.figure()
        plt.subplot(211)
        plt.plot(w[:, i], ".", label="True weights")
        plt.plot(gl.coef_[:, i], ".", label="Estimated weights")

        plt.subplot(212)
        plt.plot(w[gl.sparsity_mask, i], ".", label="True weights")
        plt.plot(gl.coef_[gl.sparsity_mask, i], ".", label="Estimated weights")
        plt.legend()

    plt.figure()
    plt.plot([w.min(), w.max()], [gl.coef_.min(), gl.coef_.max()], "gray")
    plt.scatter(w, gl.coef_, s=10)
    plt.ylabel("Learned coefficients")
    plt.xlabel("True coefficients")

    plt.figure()
    plt.plot(gl.losses_)

    print("X shape: {X.shape}".format(X=X))
    print("Transformed X shape: {shape}".format(shape=gl.transform(X).shape))
    print("True intercept: {intercept}".format(intercept=intercept))
    print("Estimated intercept: {intercept}".format(intercept=gl.intercept_))
    plt.show()