1. The key knowledge points in this section are summed up in your own words, can be accompanied by pictures, and explain the importance of the knowledge points
1. The regression algorithm belongs to supervised learning, and linear regression belongs to one of the knowledge points.
2. Linear regression is a linear relationship formed by a combination of multiple independent and dependent variables.
3. A linear regression algorithm is used in statistical learning, which uses the error score / least square method. At the same time, if the error is to be minimized, the normal equation / gradient descent is used.
4. Can help us predict some data, make certain judgments on this, and reduce errors
Learning code in this section:
import random import time import matplotlib.pyplot as plt # 梯度下降 xs = [0.1*x for x in range(0,10)] ys = [12*i+4 for i in xs] print(xs) print(ys) w = random.random() b = random.random() a1=[] b1=[] for i in range(10): #(1) for x, y in zip (xs, ys): o = w * x + b #predicted value e = (o- y) #error loss = e ** 2 dw = 2 * e * x #derivative db = 2 * e * 1 w = w- 0.1 * dw # 0 .1 is the learning rate, this must be calculated b = b- 0.1 * db print ( ' loss = {0}, w = {1}, b = {2} ' .format (loss, w, b)) a1.append (i) b1.append (loss) plt.plot(a1,b1) plt.pause(0.1) plt.show()
2. Thinking about what linear regression algorithms can be used for? (Everyone try not to write duplicates)
1. Finance, to analyze the relationship between the maturity structure of corporate debt and the marketization degree of the region
2. Teaching prediction, predict and analyze the current course performance based on the students' learning foundation, and improve teaching methods
3. Epidemiology, early evidence on the impact of smoking on mortality and morbidity
3. Write a linear regression algorithm independently, the data can be made by yourself, or obtained from the Internet. (Plus points)
Using Linear Regression Algorithm to Forecast Boston House Price
Code:
from sklearn.datasets import load_boston from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt data=load_boston() data_all = data['data'] x=data_all[:,5:6] #x[0] array([6.575]) numpy.ndarray type(x[0]) x[0] type(x[0]) y=data['target'] #y[0] 24.0 numpy.float64 y [ 0 ] type (y [ 0 ]) model_LR = LinearRegression () model_LR.fit (x, y) print ( ' weight of model: ' , model_LR.coef_, ' intercept term: ' , model_LR .intercept_) pre = model_LR.predict (x) #Use visual methods to compare the fitted linear regression equation with the distribution of real house prices plt.scatter (x, y) #The distribution of real house prices , c = ' r ' ) #fitted linear regression equation plt.legend ([ ' real ' , ' pre ' ]) plt.show ()
Screenshot: