Python implements gradient descent method

The examples in this article share the specific code of python to implement the gradient descent method for your reference. The specific content is as follows

**Use tool: **Python(x,y) 2.6.6
**Operating environment: **Windows10

Problem: Solve y=2x1+x2+3, that is, use the gradient descent method to solve the optimal values of parameters a, b, c in y=ax1+b*x2+c (supervised learning)

**Training data: **

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]

Test Data:

x_test = [1, 4],[2, 2],[2, 5],[5, 3],[1,5],[4, 1]

# - *- coding: utf-8-*-"""
Created on Wed Nov 1609:37:032016
@ author: Jason
"""
 
import numpy as np
import matplotlib.pyplot as plt
 
# y=2*(x1)+(x2)+3 
 
rate =0.001
x_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 inrange(100):
 sum_a=0
 sum_b=0
 sum_c=0for x, y inzip(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()

operation result:

**Conclusion: The ** line segment is gradually approaching. The more training data and the more iterations, the closer to the true value.

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements gradient descent method
python implements the gradient method python the fastest descent method
Python implements the steepest descent method
Implementation of python gradient descent algorithm
Python implements Super Mario
Python implements tic-tac-toe game
Python object-oriented magic method
Python3.7 debugging example method
Python implements tic-tac-toe game
Python implements man-machine gobang
Python implements Tetris game
Python function-dictionary get() method
Python implements minesweeper game
Python implements scanning tools
Python error handling method
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python defines a function method
Python implements simple tank battle
Python implements udp chat window
Python TCP packet injection method
Python implements WeChat airplane game
Python implements word guessing game
Python implements a guessing game
Python drawing ring graph method
Python implements digital bomb game
Python implements TCP file transfer
Python numpy implements rolling case
Python implements simple tic-tac-toe game
Python implements password strength verification
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements text version minesweeper
Python right alignment example method
Python method of parameter passing
Python implements image stitching function
Python arithmetic sequence calculation method
Python implements the brick-and-mortar game
Method steps to increase python font
Summary of logarithm method in Python
Python and js interactive call method
Python implements student performance evaluation system
How Python implements the mail function
Python simply implements the snake game
Python3 implements the singleton design pattern
Python implements exchange rate conversion operations
Python implements string and number splicing
Python implements ten classic sorting algorithms
Python implements a universal web framework
Python implements 126 mailboxes to send mail
Python implements AI face change function
Python implements the actual banking system
Python implements digital bomb game program
Python implements ftp file transfer function
Python implements username and password verification