Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

New Feature: plotting #70

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ build/
_glmnetmodule.c
dist
MANIFEST
.ipynb_checkpoints
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased
## 2.2.2 - 2021-03-21
### Added
* [#70](https://github.com/civisanalytics/python-glmnet/pull/70)
Add new plotting functionality for cross-validation score and coefficient path.

## 2.2.1 - 2020-06-30
### Fix
Expand Down
2 changes: 2 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ numpy==1.19.0
pytest==5.4.3
scikit-learn==0.23.1
scipy==1.5.0
pandas==1.0.1
matplotlib==3.3.4
371 changes: 371 additions & 0 deletions example/ElasticNet.ipynb

Large diffs are not rendered by default.

419 changes: 419 additions & 0 deletions example/LogitNet.ipynb

Large diffs are not rendered by default.

151 changes: 150 additions & 1 deletion glmnet/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
_check_user_lambda,
_interpolate_model,
_score_lambda_path)

from glmnet.plotting import coeff_path_plot, cv_score_plot

class ElasticNet(BaseEstimator):
"""Elastic Net with squared error loss.
Expand Down Expand Up @@ -198,6 +198,8 @@ def fit(self, X, y, sample_weight=None, relative_penalties=None, groups=None):
self : object
Returns self.
"""
# keep local copy of features
self.X_ = X

X, y = check_X_y(X, y, accept_sparse='csr', ensure_min_samples=2)
if sample_weight is None:
Expand Down Expand Up @@ -446,3 +448,150 @@ def r2_reverse(y_pred, y_true):

# compute the score for each value of lambda
return np.apply_along_axis(r2_reverse, 0, pred, y)

def plot_coeff_path(self, feature_names=None, figsize=(10, 6), linestyle="-", fontsize=18,
grid=True, legend=True, legendloc="center", xlabel=None, ylabel=None,
title=None, yscale=None, bbox_to_anchor=None, save_path=None):
"""Plot coefficient's paths vs -Log(lambda).

Parameters
----------
feature_names : list, shape (n_features,)
Input features names neede for legend.

figsize : tuple or list, as (width, height)
Figure size.

linestyle: string
Linestyle of coefficients' paths.

fontsize : int, float
Fontsize of the title. The fontsizes of xlabel, ylabel,
tick_params, and legend are resized with 0.85, 0.85, 0.75,
and 0.75 fraction of title fontsize, respectively.

grid : bool
Whether to show (x,y) grid on the plot.

legend: bool
Whether to show legend on the plot.

legendloc: string
Legend location.

xlabel : string or None
Xlabel of the plot.

ylabel : string or None
Ylabel of the plot.

title : string or None
Title of the plot.

yscale: string or None
Scale for y-axis (coefficients). Valid options are
"linear", "log", "symlog", "logit". More on:
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.yscale.html

bbox_to_anchor: tuple, list or None
Relative coordinates for legend location outside of the plot.

save_path: string or None
The full or relative path to save the plot including the image format.
For example "myplot.png" or "../../myplot.pdf"

Returns None
"""
coeff_path_plot(self,
feature_names=feature_names,
figsize=figsize,
linestyle=linestyle,
fontsize=fontsize,
grid=grid,
legend=legend,
legendloc=legendloc,
xlabel=xlabel,
ylabel=ylabel,
title=title,
yscale=yscale,
bbox_to_anchor=bbox_to_anchor,
save_path=save_path
)
return None

def plot_cv_score(self, figsize=(10, 6), linestyle="--", linewidth=3, colors=None,
marker="o", markersize=5, fontsize=18, grid=True, legend=True,
legendloc="best", xlabel=None, ylabel=None, title=None, save_path=None
):
"""Plot n-folds cross-validation scores vs -Log(lambda).

Parameters
----------
figsize : tuple or list, as (width, height)
Figure size.

linestyle: string
Linestyle of the vertical lamda lines.

linewidth: integer or float
Linewidth of the vertical lamda lines.

colors: list or tuple
Colors of the marker, errorbar line, max_lambda line,
and best_lambda line, respectively. The default colors
are ("red", "black", "cyan", "blue"). The length of the
passed tuple/list should be always four.

marker: str, optional, (default="o")
Marker style. More details on:
(https://matplotlib.org/2.1.1/api/markers_api.html#module-matplotlib.markers)

markersize: intger or float
Markersize.

fontsize : int, float
Fontsize of the title. The fontsizes of xlabel, ylabel,
tick_params, and legend are resized with 0.85, 0.85, 0.75,
and 0.85 fraction of title fontsize, respectively.

grid : bool
Whether to show (x,y) grid on the plot.

legend: bool
Whether to show legend on the plot.

legendloc: string
Legend location.

xlabel : string or None
Xlabel of the plot.

ylabel : string or None
Ylabel of the plot.

title : string or None
Title of the plot.

save_path: string or None
The full or relative path to save the image including the image format.
For example "myplot.png" or "../../myplot.pdf"

Returns None
"""
cv_score_plot(self,
figsize=figsize,
linestyle=linestyle,
linewidth=linewidth,
colors=colors,
marker=marker,
markersize=markersize,
fontsize=fontsize,
grid=grid,
legend=legend,
legendloc=legendloc,
xlabel=xlabel,
ylabel=ylabel,
title=title,
save_path=save_path)

