Um herauszufinden, wie die LASSORegression funktioniert, habe ich einen kleinen Code geschrieben, der die LASSORegression durch Auswahl des besten Alpha-Parameters optimieren soll .
Ich kann nicht herausfinden, warum die LASSORegression nach der Kreuzvalidierung so instabile Ergebnisse für den Alpha-Parameter liefert.
Hier ist mein Python-Code:
from sklearn.linear_model import Lasso
from sklearn.cross_validation import KFold
from matplotlib import pyplot as plt
# generate some sparse data to play with
import numpy as np
import pandas as pd
from scipy.stats import norm
from scipy.stats import uniform
### generate your own data here
n = 1000
x1x2corr = 1.1
x1x3corr = 1.0
x1 = range(n) + norm.rvs(0, 1, n) + 50
x2 = map(lambda aval: aval*x1x2corr, x1) + norm.rvs(0, 2, n) + 500
y = x1 + x2 #+ norm.rvs(0,10, n)
Xdf = pd.DataFrame()
Xdf['x1'] = x1
Xdf['x2'] = x2
X = Xdf.as_matrix()
# Split data in train set and test set
n_samples = X.shape[0]
X_train, y_train = X[:n_samples / 2], y[:n_samples / 2]
X_test, y_test = X[n_samples / 2:], y[n_samples / 2:]
kf = KFold(X_train.shape[0], n_folds = 10, )
alphas = np.logspace(-16, 8, num = 1000, base = 2)
e_alphas = list()
e_alphas_r = list() # holds average r2 error
for alpha in alphas:
lasso = Lasso(alpha=alpha, tol=0.004)
err = list()
err_2 = list()
for tr_idx, tt_idx in kf:
X_tr, X_tt = X_train[tr_idx], X_test[tt_idx]
y_tr, y_tt = y_train[tr_idx], y_test[tt_idx]
lasso.fit(X_tr, y_tr)
y_hat = lasso.predict(X_tt)
# returns the coefficient of determination (R^2 value)
err_2.append(lasso.score(X_tt, y_tt))
# returns MSE
err.append(np.average((y_hat - y_tt)**2))
e_alphas.append(np.average(err))
e_alphas_r.append(np.average(err_2))
## print out the alpha that gives the minimum error
print 'the minimum value of error is ', e_alphas[e_alphas.index(min(e_alphas))]
print ' the minimizer is ', alphas[e_alphas.index(min(e_alphas))]
## <<< plotting alphas against error >>>
plt.figsize = (15, 15)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(alphas, e_alphas, 'b-')
ax.plot(alphas, e_alphas_r, 'g--')
ax.set_ylim(min(e_alphas),max(e_alphas))
ax.set_xlim(min(alphas),max(alphas))
ax.set_xlabel("alpha")
plt.show()
Wenn Sie diesen Code wiederholt ausführen, ergeben sich für Alpha völlig unterschiedliche Ergebnisse:
>>>
the minimum value of error is 3.99254192539
the minimizer is 1.52587890625e-05
>>> ================================ RESTART ================================
>>>
the minimum value of error is 4.07412455842
the minimizer is 6.45622425334
>>> ================================ RESTART ================================
>>>
the minimum value of error is 4.25898253597
the minimizer is 1.52587890625e-05
>>> ================================ RESTART ================================
>>>
the minimum value of error is 3.79392968781
the minimizer is 28.8971008254
>>>
Warum konvergiert der Alpha-Wert nicht richtig? Ich weiß, dass meine Daten synthetisch sind, aber die Verteilung ist dieselbe. Auch die Variation ist in x1und sehr gering x2.
Was könnte dazu führen, dass dies so instabil ist?
Dasselbe, was in R geschrieben ist, führt zu unterschiedlichen Ergebnissen - es gibt immer den höchstmöglichen Wert für Alpha als "optimal_alpha" zurück.
Ich habe das auch in R geschrieben, was mir eine etwas andere Antwort gibt, die ich nicht weiß warum?
library(glmnet)
library(lars)
library(pracma)
set.seed(1)
k = 2 # number of features selected
n = 1000
x1x2corr = 1.1
x1 = seq(n) + rnorm(n, 0, 1) + 50
x2 = x1*x1x2corr + rnorm(n, 0, 2) + 500
y = x1 + x2
filter_out_label <- function(col) {col!="y"}
alphas = logspace(-5, 6, 100)
for (alpha in alphas){
k = 10
optimal_alpha = NULL
folds <- cut(seq(1, nrow(df)), breaks=k, labels=FALSE)
total_mse = 0
min_mse = 10000000
for(i in 1:k){
# Segement your data by fold using the which() function
testIndexes <- which(folds==i, arr.ind=TRUE)
testData <- df[testIndexes, ]
trainData <- df[-testIndexes, ]
fit <- lars(as.matrix(trainData[Filter(filter_out_label, names(df))]),
trainData$y,
type="lasso")
# predict
y_preds <- predict(fit, as.matrix(testData[Filter(filter_out_label, names(df))]),
s=alpha, type="fit", mode="lambda")$fit # default mode="step"
y_true = testData$y
residuals = (y_true - y_preds)
mse=sum(residuals^2)
total_mse = total_mse + mse
}
if (total_mse < min_mse){
min_mse = total_mse
optimal_alpha = alpha
}
}
print(paste("the optimal alpha is ", optimal_alpha))
Die Ausgabe des obigen R-Codes lautet:
> source('~.....')
[1] "the optimal alpha is 1e+06"
Unabhängig davon, was ich für die Zeile " alphas = logspace(-5, 6, 100)" eingestellt habe, erhalte ich immer den höchsten Wert für Alpha zurück.
Ich denke, hier gibt es tatsächlich zwei verschiedene Fragen:
Warum ist der Alpha-Wert für die in Python geschriebene Version so instabil?
Warum gibt mir die in R geschriebene Version ein anderes Ergebnis? (Mir ist klar, dass sich die
logspaceFunktion vonRbis unterscheidetpython, aber die eingeschriebene VersionRgibt mir immer den größten Wertalphafür den optimalen Alpha-Wert, während die Python-Version dies nicht tut).
Es wäre toll, diese Dinge zu wissen ...


fit_interceptParameter beim Erstellen des Lasso-Modells zu verwenden.