"""
=========================
Group Lasso Example
=========================

This is an example demonstrating Pyglmnet with
multinomial the group lasso regularization, typical in regression
problems where it is reasonable to impose penalties to model parameters
in a group-wise fashion based on domain knowledge.


"""

from pyglmnet import GLM
from pyglmnet.datasets import fetch_group_lasso_datasets
import numpy as np

##########################################################
#
#Group Lasso Example
#similar to method found in:
#ftp://ftp.stat.math.ethz.ch/Manuscripts/buhlmann/lukas-sara-peter.pdf
#
#The task here is to determine which base pairs and positions within a 7-mer
#sequence are most important to predicting if the sequence contains a splice
#site or not.
#
##########################################################

print("Retrieving data...")

df , group_idxs= fetch_group_lasso_datasets()


print("Data retrieved")
print("Dataframe: ")
print(df.head())

#set up the group lasso GLM model

gl_glm = GLM(distr="binomial",
             group=group_idxs,
             tol=1e-2,
             score_metric="pseudo_R2",
             alpha=1.0,
             reg_lambda=np.logspace(np.log(100), np.log(0.01), 5, base=np.exp(1)))


#set up the non group GLM model

glm = GLM(distr="binomial",
          tol=1e-2,
          score_metric="pseudo_R2",
          alpha=1.0,
          reg_lambda=np.logspace(np.log(100), np.log(0.01), 5, base=np.exp(1)))

print("gl_glm: ", gl_glm)
print("glm: ", glm)

X = df[df.columns.difference(["Label"]).values]
y = df.loc[:, "Label"]

print("Fitting models")
gl_glm.fit(X.values, y.values)
glm.fit(X.values, y.values)
print("Model fitting complete.")
print("\n\n")


print("Group lasso post fitting score: ", gl_glm.score(X.values, y.values))
print("Non-group lasso post fitting score: ", glm.score(X.values, y.values))

@@ -1503,16 +1503,56 @@
 , glm)%0A%0A
+# Set up the training and testing sets.%0A
 X = df%5Bd
@@ -1595,30 +1595,134 @@
 es%5D%0A
-y = df.loc%5B:, %22Label%22%5D
+X_train = X.iloc%5B0:4000, :%5D%0AX_test = X.iloc%5B4000:, :%5D%0A%0Ay = df.loc%5B:, %22Label%22%5D%0Ay_train = y.iloc%5B0:4000%5D%0Ay_test = y.iloc%5B4000:%5D%0A
 %0A%0Apr
@@ -1747,32 +1747,38 @@
 s%22)%0Agl_glm.fit(X
+_train
 .values, y.value
@@ -1763,32 +1763,38 @@
 _train.values, y
+_train
 .values)%0Aglm.fit
@@ -1795,16 +1795,22 @@
 lm.fit(X
+_train
 .values,
@@ -1803,32 +1803,38 @@
 _train.values, y
+_train
 .values)%0Aprint(%22
@@ -1923,32 +1923,37 @@
 , gl_glm.score(X
+_test
 .values, y.value
@@ -1938,32 +1938,37 @@
 X_test.values, y
+_test
 .values))%0Aprint(
@@ -2018,16 +2018,21 @@
 .score(X
+_test
 .values,
@@ -2033,16 +2033,21 @@
 alues, y
+_test
 .values)

set up training and testing sets