資源簡介
L2正則化python實現案例(附代碼),含圖形展示,對于正則化理解又直觀幫助

代碼片段和文件信息
import?numpy?as?np
from?numpy?import?*
import?matplotlib.pylab?as?plt
from?pylab?import?*?????????????????????????????#顯示中文
mpl.rcParams[‘font.sans-serif‘]?=?[‘SimHei‘]??#顯示中文
#畫圖中正確顯示負數
import?matplotlib
matplotlib.rcParams[‘axes.unicode_minus‘]=False
#加載數據
train_data=np.loadtxt(‘ex1data1.txt‘delimiter=‘‘);#?train?data
test_data=np.loadtxt(‘test1.txt‘delimiter=‘‘)
#?new_test=test_data.copy();
#?for?i?in?range(new_test.shape[0]):
#?????new_test[i-1]=new_test[i-1]*(1.0+(0.4*np.random.random()-0.2))
#?np.savetxt(‘test1.txt‘new_testdelimiter=‘‘)
#提取數據,x和y
train_X=train_data[:0];
train_y=train_data[:-1]
test_X=test_data[:0];
test_y=test_data[:-1]
#數據標準化
m1=train_X.shape[0];?#?m=len(X)
train_X=np.c_[np.ones(m1)train_X]
train_y=np.c_[train_y]
m2=test_X.shape[0];?#?m=len(X)
test_X=np.c_[np.ones(m2)test_X]
test_y=np.c_[test_y]
#定義代價函數
def?costFunction(Xythetalamda):
????m=X.shape[0]
????h=np.dot(Xtheta)
????theta_r?=?theta.copy()??#?theta_r和theta指向不同的地址,若theta_r=theta指向同一個地址
????theta_r[0?0]?=?0.0??#?theta0不參加正則化,所以,設置為0.0
????R=lamda/(2.0*m)*np.dot(theta_r.Ttheta_r)???#正則化項
????J=1.0/(2.0*m)*np.dot((h-y).T(h-y))?+R???#帶正則化的代價函數
????return?J
#定義梯度下降
def?gradDesc(Xyalpha=0.001lamda=0.0iter_num=15000):
????mn=X.shape???#樣本數m,特征數n
????theta=np.zeros((n1))??#初始化theta
????J_history=np.zeros(iter_num)???#初始化代價函數值
????#開始梯度下降
????for?i?in?range(iter_num):
????????J_history[i]=costFunction(Xythetalamda)???#計算代價值
????????h=np.dot(Xtheta)??#預測值
????????theta_r=theta.copy()???#theta_r和theta指向不同的地址,若theta_r=theta指向同一個地址
????????theta_r[00]=0.0???????#theta0不參加正則化,所以,設置為0.0
????????deltatheta=1.0/m*np.dot(X.T(h-y))+lamda/m*theta_r??#計算帶正則化的deltatheta
????????theta-=alpha*deltatheta???#更新theta
????return?J_historytheta
#執行梯度下降(帶正則化項和不帶正則化項)
J_historytheta=gradDesc(train_Xtrain_y)???#無正則化項
print(‘無正則化時theta=‘theta)
plt.plot(J_history‘r‘label=‘無正則化‘)
J_history_rtheta_r=gradDesc(test_Xtest_ylamda=200)???#有正則化項
print(‘有正則化時theta=‘theta_r)
plt.plot(J_history_r‘g‘label=‘有正則化‘)
plt.legend(loc=‘best‘)
plt.title(‘正則化前后的代價曲線‘)
plt.show()
#?#調用ridge嶺回歸
#?from?sklearn.linear_model?import?Ridge
#?ridge_reg?=?Ridge(alpha=200solver=“cholesky“)??#solver=‘sag‘
#?ridge_reg.fit(train_X?train_y)
#畫數據圖比較正則化前后的變化
plt.figure(figsize=(2525))
plt.subplot(211)
plt.title(‘訓練集及回歸曲線‘)
plt.scatter(train_X[:1]train_y[:0])
plt.plot(train_X[:1]np.dot(train_Xtheta)c=‘r‘label=‘回歸曲線‘)
plt.legend(loc=‘lower?right‘shadow=Truefacecolor=‘0.9‘)
plt.subplot(212)
plt.title(‘測試集正則化前后的變化‘)
plt.scatter(test_X[:1]test_y[:0])
plt.plot(test_X[:1]np.dot(test_Xtheta)c=‘r‘label=‘lamda=0‘)
plt.plot(test_X[:1]np.dot(test_Xtheta_r)c=‘g‘label=‘lamda=200‘)
#?plt.plot(test_X[:1]ridge_reg.predict(test_X)c=‘k‘label=‘ridge=200‘)
plt.legend(loc=‘lower?right‘shadow=Truefacecolor=‘0.9‘)
plt.show()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3478??2019-02-03?19:21??LR_regular.py
?????文件???????1359??2017-10-21?10:37??ex1data1.txt
?????文件???????5155??2019-01-26?11:01??test1.txt
-----------?---------??----------?-----??----
?????????????????9992????????????????????3
評論
共有 條評論