Getting StartedΒΆ

Here is a brief example of how to use the GLM() class.

import numpy as np
import scipy.sparse as sps
from sklearn.preprocessing import StandardScaler
from pyglmnet import GLM
# create an instance of the GLM class
glm = GLM(distr='poisson')
n_samples, n_features = 10000, 100
# sample random coefficients
beta0 = np.random.normal(0.0, 1.0, 1)
beta = sps.rand(n_features, 1, 0.1)
beta = np.array(beta.todense())
# simulate training data
Xtrain = np.random.normal(0.0, 1.0, [n_samples, n_features])
ytrain = glm.simulate(beta0, beta, Xtrain)
# simulate testing data
Xtest = np.random.normal(0.0, 1.0, [n_samples, n_features])
ytest = glm.simulate(beta0, beta, Xtest)
# fit the model on the training data
scaler = StandardScaler().fit(Xtrain)
glm.fit(scaler.transform(Xtrain), ytrain)
# predict using fitted model on the test data
yhat = glm.predict(scaler.transform(Xtest))
# score the model on test data
deviance = glm.score(scaler.transform(Xtest), ytest)