Skip to content

Instantly share code, notes, and snippets.

@agramfort
Created November 11, 2011 02:41
Show Gist options
  • Save agramfort/1357024 to your computer and use it in GitHub Desktop.
Save agramfort/1357024 to your computer and use it in GitHub Desktop.
parameter scaling by n_samples pb
import numpy as np
from scipy import linalg
from sklearn import datasets, svm, linear_model
from sklearn.svm import l1_min_c
iris = datasets.load_iris()
X = iris.data
y = iris.target
X = X[y != 2]
y = y[y != 2]
X -= np.mean(X, 0)
X2 = np.r_[X, X]
y2 = np.r_[y, y]
clfs = [linear_model.LogisticRegression(penalty='l1', tol=1e-6),
linear_model.LogisticRegression(penalty='l2', tol=1e-6),
svm.SVR(kernel='linear', tol=1e-6)]
for clf in clfs:
for scaling, scaling2 in [(1., 1.), (len(X), len(X2))]:
clf.C = 10. / scaling
coef_ = clf.fit(X, y).coef_
clf.C = 10. / scaling2
coef2_ = clf.fit(X2, y2).coef_
print "Error : %s" % (linalg.norm(coef2_ - coef_) / linalg.norm(coef_))
clfs = [linear_model.Lasso(tol=1e-6)]
for clf in clfs:
for scaling, scaling2 in [(1., 1.), (len(X), len(X2))]:
clf.alpha = 0.001 * scaling
coef_ = clf.fit(X, y).coef_
clf.alpha = 0.001 * scaling2
coef2_ = clf.fit(X2, y2).coef_
print "Error : %s" % (linalg.norm(coef2_ - coef_) / linalg.norm(coef_))
# Output:
# Error : 0.13608933675
# Error : 8.56854960126e-16
# Error : 0.124357027952
# Error : 3.02427897402e-16
# Error : 0.000213002674373
# Error : 2.40941736763e-07
# Error : 4.07295351853e-12
# Error : 0.16694490818
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment