机器学习基础——线性回归

线性回归简介

       线性回归可分为一元回归和多元回归,一元回归就是只有一个影响因子,也就是大家熟悉的线性方程,多元回归就是有多个影响因子。
一元线性回归方程是
在这里插入图片描述
简单来说就是二维平面上的一条直线;
多元线性回归方程
在这里插入图片描述
从数学角度如果只有两个影响因子可以看做是回归平面,
如果涉及到高维度不能用图形来描述。



可以用损失值来衡量回归
损失函数
在这里插入图片描述
要选择最优的θ,使得h(x)最近进真实值。这个问题就转化为求解最优的θ,使损失函数J(θ)取最小值。
1、梯度下降算法
      以单变量为例:
机器学习基础——线性回归_第1张图片
α是学习速率,决定梯度下降的步长,α越大,下降越快
2、正规方程(适用于简单线性回归,小数据集)
在这里插入图片描述
X是特征值矩阵,Y是目标值矩阵缺点:特征值过大,求解速度过慢

一元线性回归

1、正规方程

import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error  # 均方误差
import warnings
warnings.filterwarnings('ignore')

plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

x = np.array([i for i in range(1, 31)])
y = np.array([-0.39, -0.02, 3.1, -0.24, 2.21, 2.59, 5.64, 3.85, 8.3, 4.72, 10.47, 8.3, 
              7.3, 9.72, 15.29, 13.4, 16.3, 12.13, 16.68, 13.28, 14.45, 14.79, 16.43, 
              20.3, 19.17, 20.97, 18.41, 19.72, 20.96, 23.44])
lr = LinearRegression()
lr.fit(x.reshape(-1, 1), y.reshape(-1, 1))
y_predict = lr.predict(x.reshape(-1, 1))

mse = mean_squared_error(y_true=y, 
                   y_pred=y_predict)
# plt.figure(figsize=(10, 8))
plt.xlabel('X轴', fontsize=16)
plt.ylabel('Y轴', fontsize=16)
plt.scatter(x, y)
plt.plot(x, y_predict, c='r')
plt.text(2, 22, r'均方误差:{}'.format(mse), fontsize=14)    #文本中注释
plt.show()

机器学习基础——线性回归_第2张图片

2、梯度下降

import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression  # 正规方程
from sklearn.linear_model import SGDRegressor  # 梯度下降
from sklearn.metrics import mean_squared_error  # 均方误差
import warnings
warnings.filterwarnings('ignore')

plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

x = np.array([i for i in range(1, 31)])
y = np.array([-0.39, -0.02, 3.1, -0.24, 2.21, 2.59, 5.64, 3.85, 8.3, 4.72, 10.47, 8.3, 
              7.3, 9.72, 15.29, 13.4, 16.3, 12.13, 16.68, 13.28, 14.45, 14.79, 16.43, 
              20.3, 19.17, 20.97, 18.41, 19.72, 20.96, 23.44])

sgd = SGDRegressor()
sgd.fit(x.reshape(-1, 1), y)
y_predict = sgd.predict(x.reshape(-1, 1))

mse = mean_squared_error(y_true=y, 
                   y_pred=y_predict)
# plt.figure(figsize=(10, 8))
plt.xlabel('X轴', fontsize=16)
plt.ylabel('Y轴', fontsize=16)
plt.scatter(x, y)
plt.plot(x, y_predict, c='r')
plt.text(2, 22, r'均方误差:{}'.format(mse), fontsize=14)    #文本中注释
plt.show()

机器学习基础——线性回归_第3张图片

多元线性回归(方便画图以二元为例)

import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression  # 正规方程
from sklearn.linear_model import SGDRegressor  # 梯度下降
from sklearn.metrics import mean_squared_error  # 均方误差
from mpl_toolkits.mplot3d.axes3d import Axes3D
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')

plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

x = np.array([[2.03, -3.03], [4.14, 0.61], [7.09, 2.36], [-0.54, -0.52], [9.18, 8.77], [1.07, 3.56], [11.88, 2.73], [5.06, 10.17], [4.8, 9.3], [6.83, 13.21], [7.59, 9.78], [7.91, 8.72], [14.11, 11.6], [11.95, 14.35], [15.85, 17.4], [15.27, 20.36], [20.67, 14.51], [19.05, 18.45], [15.26, 16.45], [21.73, 20.06], [22.14, 23.28], [17.38, 20.42], [23.91, 21.65], [20.95, 23.83], [20.02, 25.06], [24.59, 28.03], [22.89, 27.57], [23.17, 32.27], [31.62, 28.8], [33.19, 31.64], [26.51, 34.46], [33.78, 36.62], [31.93, 37.84], [33.46, 29.63], [38.13, 31.3], [39.25, 39.45]])
y = np.array([4.73, 1.72, 5.8, 3.55, 14.42, 16.42, 13.03, 19.2, 15.73, 20.45, 19.43, 20.37, 24.46, 29.05, 27.63, 31.19, 36.32, 36.84, 42.8, 39.7, 45.89, 47.12, 49.39, 43.66, 54.14, 54.87, 52.43, 57.98, 58.26, 61.16, 63.7, 68.43, 66.51, 67.56, 71.93, 72.73])

lr = LinearRegression()
lr.fit(x, y)

y_predict = lr.predict(x)
mse = mean_squared_error(y_true=y, 
                   y_pred=y_predict)

# 系数
a = lr.coef_
# 截距
b = lr.intercept_

# 得到平面方程
# z = a*x + b

# 根据拟合平面画出平面方程
xx, yy = np.meshgrid(np.linspace(0,50, 5), np.linspace(0,50, 5))
zz = a[0] * xx + a[1] * yy + b

fig = plt.figure(figsize=(12, 8))
ax = plt.axes(projection='3d')
# 真实数据的散点图
ax.scatter(x[:,[0]], x[:,[1]], y, c='r')
# 拟合平面图
ax.plot_surface(xx, yy, zz, alpha=0.3)
plt.show()

机器学习基础——线性回归_第4张图片

你可能感兴趣的:(机器学习)