<commit_before>"""
=========================
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))
<commit_msg>set up training and testing sets<commit_after>"""
=========================
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)

# Set up the training and testing sets.
X = df[df.columns.difference(["Label"]).values]
X_train = X.iloc[0:4000, :]
X_test = X.iloc[4000:, :]

y = df.loc[:, "Label"]
y_train = y.iloc[0:4000]
y_test = y.iloc[4000:]


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


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

{'fraction_non_alphanumeric': 0.2002503128911139, 'fraction_numerical': 0.018022528160200252, 'mean_word_length': 4.29271523178808, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1656115', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1307, 'n_words': 346}