Sample Code

import numpy as np from sklearn.linear_model import PoissonRegressor from sklearn.metrics import mean_poisson_deviance # 1. Generate some synthetic count data X = np.array([[1, 2], [2, 3], [3, 4], [4, 5]]) y = np.array([2, 5, 9, 14]) # Counts must be non-negative integers # 2. Initialize the model (L2 regularization is active by default with alpha=1.0) model = PoissonRegressor(alpha=1.0, solver='lbfgs') # 3. Fit the model model.fit(X, y) # 4. Make predictions (predictions are strictly positive) y_pred = model.predict(X) # 5. Evaluate using Mean Poisson Deviance (ideal for Poisson data) loss = mean_poisson_deviance(y, y_pred) print(f"Coefficients: {model.coef_}") print(f"Intercept: {model.intercept_}") print(f"Mean Poisson Deviance: {loss:.4f}")