收入即学习

2022-09-19

2022-09-19  本文已影响0人  扫地僧Andy

算法简介

image.png

线性回归基础

什么是线性?

什么是回归?

什么是回归分析?

线性函数

线性回归原理

线性回归

线性回归理论基础

线性回归模型

线性回归模型损失函数

线性回归模型训练

线性回归应用

线性回归的Python实现

import numpy as np
import matplotlib.pyplot as plt

x = np.array([[1, 5.56], [2, 5.70], [3, 5.91], [4, 6.40],[5, 6.80],
              [6, 7.05], [7, 8.90], [8, 8.70],[9, 9.00], [10, 9.05]])
m, n = np.shape(x)
x_data = np.ones((m, n))
x_data[:, :-1] = x[:, :-1]
y_data = x[:, -1]
m, n = np.shape(x_data)
theta = np.ones(n)

def gradientDescent(iter, x, y, w, alpha):
    x_train = x.transpose()
    for i in range(0, iter):
        pre = np.dot(x, w)
        loss = (pre - y)
        gradient = np.dot(x_train, loss) / m
        w = w - alpha * gradient
        cost = 1.0 / 2 * m * np.sum(np.square(np.dot(x, np.transpose(w)) - y))
        print("第{}次梯度下降损失为: {}".format(i,round(cost,2)))
    return w

result = gradientDescent(1000, x_data, y_data, theta, 0.01)
y_pre = np.dot(x_data, result)
print("线性回归模型 w: ", result)

plt.rc('font', family='Arial Unicode MS', size=14)
plt.scatter(x[:, 0], x[:, 1], color='b', label='训练数据')
plt.plot(x[:, 0], y_pre, color='r', label='预测数据')
plt.xlabel('x')
plt.ylabel('y')
plt.title('线性回归预测(梯度下降)')
plt.legend()
plt.show()

线性回归的Sklearn实现

# 数据集:Advertising
# 模型:Linear Regression Model
import matplotlib.pyplot as plt
%matplotlib inline
plt.rc('font', family='Arial Unicode MS', size=14)
import numpy as np
import pandas as pd
from sklearn import datasets, linear_model

# 数据处理
data = pd.read_csv('./data/Advertising.csv')
X = data[['TV', 'Radio', 'Newspaper']]
y = data[['Sales']]

# 划分训练集和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

# 求解线性模型参数
from sklearn.linear_model import LinearRegression
linreg = LinearRegression()
linreg.fit(X_train, y_train)
print(linreg.intercept_)  # 常数项 [2.87696662]
print(linreg.coef_)       # 变量系数 [[0.04656457 0.17915812 0.00345046]]

# 交叉验证
from sklearn.model_selection import cross_val_predict
from sklearn import metrics
predicted = cross_val_predict(linreg, X, y, cv=10)
print("MSE:",metrics.mean_squared_error(y, predicted))
print("RMSE:",np.sqrt(metrics.mean_squared_error(y, predicted)))

# 画图描述真实值和预测值的变化关系
fig, ax = plt.subplots()
ax.scatter(y, predicted)
ax.plot([y.min(), y.max()], [y.min(), y.max()], 'r--', lw=5)
ax.set_xlabel('真实值')
ax.set_ylabel('预测值')
plt.title('Advertising的真实值和预测值的变化关系')
plt.show()

线性回归的Sklearn实现

# TensorFlow 2.0 + Linear Regression

import tensorflow as tf
import numpy as np

x = np.float32(np.random.rand(100,1))

# y=a*x+b
y = np.dot(x,0.8) + 0.2

a = tf.Variable(np.float32())
b = tf.Variable(np.float32())

def model(x):
    return a*x+b

def loss(predicted_y, desired_y):
    return tf.reduce_sum(tf.square(predicted_y - desired_y))

optimizer = tf.optimizers.Adam(0.1)

for step in range(0, 100):
    with tf.GradientTape() as t:
        outputs = model(x)
        current_loss = loss(outputs, y)
        grads = t.gradient(current_loss, [a, b])
        optimizer.apply_gradients(zip(grads,[a, b]))
    if step % 10 == 0:
        print("Step:%d, loss:%2.5f, weight:%2.5f, bias:%2.5f "
              %(step, current_loss.numpy(), a.numpy(), b.numpy()))

线性回归总结

线性回归价值

线性回归优缺点

上一篇下一篇

猜你喜欢

热点阅读