return None
159 changes: 159 additions & 0 deletions glmnet/logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
_check_user_lambda,
_interpolate_model,
_score_lambda_path)
from glmnet.plotting import coeff_path_plot, cv_score_plot


class LogitNet(BaseEstimator):
Expand Down Expand Up @@ -203,6 +204,9 @@ def fit(self, X, y, sample_weight=None, relative_penalties=None, groups=None):
self : object
Returns self.
"""
# keep local copy of features
self.X_ = X

X, y = check_X_y(X, y, accept_sparse='csr', ensure_min_samples=2)
if sample_weight is None:
sample_weight = np.ones(X.shape[0])
Expand Down Expand Up @@ -565,3 +569,158 @@ def score(self, X, y, lamb=None):
"""
pred = self.predict(X, lamb=lamb)
return np.apply_along_axis(accuracy_score, 0, pred, y)

def plot_coeff_path(self, feature_names=None, figsize=(10, 6), linestyle="-", fontsize=18,
grid=True, legend=True, legendloc="center", xlabel=None, ylabel=None,
title=None, yscale=None, bbox_to_anchor=None, save_path=None):
"""Plot coefficient's paths vs -Log(lambda).

Parameters
----------
est : estimator
The previously fitted estimator.

feature_names : list, shape (n_features,)
Input features names neede for legend.

figsize : tuple or list, as (width, height)
Figure size.

linestyle: string
Linestyle of coefficients' paths.

fontsize : int, float
Fontsize of the title. The fontsizes of xlabel, ylabel,
tick_params, and legend are resized with 0.85, 0.85, 0.75,
and 0.75 fraction of title fontsize, respectively.

grid : bool
Whether to show (x,y) grid on the plot.

legend: bool
Whether to show legend on the plot.

legendloc: string
Legend location.

xlabel : string or None
Xlabel of the plot.

ylabel : string or None
Ylabel of the plot.

title : string or None
Title of the plot.

yscale: string or None
Scale for y-axis (coefficients). Valid options are
"linear", "log", "symlog", "logit". More on:
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.yscale.html

bbox_to_anchor: tuple, list or None
Relative coordinates for legend location outside of the plot.

save_path: string or None
The full or relative path to save the plot including the image format.
For example "myplot.png" or "../../myplot.pdf"

Returns None
"""
coeff_path_plot(self,
feature_names=feature_names,
figsize=figsize,
linestyle=linestyle,
fontsize=fontsize,
grid=grid,
legend=legend,
legendloc=legendloc,
xlabel=xlabel,
ylabel=ylabel,
title=title,
yscale=yscale,
bbox_to_anchor=bbox_to_anchor,
save_path=save_path
)
return None

def plot_cv_score(self, figsize=(10, 6), linestyle="--", linewidth=3, colors=None,
marker="o", markersize=5, fontsize=18, grid=True, legend=True,
legendloc="best", xlabel=None, ylabel=None, title=None, save_path=None
):
"""Plot n-folds cross-validation scores vs -Log(lambda).

Parameters
----------
figsize : tuple or list, as (width, height)
Figure size.

linestyle: string
Linestyle of the vertical lamda lines.

linewidth: integer or float
Linewidth of the vertical lamda lines.

colors: list or tuple
Colors of the marker, errorbar line, max_lambda line,
and best_lambda line, respectively. The default colors
are ("red", "black", "cyan", "blue"). The length of the
passed tuple/list should be always four.

marker: str, optional, (default="o")
Marker style. More details on:
(https://matplotlib.org/2.1.1/api/markers_api.html#module-matplotlib.markers)

markersize: intger or float
Markersize.

fontsize : int, float
Fontsize of the title. The fontsizes of xlabel, ylabel,
tick_params, and legend are resized with 0.85, 0.85, 0.75,
and 0.85 fraction of title fontsize, respectively.

grid : bool
Whether to show (x,y) grid on the plot.

legend: bool
Whether to show legend on the plot.

legendloc: string
Legend location.

xlabel : string or None
Xlabel of the plot.

ylabel : string or None
Ylabel of the plot.

title : string or None
Title of the plot.

save_path: string or None
The full or relative path to save the image including the image format.
For example "myplot.png" or "../../myplot.pdf"

Returns None
"""
cv_score_plot(self,
figsize=figsize,
linestyle=linestyle,
linewidth=linewidth,
colors=colors,
marker=marker,
markersize=markersize,
fontsize=fontsize,
grid=grid,
legend=legend,
legendloc=legendloc,
xlabel=xlabel,
ylabel=ylabel,
title=title,
save_path=save_path)

return None





Loading