A simple linear regression algorithm based on Python

Time:2020-11-26

Recently, I listened to Mr. Zhang Jiang’s deep learning course and used Python to realize neural network prediction. I had a little knowledge of tensorflow when I did the prediction of Titanic survival rate. I heard that the pyrorch that tensorflow can do can be done, and it is more convenient and fast. I tried the logic of the code, which is really simple.

The basic data types involved in pytoch are tensor (tensor) and autograd (automatic differential variable). I know a little about these concepts. Tensor and vector, matrix and other concepts have overlapping parts. Next time, I have time to make up the basic knowledge of mathematics. However, the task at this stage is mainly application. It is enough to learn and master the thinking and methods, so I won’t go into it. Tensor and ndarray can be converted to each other, and the commands in Python’s numpy library are basically applicable.

Some basic codes:

Import torch ා import torch package
x =  torch.rand (5,3) ා generates a 5 * 3 tensor, which is randomly selected between [0,1]
y =  torch.ones (5, 3) ා produces a 5 * 3 tensor with elements 1 
#Same as numpy's command

#The operation of tensor
Z = x + y # two tensors can be added directly
Q = x.mm (y.transfer (0,1)) ා x multiplied by the transposition mm of Y is the matrix multiplication. The matrix multiplication must have the row of one matrix equal to the column of another matrix

##Tensor and numpy.ndarray Conversion between
Import numpy as NP ා import numpy package
a =  np.ones ([5,3]) to establish a two-dimensional array (matrix) with 5 * 3 all 1
b =  torch.from_ Numpy (a) ා using from_ Numpy converts it to tensor
c =  torch.FloatTensor (a) Another method of converting to tensor is flotatensor, which can also make longtensor, integer data type
b. Numpy() ා converts from a tensor to a multidimensional array of numpy

from  torch.autograd  Import variable ා import automatic gradient operation package, mainly using variable
x = Variable( torch.ones (2, 2), requires_ Grad = true) ා create a variable, wrap a 2 * 2 tensor, and set the gradient property to true

Let’s use Python to make a simple linear relationship prediction

Linear relationship is a very simple relationship between variables. When dependent variables and independent variables are online, linear regression algorithm can be used to model the linear relationship between one or more dependent variables and independent variables. The coefficients of the model can be solved by least square method. The scenes in our life are often complex. We need to consider the multiple linear and nonlinear relations, and use other regression analysis methods to solve the problem.

Code on Po:

#Some sample points are artificially generated as raw data
x = Variable(torch.linspace(0, 100).type(torch.FloatTensor)) 
rand = Variable( torch.randn (100)) * 10 ා randomly generate 100 random numbers meeting the standard normal distribution, with the mean value of 0 and the variance of 1. Multiply this number by 10, and the standard deviation becomes 10
Y = x + Rand ා add X and Rand to get forged label data y. So this line y = y should fall approximately on the ground

import  matplotlib.pyplot  As PLT ා import drawing package
plt.figure (figsize = (10,8)) ා set the drawing window size to 10 * 8 inch
plt.plot (X. data.numpy (), y. data.numpy (),'O ') ා draw data. Considering that X and y are variables, we need to use data to get tensors wrapped in them and make them numpy
plt.xlabel('X') 
plt.ylabel('Y') 
plt.show()

Figure:

Training model:

#a. B is the coefficient of the linear function to be constructed
a = Variable( torch.rand (1), requires_ Grad = true) ා create a variable and initialize it by random assignment
b = Variable( torch.rand (1), requires_ Grad = true)
print('Initial parameters:', [a, b])

learning_ Rate = 0.0001 ා set learning rate
for i in range(1000):
  ###This part of code is added to clear the gradient information stored in variables A and B, so as to avoid repeated accumulation in backward process
  if (a.grad is not None) and (b.grad is not None): 
    a.grad.data.zero_() 
    b.grad.data.zero_() 
  predictions = a.expand_ as(x) * x+ b.expand_ As (x) ා calculates the predicted values of the model under the current a and B conditions
  loss =  torch.mean ((predictions - y) * * 2) # the error was calculated by comparing with the label data y
  print('loss:', loss)

  loss.backward () ාාාාාාාාgradient back propagation, backward direction propagation algorithm
  A. data.add_ (- learning_ rate * a. grad.data )Update the data value in a by using the gradient information of a obtained in the previous step
  B. data.add_ (- learning_ rate * b. grad.data )Update the data value in B by using the gradient information of B obtained in the previous step

##Fitting
x_data = x.data.numpy() 
plt.figure(figsize = (10, 7))
xplot =  plt.plot (x_ data, y. data.numpy (),'O '), plot raw data
yplot =  plt.plot (x_ data, a. data.numpy () * x_ data + b. data.numpy ()) ා draw the fitting data
plt.xlabel('X') 
plt.ylabel('Y') 
str1 = str(a. data.numpy ()[0]) + 'x +' + str(b. data.numpy () [0]) legend information
plt.legend ([xplot, yplot], ['data ', STR1]) draws a legend
plt.show()

Figure:

Test:

X_ test = Variable( torch.FloatTensor ([1, 2, 10, 100, 1000])) ා select some points randomly 1, 2 ,1000
predictions = a.expand_ as(x_ test) * x_ test + b.expand_ as(x_ The prediction results of the calculation model
Predictions ා output

OK, it’s finished. You can see that using Python to do machine learning has advantages in both accuracy and convenience. Continue to explore and learn.

The simple linear regression algorithm realized by using Python is the whole content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developpaer more.