### Install Documentation Dependencies Source: https://glm-tools.github.io/pyglmnet/_sources/contributing.rst.txt Install the required packages for building documentation using pip. ```bash pip install sphinx sphinx-gallery pillow numpydoc matplotlib ``` -------------------------------- ### Install development version of pyglmnet Source: https://glm-tools.github.io/pyglmnet/_sources/install.rst.txt Install the development version of pyglmnet by cloning the repository and installing from the GitHub zipball URL. ```bash pip install https://api.github.com/repos/glm-tools/pyglmnet/zipball/master ``` -------------------------------- ### Install flake8 Source: https://glm-tools.github.io/pyglmnet/_sources/contributing.rst.txt Install the flake8 tool for PEP8 style checking. ```bash pip install flake8 ``` -------------------------------- ### Importing dependencies Source: https://glm-tools.github.io/pyglmnet/tutorial.html Initial imports required for the tutorial examples. ```python # Author: Pavan Ramkumar # License: MIT import numpy as np from scipy.special import expit ``` -------------------------------- ### Install development version from GitHub Source: https://glm-tools.github.io/pyglmnet/install.html Install the development version of pyglmnet by cloning the repository from GitHub using pip. ```bash pip install https://api.github.com/repos/glm-tools/pyglmnet/zipball/master ``` -------------------------------- ### Install dependencies with pip Source: https://glm-tools.github.io/pyglmnet/install.html Install the required Python packages for pyglmnet using pip. The first command installs numpy and scipy, and the second upgrades scikit-learn. ```bash pip install numpy scipy pip install -U scikit-learn ``` -------------------------------- ### Author and License Information Source: https://glm-tools.github.io/pyglmnet/_downloads/d388352c4ab7ebcb43e806c7eee944e7/plot_group_lasso.ipynb Metadata identifying the author and license for the example. ```python # Author: Matthew Antalek # License: MIT ``` -------------------------------- ### Install testing dependencies Source: https://glm-tools.github.io/pyglmnet/_sources/contributing.rst.txt Install pytest and pytest-cov for running tests locally. ```bash pip install pytest pytest-cov ``` -------------------------------- ### Author and license comment Source: https://glm-tools.github.io/pyglmnet/_downloads/210165b04144331bdfcde83b07b6c7bd/plot_community_crime.ipynb Identifies the author and license for the example. ```python # Author: Vinicius Marques # License: MIT ``` -------------------------------- ### Import required libraries for group lasso example Source: https://glm-tools.github.io/pyglmnet/_sources/auto_examples/plot_group_lasso.rst.txt Imports GLMCV, fetch_group_lasso_datasets, and matplotlib.pyplot for the group lasso example. ```python from pyglmnt import GLMCV from pyglmnet.datasets import fetch_group_lasso_datasets import matplotlib.pyplot as plt ``` -------------------------------- ### Install stable pyglmnet with pip Source: https://glm-tools.github.io/pyglmnet/_sources/install.rst.txt Install the latest stable release of pyglmnet from PyPI. ```bash pip install pyglmnet ``` -------------------------------- ### GLM Model Fitting and Prediction Example Source: https://glm-tools.github.io/pyglmnet/api.html Demonstrates how to initialize a GLM model with a Gaussian distribution, fit it to synthetic data, and perform predictions. ```python >>> import numpy as np >>> random_state = 1 >>> n_samples, n_features = 100, 4 >>> rng = np.random.RandomState(random_state) >>> X = rng.normal(0, 1, (n_samples, n_features)) >>> y = 2.2 * X[:, 0] -1.0 * X[:, 1] + 0.3 * X[:, 3] + 1.0 >>> glm = GLM(distr='gaussian', verbose=False, random_state=random_state) >>> glm = glm.fit(X, y) >>> glm.beta0_ # The intercept 1.005380485553247 >>> glm.beta_ # The coefficients array([ 1.90216711, -0.78782533, -0. , 0.03227455]) >>> y_pred = glm.predict(X) ``` -------------------------------- ### Install dependencies with pip Source: https://glm-tools.github.io/pyglmnet/_sources/install.rst.txt Install the required Python packages for pyglmnet using pip. This is needed if you have a Python distribution other than Canopy or Anaconda. ```bash pip install numpy scipy pip install -U scikit-learn ``` -------------------------------- ### Tikhonov Model Output Source: https://glm-tools.github.io/pyglmnet/auto_examples/plot_tikhonov.html Example output showing convergence warnings and model scores. ```text /Users/mainak/Documents/github_repos/pyglmnet/pyglmnet/pyglmnet.py:864: UserWarning: Reached max number of iterations without convergence. "Reached max number of iterations without convergence.") train score: 0.048506 test score: 0.015134 ``` -------------------------------- ### Create a new feature branch Source: https://glm-tools.github.io/pyglmnet/_sources/contributing.rst.txt Start developing a new feature on a dedicated branch. ```bash git checkout -b feat ``` -------------------------------- ### GLM Fit Method Source: https://glm-tools.github.io/pyglmnet/_modules/pyglmnet/pyglmnet.html The fit method performs input validation, checks for group constraints, and initializes model parameters before starting the optimization process. ```python [docs] def fit(self, X, y): """The fit function. Parameters ---------- X: array The 2D input data of shape (n_samples, n_features) y: array The 1D target data of shape (n_samples,) Returns ------- self: instance of GLM The fitted model. """ # checks for group if self.group is not None: self.group = np.array(self.group) self.group = self.group.astype(np.int64) # shape check if self.group.shape[0] != X.shape[1]: raise ValueError('group should be (n_features,)') # int check if not np.all([isinstance(g, np.int64) for g in self.group]): raise ValueError('all entries of group should be integers') # type check for data if not (isinstance(X, np.ndarray) and isinstance(y, np.ndarray)): msg = ("Input must be ndarray. Got {} and {}" .format(type(X), type(y))) raise ValueError(msg) if X.ndim != 2: raise ValueError("X must be a 2D array, got %sD" % X.ndim) if y.ndim != 1: raise ValueError("y must be 1D, got %sD" % y.ndim) if hasattr(self, '_allow_refit') and self._allow_refit is False: raise ValueError("This glm object has already been fit before." "A refit is not allowed") n_observations, n_features = X.shape if n_observations != len(y): raise ValueError('Shape mismatch.' + 'X has {} observations, y has {}.' .format(n_observations, len(y))) # Initialize parameters beta = np.zeros((n_features + int(self.fit_intercept),)) if self.fit_intercept: if self.beta0_ is None and self.beta_ is None: beta[0] = 1 / (n_features + 1) * \ self.rng.normal(0.0, 1.0, 1) beta[1:] = 1 / (n_features + 1) * \ self.rng.normal(0.0, 1.0, (n_features, )) else: beta[0] = self.beta0_ beta[1:] = self.beta_ else: if self.beta0_ is None and self.beta_ is None: beta = 1 / (n_features + 1) * \ self.rng.normal(0.0, 1.0, (n_features, )) else: beta = self.beta_ logger.info('Lambda: %6.4f' % self.reg_lambda) tol = self.tol alpha = self.alpha reg_lambda = self.reg_lambda if self.solver == 'cdfast': # init active set ActiveSet = np.ones_like(beta) ``` -------------------------------- ### Build Documentation Locally Source: https://glm-tools.github.io/pyglmnet/_sources/contributing.rst.txt Generate the documentation pages locally using the make command. The output is stored in _build/html/. ```bash make html ``` -------------------------------- ### Deploy Documentation to gh-pages Source: https://glm-tools.github.io/pyglmnet/_sources/contributing.rst.txt Push the built documentation pages to the gh-pages branch. ```bash make install ``` -------------------------------- ### Clone the repository Source: https://glm-tools.github.io/pyglmnet/_sources/contributing.rst.txt Initial clone of the forked repository. ```bash git clone https://github.com//pyglmnet ``` -------------------------------- ### Configure upstream remote Source: https://glm-tools.github.io/pyglmnet/_sources/contributing.rst.txt Add the main upstream repository and verify the remote configuration. ```bash git remote add upstream https://github.com/glm-tools/pyglmnet git remote -v ``` -------------------------------- ### Fetch and Load Data Source: https://glm-tools.github.io/pyglmnet/_downloads/041aa0da5178deb73584c29f2ae0aecb/plot_tikhonov.ipynb Downloads the required dataset into a temporary directory and loads CSV files into pandas DataFrames. ```python from pyglmnet.datasets import fetch_tikhonov_data with TemporaryDirectory(prefix="tmp_glm-tools") as temp_dir: dpath = fetch_tikhonov_data(dpath=temp_dir) fixations_df = pd.read_csv(op.join(dpath, 'fixations.csv')) probes_df = pd.read_csv(op.join(dpath, 'probes.csv')) probes_df = pd.read_csv(op.join(dpath, 'probes.csv')) spikes_df = pd.read_csv(op.join(dpath, 'spiketimes.csv')) spiketimes = np.squeeze(spikes_df.values) ``` -------------------------------- ### Simulate data for GLM Source: https://glm-tools.github.io/pyglmnet/start.html Sets up a sparse model and simulates training and test data using simulate_glm with a Poisson distribution. ```python n_samples, n_features = 1000, 100 distr = 'poisson' # sample a sparse model beta0 = np.random.normal(0.0, 1.0, 1) beta = sps.rand(n_features, 1, 0.1) beta = np.array(beta.todense()) # simulate data Xtrain = np.random.normal(0.0, 1.0, [n_samples, n_features]) ytrain = simulate_glm('poisson', beta0, beta, Xtrain)[:, 0] Xtest = np.random.normal(0.0, 1.0, [n_samples, n_features]) ytest = simulate_glm('poisson', beta0, beta, Xtest)[:, 0] ``` -------------------------------- ### Download and preprocess data Source: https://glm-tools.github.io/pyglmnet/_downloads/210165b04144331bdfcde83b07b6c7bd/plot_community_crime.ipynb Fetches the Communities and Crime dataset and extracts the number of samples and features. ```python X, y = datasets.fetch_community_crime_data() n_samples, n_features = X.shape ``` -------------------------------- ### Download and preprocess data files Source: https://glm-tools.github.io/pyglmnet/auto_examples/plot_community_crime.html Fetches the Communities and Crime dataset and extracts the feature matrix X and target vector y, along with their shapes. ```python X, y = datasets.fetch_community_crime_data() n_samples, n_features = X.shape ``` -------------------------------- ### GLM Class Usage Source: https://glm-tools.github.io/pyglmnet/_sources/start.rst.txt Demonstrates how to instantiate the GLM class, fit it on training data, predict on test data, and score the model. ```APIDOC ## GLM Class ### Description This example shows how to use the GLM class for fitting a generalized linear model, predicting, and scoring. ### Usage ```python from pyglmnet import GLM, simulate_glm # Create an instance of the GLM class glm = GLM(distr='poisson', score_metric='deviance') # Fit the model on the training data glm.fit(Xtrain, ytrain) # Predict using fitted model on the test data yhat = glm.predict(Xtest) # Score the model on test data deviance = glm.score(Xtest, ytest) ``` ### Methods #### fit(X, y) - **Description**: Fit the GLM model to the training data. - **Parameters**: - X (array-like) - Training features. - y (array-like) - Training target values. - **Returns**: None #### predict(X) - **Description**: Predict target values for new data using the fitted model. - **Parameters**: - X (array-like) - Test features. - **Returns**: Predicted values (array-like). #### score(X, y) - **Description**: Compute the score (e.g., deviance) of the model on given data. - **Parameters**: - X (array-like) - Test features. - y (array-like) - True target values. - **Returns**: Score value (float). ### Constructor Parameters - **distr** (str) - Distribution type (e.g., 'poisson'). - **score_metric** (str) - Metric used for scoring (e.g., 'deviance'). ``` -------------------------------- ### Import pyglmnet and dependencies Source: https://glm-tools.github.io/pyglmnet/_sources/start.rst.txt Initial imports required to use the GLM class and simulation utilities. ```python import numpy as np import scipy.sparse as sps from pyglmnet import GLM, simulate_glm ``` -------------------------------- ### Clean Built Documentation Source: https://glm-tools.github.io/pyglmnet/_sources/contributing.rst.txt Remove the generated documentation files from the local repository. ```bash make clean ``` -------------------------------- ### GLM.__init__ Source: https://glm-tools.github.io/pyglmnet/_modules/pyglmnet/pyglmnet.html Constructor for the GLM class. Initializes the model with distribution family, regularization parameters, solver settings, and other configuration options. ```APIDOC ## GLM.__init__ ### Description Initializes a GLM (Generalized Linear Model) object with specified distribution family, regularization, solver, and optimization parameters. ### Method Constructor ### Signature GLM(distr='poisson', alpha=0.5, Tau=None, group=None, reg_lambda=None, cv=10, solver='batch-gradient', learning_rate=2e-1, max_iter=1000, tol=1e-6, eta=2.0, score_metric='deviance', fit_intercept=True, random_state=0, verbose=False) ### Parameters - **distr** (str) - Optional - Distribution family. One of 'gaussian', 'binomial', 'poisson', 'softplus', 'probit', 'gamma'. Default: 'poisson'. - **alpha** (float) - Optional - Weighting between L1 and L2 penalty terms. Default: 0.5. - **Tau** (array | None) - Optional - Tikhonov matrix of shape (n_features, n_features). Default: None (identity, ridge-like L2 penalty). - **group** (array | list | None) - Optional - Group identities for each parameter (except beta0). Entries are ints from 1 to n_groups; 0 means no group. Default: None (L1 regularization). - **reg_lambda** (array | list | None) - Optional - Array of regularization parameters lambda. Default: None (10 floats logarithmically spaced between 0.5 and 0.01). - **cv** (cross validation object) - Optional - Iterator for cross-validation. Default: 10. - **solver** (str) - Optional - Optimization method: 'batch-gradient' or 'cdfast'. Default: 'batch-gradient'. - **learning_rate** (float) - Optional - Learning rate for gradient descent. Default: 2e-1. - **max_iter** (int) - Optional - Maximum iterations. Default: 1000. - **tol** (float) - Optional - Convergence threshold (norm of gradient). Default: 1e-6. - **eta** (float) - Optional - Threshold parameter to linearize exp() above eta. Default: 2.0. - **score_metric** (str) - Optional - Scoring metric: 'deviance' or 'pseudo_R2'. Default: 'deviance'. - **fit_intercept** (boolean) - Optional - Whether to add intercept. Default: True. - **random_state** (int) - Optional - Seed for random number generator. Default: 0. - **verbose** (boolean or int) - Optional - Verbosity. Default: False. ### Attributes - **beta0_** (int) - The intercept. - **beta_** (array, shape (n_features,)) - The learned betas. - **glm_** (instance of GLM) - The GLM object with best score. - **reg_lambda_opt_** (float) - The reg_lambda parameter for best GLM object. - **n_iter_** (int) - The number of iterations. ### Notes To select a subset of fitted glm models, use slicing: glm = glm[1:3]; glm[2].predict(X_test). ### Reference Friedman, Hastie, Tibshirani (2010). Regularization Paths for Generalized Linear Models via Coordinate Descent, J Statistical Software. https://core.ac.uk/download/files/153/6287975.pdf ``` -------------------------------- ### GLM.__init__ Source: https://glm-tools.github.io/pyglmnet/_modules/pyglmnet/pyglmnet.html Constructor for the GLM class. Initializes the model with distribution family, regularization parameters, solver settings, and other configuration options. ```APIDOC ## GLM.__init__ ### Description Initializes a GLM object with specified distribution, regularization, solver, and other parameters. ### Method Constructor ### Signature GLM(distr='poisson', alpha=0.5, Tau=None, group=None, reg_lambda=0.1, solver='batch-gradient', learning_rate=2e-1, max_iter=1000, tol=1e-6, eta=2.0, score_metric='deviance', fit_intercept=True, random_state=0, callback=None, verbose=False) ### Parameters - **distr** (str) - Optional - Distribution family. One of 'gaussian', 'binomial', 'poisson', 'softplus', 'probit', 'gamma'. Default: 'poisson'. - **alpha** (float) - Optional - Weighting between L1 and L2 penalty terms. Default: 0.5. - **Tau** (array | None) - Optional - Tikhonov matrix of shape (n_features, n_features). Default: None (identity, ridge penalty). - **group** (array | list | None) - Optional - Group identities for each parameter (except beta0). Entries are ints from 1 to n_groups; 0 means no group. Default: None (L1 regularization). - **reg_lambda** (float) - Optional - Regularization parameter lambda. Default: 0.1. - **solver** (str) - Optional - Optimization method. One of 'batch-gradient' or 'cdfast'. Default: 'batch-gradient'. - **learning_rate** (float) - Optional - Learning rate for gradient descent. Default: 2e-1. - **max_iter** (int) - Optional - Maximum iterations. Default: 1000. - **tol** (float) - Optional - Convergence threshold; stops when norm(gradient) is below threshold. Default: 1e-6. - **eta** (float) - Optional - Threshold parameter to linearize exp() above eta. Default: 2.0. - **score_metric** (str) - Optional - Scoring metric. One of 'deviance' or 'pseudo_R2'. Default: 'deviance'. - **fit_intercept** (boolean) - Optional - Whether to add intercept. Default: True. - **random_state** (int) - Optional - Seed for random number generator. Default: 0. - **verbose** (boolean or int) - Optional - Verbosity. Default: False. ### Attributes - **beta0_** (int) - The intercept. - **beta_** (array, shape (n_features,)) - The learned coefficients. - **n_iter_** (int) - Number of iterations. ### Example ```python import numpy as np random_state = 1 n_samples, n_features = 100, 4 rng = np.random.RandomState(random_state) X = rng.normal(0, 1, (n_samples, n_features)) y = 2.2 * X[:, 0] -1.0 * X[:, 1] + 0.3 * X[:, 3] + 1.0 glm = GLM(distr='gaussian', verbose=False, random_state=random_state) glm = glm.fit(X, y) glm.beta0_ # The intercept 1.005380485553247 glm.beta_ # The coefficients array([ 1.90216711, -0.78782533, -0. , 0.03227455]) y_pred = glm.predict(X) ``` ``` -------------------------------- ### Fetch and display the group lasso dataset Source: https://glm-tools.github.io/pyglmnet/_sources/auto_examples/plot_group_lasso.rst.txt Fetches the group lasso dataset and prints a preview. The output shows a progress indication and the head of the returned DataFrame. ```python df, group_idxs = fetch_group_lasso_datasets() print(df.head()) ``` ```none ...0%, 0 MB ...8%, 0 MB ...16%, 0 MB ...24%, 0 MB ...32%, 0 MB ...40%, 0 MB ...48%, 0 MB ...56%, 0 MB ...64%, 0 MB ...73%, 0 MB ...81%, 0 MB ...89%, 0 MB ...97%, 0 MB ...100%, 0 MB ``` -------------------------------- ### Set up group lasso and lasso GLM models Source: https://glm-tools.github.io/pyglmnet/_sources/auto_examples/plot_group_lasso.rst.txt Creates two GLMCV models: one with group indices for group lasso and one without for standard lasso. Both use binomial distribution, pseudo_R2 score metric, alpha=1.0, learning_rate=3, max_iter=100, and 3-fold cross-validation. The models are printed to show their configuration. ```python # set up the group lasso GLM model gl_glm = GLMCV(distr="binomial", tol=1e-3, group=group_idxs, score_metric="pseudo_R2", alpha=1.0, learning_rate=3, max_iter=100, cv=3, verbose=True) # set up the lasso model glm = GLMCV(distr="binomial", tol=1e-3, score_metric="pseudo_R2", alpha=1.0, learning_rate=3, max_iter=100, cv=3, verbose=True) print("gl_glm: ", gl_glm) print("glm: ", glm) ``` ```none gl_glm: < Distribution | binomial alpha | 1.00 max_iter | 100.00 lambda: 0.50 to 0.01 > glm: < Distribution | binomial alpha | 1.00 max_iter | 100.00 lambda: 0.50 to 0.01 > ``` -------------------------------- ### Model initialization output Source: https://glm-tools.github.io/pyglmnet/auto_examples/plot_group_lasso.html The expected console output after initializing the GLMCV models. ```text gl_glm: < Distribution | binomial alpha | 1.00 max_iter | 100.00 lambda: 0.50 to 0.01 > glm: < Distribution | binomial alpha | 1.00 max_iter | 100.00 lambda: 0.50 to 0.01 > ``` -------------------------------- ### Verify remote configuration output Source: https://glm-tools.github.io/pyglmnet/_sources/contributing.rst.txt Expected output after configuring the upstream remote. ```console upstream https://github.com/glm-tools/pyglmnet.git (fetch) upstream https://github.com/glm-tools/pyglmnet.git (push) origin http://github.com//pyglmnet.git (fetch) origin http://github.com//pyglmnet.git (push) ``` -------------------------------- ### Initialize Group Lasso and Lasso Models Source: https://glm-tools.github.io/pyglmnet/_downloads/d388352c4ab7ebcb43e806c7eee944e7/plot_group_lasso.ipynb Configures GLMCV models for both group lasso (using group_idxs) and standard lasso regularization. ```python # set up the group lasso GLM model gl_glm = GLMCV(distr="binomial", tol=1e-3, group=group_idxs, score_metric="pseudo_R2", alpha=1.0, learning_rate=3, max_iter=100, cv=3, verbose=True) # set up the lasso model glm = GLMCV(distr="binomial", tol=1e-3, score_metric="pseudo_R2", alpha=1.0, learning_rate=3, max_iter=100, cv=3, verbose=True) print("gl_glm: ", gl_glm) print("glm: ", glm) ``` -------------------------------- ### Simulate sparse model data Source: https://glm-tools.github.io/pyglmnet/_sources/start.rst.txt Generate training and testing datasets using a Poisson distribution for model evaluation. ```python n_samples, n_features = 1000, 100 distr = 'poisson' # sample a sparse model beta0 = np.random.normal(0.0, 1.0, 1) beta = sps.rand(n_features, 1, 0.1) beta = np.array(beta.todense()) # simulate data Xtrain = np.random.normal(0.0, 1.0, [n_samples, n_features]) ytrain = simulate_glm('poisson', beta0, beta, Xtrain)[:, 0] Xtest = np.random.normal(0.0, 1.0, [n_samples, n_features]) ytest = simulate_glm('poisson', beta0, beta, Xtest)[:, 0] ``` -------------------------------- ### Output of data download Source: https://glm-tools.github.io/pyglmnet/auto_examples/plot_community_crime.html Console output showing the progress of downloading the dataset, ending with 100% and 1 MB. ```text ...0%, 0 MB ...0%, 0 MB ...1%, 0 MB ...2%, 0 MB ...2%, 0 MB ...3%, 0 MB ...4%, 0 MB ...5%, 0 MB ...5%, 0 MB ...6%, 0 MB ...7%, 0 MB ...8%, 0 MB ...8%, 0 MB ...9%, 0 MB ...10%, 0 MB ...11%, 0 MB ...11%, 0 MB ...12%, 0 MB ...13%, 0 MB ...14%, 0 MB ...14%, 0 MB ...15%, 0 MB ...16%, 0 MB ...17%, 0 MB ...17%, 0 MB ...18%, 0 MB ...19%, 0 MB ...20%, 0 MB ...20%, 0 MB ...21%, 0 MB ...22%, 0 MB ...23%, 0 MB ...23%, 0 MB ...24%, 0 MB ...25%, 0 MB ...25%, 0 MB ...26%, 0 MB ...27%, 0 MB ...28%, 0 MB ...28%, 0 MB ...29%, 0 MB ...30%, 0 MB ...31%, 0 MB ...31%, 0 MB ...32%, 0 MB ...33%, 0 MB ...34%, 0 MB ...34%, 0 MB ...35%, 0 MB ...36%, 0 MB ...37%, 0 MB ...37%, 0 MB ...38%, 0 MB ...39%, 0 MB ...40%, 0 MB ...40%, 0 MB ...41%, 0 MB ...42%, 0 MB ...43%, 0 MB ...43%, 0 MB ...44%, 0 MB ...45%, 0 MB ...46%, 0 MB ...46%, 0 MB ...47%, 0 MB ...48%, 0 MB ...49%, 0 MB ...49%, 0 MB ...50%, 0 MB ...51%, 0 MB ...51%, 0 MB ...52%, 0 MB ...53%, 0 MB ...54%, 0 MB ...54%, 0 MB ...55%, 0 MB ...56%, 0 MB ...57%, 0 MB ...57%, 0 MB ...58%, 0 MB ...59%, 0 MB ...60%, 0 MB ...60%, 0 MB ...61%, 0 MB ...62%, 0 MB ...63%, 0 MB ...63%, 0 MB ...64%, 0 MB ...65%, 0 MB ...66%, 0 MB ...66%, 0 MB ...67%, 0 MB ...68%, 0 MB ...69%, 0 MB ...69%, 0 MB ...70%, 0 MB ...71%, 0 MB ...72%, 0 MB ...72%, 0 MB ...73%, 0 MB ...74%, 0 MB ...75%, 0 MB ...75%, 0 MB ...76%, 0 MB ...77%, 0 MB ...77%, 0 MB ...78%, 0 MB ...79%, 0 MB ...80%, 0 MB ...80%, 0 MB ...81%, 0 MB ...82%, 0 MB ...83%, 0 MB ...83%, 0 MB ...84%, 0 MB ...85%, 0 MB ...86%, 0 MB ...86%, 0 MB ...87%, 0 MB ...88%, 0 MB ...89%, 0 MB ...89%, 0 MB ...90%, 0 MB ...91%, 0 MB ...92%, 0 MB ...92%, 0 MB ...93%, 0 MB ...94%, 0 MB ...95%, 1 MB ...95%, 1 MB ...96%, 1 MB ...97%, 1 MB ...98%, 1 MB ...98%, 1 MB ...99%, 1 MB ...100%, 1 MB ``` -------------------------------- ### Run style checks Source: https://glm-tools.github.io/pyglmnet/_sources/contributing.rst.txt Check code style using flake8. ```bash flake8 --count pyglmnet ``` -------------------------------- ### Fit and score the GLM model Source: https://glm-tools.github.io/pyglmnet/_sources/start.rst.txt Instantiate the GLM class, fit the model to training data, and evaluate performance using test data. ```python # create an instance of the GLM class glm = GLM(distr='poisson', score_metric='deviance') # fit the model on the training data glm.fit(Xtrain, ytrain) # predict using fitted model on the test data yhat = glm.predict(Xtest) # score the model on test data deviance = glm.score(Xtest, ytest) ``` -------------------------------- ### Import Necessary Libraries Source: https://glm-tools.github.io/pyglmnet/_downloads/041aa0da5178deb73584c29f2ae0aecb/plot_tikhonov.ipynb Imports required modules for data handling, GLM modeling, and visualization. ```python import os.path as op import numpy as np import pandas as pd from pyglmnet import GLMCV from spykes.ml.strf import STRF import matplotlib.pyplot as plt from tempfile import TemporaryDirectory ``` -------------------------------- ### Run tests Source: https://glm-tools.github.io/pyglmnet/_sources/contributing.rst.txt Execute tests using pytest with coverage reporting. ```bash py.test --cov=pyglmnet tests/ ``` -------------------------------- ### Import required libraries Source: https://glm-tools.github.io/pyglmnet/_downloads/210165b04144331bdfcde83b07b6c7bd/plot_community_crime.ipynb Imports matplotlib, scikit-learn's train_test_split, and pyglmnet's GLM, GLMCV, and datasets modules. ```python import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from pyglmnet import GLM, GLMCV, datasets ``` -------------------------------- ### Define Model Parameters Source: https://glm-tools.github.io/pyglmnet/_downloads/041aa0da5178deb73584c29f2ae0aecb/plot_tikhonov.ipynb Sets the spatial extent, window of interest, bin size, and zero padding for the model. ```python # Spatial extent n_shape = 50 n_features = n_spatial_basis # Window of interest window = [-100, 100] # Bin size binsize = 20 # Zero pad bins n_zero_bins = int(np.floor((window[1] - window[0]) / binsize / 2)) ``` -------------------------------- ### GLMCV constructor Source: https://glm-tools.github.io/pyglmnet/api.html Initializes a GLMCV object for regularized generalized linear model estimation with cross-validation. ```APIDOC ## GLMCV(distr='poisson', alpha=0.5, Tau=None, group=None, reg_lambda=None, cv=10, solver='batch-gradient', learning_rate=0.2, max_iter=1000, tol=1e-06, eta=2.0, score_metric='deviance', fit_intercept=True, random_state=0, verbose=False) ### Description Creates a GLMCV instance for estimating regularized generalized linear models along a regularization path with warm restarts. ### Parameters - **distr** (str) - Optional - Distribution family: 'gaussian' | 'binomial' | 'poisson' | 'softplus' | 'probit' | 'gamma'. Default: 'poisson'. - **alpha** (float) - Optional - Weighting between L1 and L2 penalty. Default: 0.5. - **Tau** (array | None) - Optional - Tikhonov matrix of shape (n_features, n_features). Default: None (identity). - **group** (array | list | None) - Optional - Group identities for each parameter. Default: None (L1 regularization). - **reg_lambda** (array | list | None) - Optional - Array of regularization parameters. Default: None (10 floats log-spaced between 0.5 and 0.01). - **cv** (cross validation object) - Optional - Iterator for cross validation. Default: 10. - **solver** (str) - Optional - Optimization method: 'batch-gradient' or 'cdfast'. Default: 'batch-gradient'. - **learning_rate** (float) - Optional - Learning rate for gradient descent. Default: 0.2. - **max_iter** (int) - Optional - Maximum iterations. Default: 1000. - **tol** (float) - Optional - Convergence threshold. Default: 1e-6. - **eta** (float) - Optional - Threshold for linearizing exp(). Default: 2.0. - **score_metric** (str) - Optional - Scoring metric: 'deviance' or 'pseudo_R2'. Default: 'deviance'. - **fit_intercept** (boolean) - Optional - Whether to add intercept. Default: True. - **random_state** (int) - Optional - Seed for random number generator. Default: 0. - **verbose** (boolean or int) - Optional - Verbosity. Default: False. ### Attributes - **beta0_** (int) - The intercept. - **beta_** (array, shape (n_features)) - The learned betas. - **glm_** (instance of GLM) - The GLM object with best score. - **reg_lambda_opt_** (float) - The reg_lambda for best GLM object. - **n_iter_** (int) - The number of iterations. ```