Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alphas Plot #103

Closed
rebeccabilbro opened this issue Jan 20, 2017 · 7 comments
Closed

Alphas Plot #103

rebeccabilbro opened this issue Jan 20, 2017 · 7 comments
Assignees
Labels
level: intermediate python coding expertise required type: feature a new visualizer or utility for yb
Milestone

Comments

@rebeccabilbro
Copy link
Member

rebeccabilbro commented Jan 20, 2017

Create some new type of visualizer that enables the user to plot the error for different alphas. Line graph - use the line to show which alpha was selected.

@bbengfort bbengfort added level: intermediate python coding expertise required type: feature a new visualizer or utility for yb labels Jan 20, 2017
@bbengfort bbengfort modified the milestone: Backlog Jan 20, 2017
@bbengfort
Copy link
Member

bbengfort commented Feb 23, 2017

I've started investigating this issue this morning; it seems like a fairly straight forward implementation. For reference, here is the implementation from the Machine Learning slides. However, I believe the best way to implement this visualizer is to wrap one of the RegressorCV models, e.g. RidgeCV or LassoCV, etc.

This is the manual method:

from sklearn.linear_model import Ridge
from sklearn.cross_validation import cross_val_score
from sklearn.metrics import mean_squared_error

errors = []
for alpha in alphas:
    model = Ridge(alpha=alpha)
    score = cross_val_score(model, X, y, cv=12, scoring='mean_squared_error')
    errors.append(score.mean())

plt.plot(alphas, errors, label='ridge')

And here is the result:

manual

So obviously something went weird there. Also I was expecting a more jagged result, more like:

alpha_error

So there are interesting things to look at here!

@bbengfort
Copy link
Member

More investigation reveals that RidgeCV, LassoCV, ElasticNetCV, and LassoLarsCV all have different attributes to fetch for their models:

RidgeCV:

from sklearn.linear_model import RidgeCV 

# Must pass store_cv_values=True in order to store the MSE of the alphas. 
ridge = RidgeCV(alphas=alphas, store_cv_values=True)
ridge.fit(X, y)

# Plot against ridge.alphas and ridge.cv_values_
g = plt.plot(ridge.alphas, ridge.cv_values_.mean(0), label="ridge")

ridge

LassoCV:

from sklearn.linear_model import LassoCV 

lasso = LassoCV(alphas=alphas)
lasso.fit(X, y)

# plot against lasso.alphas_ and lasso.mse_path_
g = plt.plot(lasso.alphas_, lasso.mse_path_.mean(1), label="lasso")

lasso

@bbengfort
Copy link
Member

ElasticNetCV:

from sklearn.linear_model import ElasticNetCV 

enet = ElasticNetCV(alphas=alphas)
enet.fit(X, y)

g = plt.plot(enet.alphas_, enet.mse_path_.mean(1), label="elasticnet")

enet

LassoLarsCV:

from sklearn.linear_model import LassoLarsCV 

larslasso = LassoLarsCV()
larslasso.fit(X, y)

# plot against lars.cv_alphas_ and lars.cv_mse_path_
g = plt.plot(larslasso.cv_alphas_, larslasso.cv_mse_path_.mean(1), label="lasso lars")

lassolars

All of them on the same graph:

all

@bbengfort
Copy link
Member

Note: we should also put the selected alpha and final cross validation score into the plot as well!

@bbengfort bbengfort modified the milestones: Version 0.3.4, Backlog Feb 23, 2017
@bbengfort bbengfort self-assigned this Feb 23, 2017
@bbengfort bbengfort added the in progress label for Waffle board label Feb 23, 2017
@bbengfort
Copy link
Member

AlphaSelection visualizer implemented, as well as a refactoring of the regressor package!

Here are some screenshots:

from yellowbrick.regressor import AlphaSelection 
from sklearn.linear_model import RidgeCV, LassoCV, LassoLarsCV, ElasticNetCV

alphas = np.logspace(-12, -1, 400)

model = AlphaSelection(RidgeCV(alphas=alphas, scoring="neg_mean_squared_error"))
model.fit(X, y)
model.poof()

ridge

model = AlphaSelection(LassoCV(alphas=alphas))
model.fit(X, y)
model.poof()

lasso

model = AlphaSelection(LassoLarsCV())
model.fit(X, y)
model.poof()

lassolars

model = AlphaSelection(ElasticNetCV(alphas=alphas))
model.fit(X, y)
model.poof()

elasticnet

@bbengfort
Copy link
Member

Done but not in examples; closing this issue and filing bug report for the examples thing.

@BradKML
Copy link

BradKML commented Oct 26, 2022

Thanks for the note, but I am also encountering the LassoLarsCV issue as they don't require someone to define alphas.
Also as a side note how can LassoLarsIC use this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
level: intermediate python coding expertise required type: feature a new visualizer or utility for yb
Projects
None yet
Development

No branches or pull requests

3 participants