官术网_书友最值得收藏!

Visualizing the Lasso path

Using scikit-learn, we can easily visualize what happens as the value of the regularization parameter (alphas) changes. We will again use the Boston data, but now we will use the Lasso regression object:

las = Lasso() 
alphas = np.logspace(-5, 2, 1000)
alphas, coefs, _= las.path(x, y,
alphas=alphas)

For each value in alphas, the path method on the Lasso object returns the coefficients that solve the Lasso problem with that parameter value. Because the result changes smoothly with alpha, this can be computed very efficiently.

A typical way to visualize this path is to plot the value of the coefficients as alpha decreases. You can do so as follows:

fig,ax = plt.subplots() 
ax.plot(alphas, coefs.T)
# Set log scale
ax.set_xscale('log')
# Make alpha decrease from left to right
ax.set_xlim(alphas.max(), alphas.min())

This results in the following plot (we left out the trivial code that adds the axis labels and the title):

In this plot, the x axis shows decreasing amounts of regularization from left to right (alpha is decreasing). Each line shows how a different coefficient varies as alpha changes. The plot shows that when using very strong regularization (left side, very high alpha), the best solution is to have all values be exactly zero. As the regularization becomes weaker, one by one, the values of the different coefficients first shoot up, then stabilize. At some point, they all plateau as we are probably already close to the unpenalized solution.

主站蜘蛛池模板: 岫岩| 邵阳市| 麻城市| 东源县| 手机| 岚皋县| 丹棱县| 大化| 阳城县| 交口县| 富裕县| 德令哈市| 鄱阳县| 许昌市| 大同市| 福安市| 永胜县| 咸丰县| 睢宁县| 兰州市| 洪洞县| 思茅市| 城步| 万山特区| 青川县| 庆城县| 井冈山市| 平昌县| 永登县| 厦门市| 桓仁| 昌平区| 松江区| 屏南县| 西吉县| 西吉县| 大名县| 淮北市| 苏尼特左旗| 确山县| 临沭县|