[TOC] 更新、更全的《機器學習》的更新網站,更有python、go、數據結構與算法、爬蟲、人工智能教學等着你:<a target="_blank" href="https://www.cnblogs.com/nickchen121/p/11686958.html">http://www.javashuo.com/article/p-vozphyqp-cm.html</a>html
import pandas as pd import numpy as np import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties from sklearn.linear_model import Lasso from sklearn.linear_model import Ridge from sklearn.linear_model import ElasticNet %matplotlib inline font = FontProperties(fname='/Library/Fonts/Heiti.ttc')
df = pd.read_csv('housing-data.txt', sep='\s+', header=0) X = df[['RM']].values y = df['MEDV'].values
# Lasso(L1)正則迴歸 lasso = Lasso(alpha=1.0) lasso.fit(X, y) lasso_predict = lasso.predict(X) # Ridge(L2)正則迴歸 ridge = Ridge(alpha=1.0) ridge.fit(X, y) ridge_predict = ridge.predict(X) # ElasticNet(彈性網絡)迴歸 # l1_ratio=0時等同於Lasso(L1)迴歸 elastic_net = ElasticNet(alpha=0.1, l1_ratio=0.5) elastic_net.fit(X, y) elastic_net_predict = elastic_net.predict(X)
plt.scatter(X, y, c='gray', edgecolor='white', marker='s', label='訓練數據') plt.plot(X, lasso_predict, c='r',label='L1正則化',linestyle='--') plt.plot(X, ridge_predict, c='b',label='L2正則化',linestyle='-') plt.plot(X, elastic_net_predict, c='g',label='彈性網絡',linestyle=':') plt.xlabel('平均房間數目[MEDV]', fontproperties=font) plt.ylabel('以1000美圓爲計價單位的房價[RM]', fontproperties=font) plt.title('波士頓房價預測', fontproperties=font, fontsize=20) plt.legend(prop=font) plt.show()
![png](http://www.chenyoude.com/ml/02-10 正則化線性迴歸(波士頓房價預測)_8_0.png?x-oss-process=style/watermark)python