slickml.regression._glmnet
#
Module Contents#
Classes#
GLMNetCVRegressor. |
- class slickml.regression._glmnet.GLMNetCVRegressor[source]#
Bases:
sklearn.base.BaseEstimator
,sklearn.base.RegressorMixin
GLMNetCVRegressor.
This is wrapper using GLM-Net [glmnet-api] to train a Regularized Linear Model via ElasticNet regression and find the optimal penalty values through N-Folds cross validation. In principle, GLMNet (also known as ElasticNet) can also be used for feature selection and dimensionality reduction using the LASSO (Least Absolute Shrinkage and Selection Operator) Regression part of the alogrithm while reaching a solid solution using the Ridge Regression part of the algorithm.
- Parameters:
alpha (float, optional) – The stability parameter with a possible values of
0 <= alpha <= 1
wherealpha=0.0
andalpha=1.0
will lead to classic Ridge and LASSO regression models, respectively, by default 0.5n_lambda (int, optional) – Maximum number of penalty values to compute, by default 100
n_splits (int, optional) – Number of cross validation folds for computing performance metrics and determining
lambda_best_
andlambda_max_
. If non-zero, must beat least 3, by default 3metric (str, optional) – Metric used for model selection during cross validation. Valid options are
"r2"
,"mean_squared_error"
,"mean_absolute_error"
, and"median_absolute_error"
. The metric affects the selection oflambda_best_
andlambda_max_
. Thus, fitting the same data with different metric methods will result in the selection of different models, by default “r2”scale (bool, optional) – Whether to standardize the input features to have a mean value of 0.0 and standard deviation of 1 prior to fitting. The final coefficients will be on the scale of the original data regardless of this step. Therefore, there is no need to pre-process the data when using
scale=True
, by default Truesparse_matrix (bool, optional) – Whether to convert the input features to sparse matrix with csr format or not. This would increase the speed of feature selection for relatively large sparse datasets. Additionally, this parameter cannot be used along with
scale=True
where standardizing the feature matrix to have a mean value of zero would turn the feature matrix into a dense matrix, by default Falsefit_intercept (bool, optional) – Include an intercept term in the model, by default True
cut_point (float, optional) – The cut point to use for selecting
lambda_best_
. Based on this value, the distance betweenlambda_max_
andlambda_best_
would becut_point * standard_error(lambda_best_)
``arg_max(lambda) for cv_score(lambda) >= cv_score(lambda_max_) - cut_point * standard_error(lambda_max_), by default 1.0min_lambda_ratio (float, optional) – In combination with
n_lambda
, the ratio of the smallest and largest values of lambda computed(min_lambda/max_lambda >= min_lambda_ratio)
, by default 1e-4tolerance (float, optional) – Convergence criteria tolerance, by default 1e-7
max_iter (int, optional) – Maximum passes over the data, by default 100000
random_state (int, optional) – Seed for the random number generator. The glmnet solver is not deterministic, this seed is used for determining the cv folds.
lambda_path (Union[List[float], np.ndarray, pd.Series], optional) – In place of supplying
n_lambda
, provide an array of specific values to compute. The specified values must be in decreasing order. When None, the path of lambda values will be determined automatically. A maximum ofn_lambda
values will be computed, by default Nonemax_features (int, optional) – Optional maximum number of features with nonzero coefficients after regularization. If not set, defaults to the number features (
X_train.shape[1]
) during fit. Note, this will be ignored if the user specifieslambda_path
, by default None
- fit(X_train, y_train)[source]#
Fits a
glmnet.ElasticNet
to input training data. ProperX_train
matrix based on chosen options i.e.sparse_matrix
, andscale
is being created based on the passedX_train
andy_train
- plot_coeff_path():
Visualizes the coefficients’ paths
- get_params():
Returns parameters
- get_intercept():
Returns model’s intercept
- get_coeffs():
Returns non-zero coefficients
- get_cv_results():
Returns cross-validation results
- get_results():
Returns model’s total results
- X_train#
Returns training data set
- Type:
pd.DataFrame
- X_test#
Returns transformed testing data set
- Type:
pd.DataFrame
- y_train#
Returns the list of training ground truth for training (targets)
- Type:
np.ndarray
- y_test#
Returns the list of testing ground truth for training (targets)
- Type:
np.ndarray
- coeff_#
Return the model’s non-zero coefficients
- Type:
pd.DataFrame
- cv_results_#
Returns the cross-validation results
- Type:
pd.DataFrame
- shap_values_train_#
Shapley values from
LinearExplainer
usingX_train
- Type:
np.ndarray
- shap_values_test_#
Shapley values from
LinearExplainer
usingX_test
- Type:
np.ndarray
- shap_explainer_#
Shap LinearExplainer with independent masker using
X_Test
- Type:
shap.LinearExplainer
- model_#
Returns fitted
glmnet.ElasticNet
model- Type:
glmnet.ElasticNet
References
- alpha :Optional[float] = 0.5#
- cut_point :Optional[float] = 1.0#
- fit_intercept :Optional[bool] = True#
- lambda_path :Optional[Union[List[float], numpy.ndarray, pandas.Series]]#
- max_features :Optional[int]#
- max_iter :Optional[int] = 100000#
- metric :Optional[str] = r2#
- min_lambda_ratio :Optional[float] = 0.0001#
- n_lambda :Optional[int] = 100#
- n_splits :Optional[int] = 3#
- random_state :Optional[int] = 1367#
- scale :Optional[bool] = True#
- sparse_matrix :Optional[bool] = False#
- tolerance :Optional[float] = 1e-07#
- __getstate__()#
- __repr__(N_CHAR_MAX=700)#
Return repr(self).
- __setstate__(state)#
- fit(X_train: Union[pandas.DataFrame, numpy.ndarray], y_train: Union[List[float], numpy.ndarray, pandas.Series]) None [source]#
Fits a
glmnet.ElasticNet
to input training data.Notes
For the cases that
sparse_matrix=True
, a CSR format of the input will be used viadf_to_csr()
function.- Parameters:
X_train (Union[pd.DataFrame, np.ndarray]) – Input data for training (features)
y_train (Union[List[float], np.ndarray, pd.Series]) – Input ground truth for training (targets)
- Returns:
None
- get_coeffs(output: Optional[str] = 'dataframe') Union[Dict[str, float], pandas.DataFrame] [source]#
Returns model’s coefficients in different format.
- Parameters:
output (str, optional) – Output format with possible values of “dataframe” and “dict”, by default “dataframe”
- Returns:
Union[Dict[str, float], pd.DataFrame]
- get_cv_results() pandas.DataFrame [source]#
Returns model’s cross-validation results.
See also
- Returns:
pd.DataFrame
- get_results() Dict[str, Any] [source]#
Returns model’s total results.
See also
- Returns:
Dict[str, Any]
- get_shap_explainer() shap.LinearExplainer [source]#
Returns
shap.LinearExplainer
object.- Returns:
shap.LinearExplainer
- plot_coeff_path(figsize: Optional[Tuple[Union[int, float], Union[int, float]]] = (8, 5), linestyle: Optional[str] = '-', fontsize: Optional[Union[int, float]] = 12, grid: Optional[bool] = True, legend: Optional[bool] = True, legendloc: Optional[Union[int, str]] = 'center', xlabel: Optional[str] = None, ylabel: Optional[str] = 'Coefficients', title: Optional[str] = None, bbox_to_anchor: Tuple[float, float] = (1.1, 0.5), yscale: Optional[str] = 'linear', save_path: Optional[str] = None, display_plot: Optional[bool] = True, return_fig: Optional[bool] = False) Optional[matplotlib.figure.Figure] [source]#
Visualizes the GLMNet coefficients’ paths.
- Parameters:
figsize (tuple, optional) – Figure size, by default (8, 5)
linestyle (str, optional) – Linestyle of paths, by default “-”
fontsize (Union[int, float], optional) – 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, by default 12
grid (bool, optional) – Whether to show (x,y) grid on the plot or not, by default True
legend (bool, optional) – Whether to show legend on the plot or not, by default True
legendloc (Union[int, str], optional) – Location of legend, by default “center”
xlabel (str, optional) – Xlabel of the plot, by default “-Log(Lambda)”
ylabel (str, optional) – Ylabel of the plot, by default “Coefficients”
title (str, optional) – Title of the plot, by default “Best {lambda_best} with {n} Features”
yscale (str, optiona) – Scale for y-axis (coefficients). Possible options are
"linear"
,"log"
,"symlog"
,"logit"
[yscale], by default “linear”bbox_to_anchor (Tuple[float, float], optional) – Relative coordinates for legend location outside of the plot, by default (1.1, 0.5)
save_path (str, optional) – The full or relative path to save the plot including the image format such as “myplot.png” or “../../myplot.pdf”, by default None
display_plot (bool, optional) – Whether to show the plot, by default True
return_fig (bool, optional) – Whether to return figure object, by default False
**kwargs (Dict[str, Any]) – Key-value pairs of results.
results_
attribute can be used
- Returns:
Figure, optional
- plot_cv_results(figsize: Optional[Tuple[Union[int, float], Union[int, float]]] = (8, 5), marker: Optional[str] = 'o', markersize: Optional[Union[int, float]] = 5, color: Optional[str] = 'red', errorbarcolor: Optional[str] = 'black', maxlambdacolor: Optional[str] = 'purple', bestlambdacolor: Optional[str] = 'navy', linestyle: Optional[str] = '--', fontsize: Optional[Union[int, float]] = 12, grid: Optional[bool] = True, legend: Optional[bool] = True, legendloc: Optional[Union[int, str]] = 'best', xlabel: Optional[str] = None, ylabel: Optional[str] = None, title: Optional[str] = None, save_path: Optional[str] = None, display_plot: Optional[bool] = True, return_fig: Optional[bool] = False) Optional[matplotlib.figure.Figure] [source]#
Visualizes the GLMNet cross-validation results.
Notes
This plotting function can be used along with
results_
attribute of any ofGLMNetCVClassifier
, orGLMNetCVRegressor
classes askwargs
.- Parameters:
figsize (tuple, optional) – Figure size, by default (8, 5)
marker (str, optional) – Marker style of the metric to distinguish the error bars. More valid marker styles can be found at [markers-api], by default “o”
markersize (Union[int, float], optional) – Markersize, by default 5
color (str, optional) – Line and marker color, by default “red”
errorbarcolor (str, optional) – Error bar color, by default “black”
maxlambdacolor (str, optional) – Color of vertical line for
lambda_max_
, by default “purple”bestlambdacolor (str, optional) – Color of vertical line for
lambda_best_
, by default “navy”linestyle (str, optional) – Linestyle of vertical lambda lines, by default “–”
fontsize (Union[int, float], optional) – 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, by default 12
grid (bool, optional) – Whether to show (x,y) grid on the plot or not, by default True
legend (bool, optional) – Whether to show legend on the plot or not, by default True
legendloc (Union[int, str], optional) – Location of legend, by default “best”
xlabel (str, optional) – Xlabel of the plot, by default “-Log(Lambda)”
ylabel (str, optional) – Ylabel of the plot, by default “{n_splits}-Folds CV Mean {metric}”
title (str, optional) – Title of the plot, by default “Best {lambda_best} with {n} Features”
save_path (str, optional) – The full or relative path to save the plot including the image format such as “myplot.png” or “../../myplot.pdf”, by default None
display_plot (bool, optional) – Whether to show the plot, by default True
return_fig (bool, optional) – Whether to return figure object, by default False
**kwargs (Dict[str, Any]) – Key-value pairs of results.
results_
attribute can be used
- Returns:
Figure, optional
- plot_shap_summary(validation: Optional[bool] = True, plot_type: Optional[str] = 'dot', figsize: Optional[Union[str, Tuple[float, float]]] = 'auto', color: Optional[str] = None, cmap: Optional[matplotlib.colors.LinearSegmentedColormap] = None, max_display: Optional[int] = 20, feature_names: Optional[List[str]] = None, layered_violin_max_num_bins: Optional[int] = 10, title: Optional[str] = None, sort: Optional[bool] = True, color_bar: Optional[bool] = True, class_names: Optional[List[str]] = None, class_inds: Optional[List[int]] = None, color_bar_label: Optional[str] = 'Feature Value', save_path: Optional[str] = None, display_plot: Optional[bool] = True) None [source]#
Visualizes shap beeswarm plot as summary of shapley values.
Notes
This is a helper function to plot the
shap
summary plot based on all types ofshap.Explainer
includingshap.LinearExplainer
for linear models,shap.TreeExplainer
for tree-based models, andshap.DeepExplainer
deep neural network models. More on details are available at [shap-api]. Note that this function should be ran after thepredict_proba()
to make sure theX_test
is being instansiated or setvalidation=False
.- Parameters:
validation (bool, optional) – Whether to calculate Shap values of using the validation data
X_test
or not. Whenvalidation=False
, Shap values are calculated usingX_train
, be default Trueplot_type (str, optional) – The type of summary plot where possible options are “bar”, “dot”, “violin”, “layered_violin”, and “compact_dot”. Recommendations are “dot” for single-output such as binary classifications, “bar” for multi-output problems, “compact_dot” for Shap interactions, by default “dot”
figsize (tuple, optional) – Figure size where “auto” is auto-scaled figure size based on the number of features that are being displayed. Passing a single float will cause each row to be that many inches high. Passing a pair of floats will scale the plot by that number of inches. If None is passed then the size of the current figure will be left unchanged, by default “auto”
color (str, optional) – Color of plots when
plot_type="violin"
andplot_type=layered_violin"
are “RdBl” color-map while color of the horizontal lines whenplot_type="bar"
is “#D0AAF3”, by default Nonecmap (LinearSegmentedColormap, optional) – Color map when
plot_type="violin"
andplot_type=layered_violin"
, by default “RdBl”max_display (int, optional) – Limit to show the number of features in the plot, by default 20
feature_names (List[str], optional) – List of feature names to pass. It should follow the order of features, by default None
layered_violin_max_num_bins (int, optional) – The number of bins for calculating the violin plots ranges and outliers, by default 10
title (str, optional) – Title of the plot, by default None
sort (bool, optional) – Flag to plot sorted shap vlues in descending order, by default True
color_bar (bool, optional) – Flag to show a color bar when
plot_type="dot"
orplot_type="violin"
class_names (List[str], optional) – List of class names for multi-output problems, by default None
class_inds (List[int], optional) – List of class indices for multi-output problems, by default None
color_bar_label (str, optional) – Label for color bar, by default “Feature Value”
save_path (str, optional) – The full or relative path to save the plot including the image format such as “myplot.png” or “../../myplot.pdf”, by default None
display_plot (bool, optional) – Whether to show the plot, by default True
- Returns:
None
- plot_shap_waterfall(validation: Optional[bool] = True, figsize: Optional[Tuple[float, float]] = (8, 5), bar_color: Optional[str] = '#B3C3F3', bar_thickness: Optional[Union[float, int]] = 0.5, line_color: Optional[str] = 'purple', marker: Optional[str] = 'o', markersize: Optional[Union[int, float]] = 7, markeredgecolor: Optional[str] = 'purple', markerfacecolor: Optional[str] = 'purple', markeredgewidth: Optional[Union[int, float]] = 1, max_display: Optional[int] = 20, title: Optional[str] = None, fontsize: Optional[Union[int, float]] = 12, save_path: Optional[str] = None, display_plot: Optional[bool] = True, return_fig: Optional[bool] = False) Optional[matplotlib.figure.Figure] [source]#
Visualizes the Shapley values as a waterfall plot.
Notes
Waterfall is defined as the cumulitative/composite ratios of shap values per feature. Therefore, it can be easily seen with each feature how much explainability we can achieve. Note that this function should be ran after the
predict_proba()
to make sure theX_test
is being instansiated or setvalidation=False
.- Parameters:
validation (bool, optional) – Whether to calculate Shap values of using the validation data
X_test
or not. Whenvalidation=False
, Shap values are calculated usingX_train
, be default Truefigsize (Tuple[float, float], optional) – Figure size, by default (8, 5)
bar_color (str, optional) – Color of the horizontal bar lines, “#B3C3F3”
bar_thickness (Union[float, int], optional) – Thickness (hight) of the horizontal bar lines, by default 0.5
line_color (str, optional) – Color of the line plot, by default “purple”
marker (str, optional) – Marker style of the lollipops. More valid marker styles can be found at [2]_, by default “o”
markersize (Union[int, float], optional) – Markersize, by default 7
markeredgecolor (str, optional) – Marker edge color, by default “purple”
markerfacecolor (str, optional) – Marker face color, by default “purple”
markeredgewidth (Union[int, float], optional) – Marker edge width, by default 1
max_display (int, optional) – Limit to show the number of features in the plot, by default 20
title (str, optional) – Title of the plot, by default None
fontsize (Union[int, float], optional) – Fontsize for xlabel and ylabel, and ticks parameters, by default 12
save_path (str, optional) – The full or relative path to save the plot including the image format such as “myplot.png” or “../../myplot.pdf”, by default None
display_plot (bool, optional) – Whether to show the plot, by default True
return_fig (bool, optional) – Whether to return figure object, by default False
- Returns:
Figure, optional
- predict(X_test: Union[pandas.DataFrame, numpy.ndarray], y_test: Optional[Union[List[float], numpy.ndarray, pandas.Series]] = None, lamb: Optional[numpy.ndarray] = None) numpy.ndarray [source]#
Returns the prediction response values (targets).
- Parameters:
X_test (Union[pd.DataFrame, np.ndarray]) – Input data for testing (features)
y_test (Union[List[float], np.ndarray, pd.Series], optional) – Input ground truth for testing (targets)
lamb (np.ndarray, optional) – Values with shape
(n_lambda,)
of lambda fromlambda_path_
from which to make predictions. If no values are provided (None), the returned predictions will be those corresponding tolambda_best_
. The values of lamb must also be in the range oflambda_path_
, values greater thanmax(lambda_path_)
or less thanmin(lambda_path_)
will be clipped
- Returns:
np.ndarray
- score(X, y, sample_weight=None)#
Return the coefficient of determination of the prediction.
The coefficient of determination \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares
((y_true - y_pred)** 2).sum()
and \(v\) is the total sum of squares((y_true - y_true.mean()) ** 2).sum()
. The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape
(n_samples, n_samples_fitted)
, wheren_samples_fitted
is the number of samples used in the fitting for the estimator.y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True values for X.
sample_weight (array-like of shape (n_samples,), default=None) – Sample weights.
- Returns:
score (float) – \(R^2\) of
self.predict(X)
wrt. y.
Notes
The \(R^2\) score used when calling
score
on a regressor usesmultioutput='uniform_average'
from version 0.23 to keep consistent with default value ofr2_score()
. This influences thescore
method of all the multioutput regressors (except forMultiOutputRegressor
).
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters:
**params (dict) – Estimator parameters.
- Returns:
self (estimator instance) – Estimator instance.