SKLearn-ElasticNetCV:寻找类似于Matlab的lassoPlot或R图的crossvalidationerrorplot(格尔姆内特简历(x,y)

2024-09-27 21:28:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用sklearn.linear_模型.ElasticNetCV和我希望得到一个类似于Matlab提供的带有plottype=CV或R的{}的图,即不同字母之间的交叉验证错误图(注意,在Matlab和R中,这个参数称为lambda)。下面是一个例子:

import sklearn.linear_model as lm
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp
import scipy.stats as stats

# toy example
# generate 200 samples of five-dimensional artificial data X from a 
# exponential distributions with various means:
X = np.zeros( (200,5 ) )
for col in range(5):
    X[ :, col ] = stats.expon.rvs( scale=1.0/(col+1) )

# generate response data Y = X*r + eps where r has just two nonzero
# components, and the noise eps is normal with standard deviation 0.1:
r = np.array( [ 0, 2, 0, -3, 0 ] )
Y = np.dot(X,r) + sp.randn( 200 )*0.1

enet = lm.ElasticNetCV()

alphas,coefs, _ = enet.path( X, Y )

# plot regulization paths    
plt.plot( -np.log10(alphas), coefs.T, linestyle='-' )
plt.show()

我还想在一个单独的图中绘制每个alpha的交叉验证错误。但看起来弹性网络路径()不返回mse向量。sklearn中是否有类似的功能Matlab.lassoPlotplottype='CV'见:http://de.mathworks.com/help/stats/lasso-and-elastic-net.html或R cv.glmnet(x,y)https://web.stanford.edu/~hastie/glmnet/glmnet_alpha.html。或者,我将使用sklearn.cross_validation来实现它。你有什么建议吗?在


Tags: importasstats错误nppltcolsklearn

热门问题