python實(shí)現(xiàn)梯度下降法
本文實(shí)例為大家分享了python實(shí)現(xiàn)梯度下降法的具體代碼,供大家參考,具體內(nèi)容如下
使用工具:Python(x,y) 2.6.6運(yùn)行環(huán)境:Windows10
問(wèn)題:求解y=2*x1+x2+3,即使用梯度下降法求解y=a*x1+b*x2+c中參數(shù)a,b,c的最優(yōu)值(監(jiān)督學(xué)習(xí))
訓(xùn)練數(shù)據(jù):
x_train=[1, 2], [2, 1],[2, 3], [3, 5], [1,3], [4, 2], [7, 3], [4, 5], [11, 3], [8, 7]
y_train=[7, 8, 10, 14, 8, 13, 20, 16, 28,26]
測(cè)試數(shù)據(jù):
x_test = [1, 4],[2, 2],[2, 5],[5, 3],[1,5],[4, 1]
# -*- coding: utf-8 -*-'''Created on Wed Nov 16 09:37:03 2016@author: Jason''' import numpy as npimport matplotlib.pyplot as plt # y=2 * (x1) + (x2) + 3 rate = 0.001x_train = np.array([[1, 2], [2, 1],[2, 3], [3, 5], [1, 3], [4, 2], [7, 3], [4, 5], [11, 3], [8, 7] ])y_train = np.array([7, 8, 10, 14, 8, 13, 20, 16, 28, 26])x_test = np.array([[1, 4],[2, 2],[2, 5],[5, 3],[1, 5],[4, 1]]) a = np.random.normal()b = np.random.normal()c = np.random.normal() def h(x): return a*x[0]+b*x[1]+c for i in range(100): sum_a=0 sum_b=0 sum_c=0 for x, y in zip(x_train, y_train): for xi in x: sum_a = sum_a+ rate*(y-h(x))*xi sum_b = sum_b+ rate*(y-h(x))*xi #sum_c = sum_c + rate*(y-h(x)) *1 a = a + sum_a b = b + sum_b c = c + sum_c plt.plot([h(xi) for xi in x_test]) print(a)print(b)print(c) result=[h(xi) for xi in x_train]print(result) result=[h(xi) for xi in x_test]print(result) plt.show()
運(yùn)行結(jié)果:
結(jié)論:線(xiàn)段是在逐漸逼近的,訓(xùn)練數(shù)據(jù)越多,迭代次數(shù)越多就越逼近真實(shí)值。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python中scrapy處理項(xiàng)目數(shù)據(jù)的實(shí)例分析2. GIT相關(guān)-IDEA/ECLIPSE工具配置的教程詳解3. js抽獎(jiǎng)轉(zhuǎn)盤(pán)實(shí)現(xiàn)方法分析4. IntelliJ IDEA導(dǎo)入jar包的方法5. 快速搭建Spring Boot+MyBatis的項(xiàng)目IDEA(附源碼下載)6. 教你在 IntelliJ IDEA 中使用 VIM插件的詳細(xì)教程7. Python requests庫(kù)參數(shù)提交的注意事項(xiàng)總結(jié)8. 如何基于Python實(shí)現(xiàn)word文檔重新排版9. 深入分析PHP設(shè)計(jì)模式10. PHP橋接模式Bridge Pattern的優(yōu)點(diǎn)與實(shí)現(xiàn)過(guò)程
