栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

线性回归(Linear Regression) Python实现

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

线性回归(Linear Regression) Python实现

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 0.绘制样本数据(观察一下)
def plot_sample(data):
    x = data[:,0] # 第一列数据 
    y = data[:,1] # 第二列数据
    plt.scatter(x,y)    
    plt.show()

# 1.计算cost function
def cost_function(data,theta0,theta1):
    x = data[:,0]
    y = data[:,1]
    m = data.shape[0] # 样本数据的数量
    cost = np.sum((theta0 + theta1 * x - y) ** 2) / (2 * m)
    return cost

# 2.梯度下降
def batch_gredient_descend(data,theta0,theta1,alpha):
    x = data[:,0]
    y = data[:,1]
    m = data.shape[0]
    theta0 = theta0 - alpha * np.sum(theta0 + theta1 * x - y) / m
    theta1 = theta1 - alpha * np.sum((theta0 + theta1 * x - y) * x) / m
    return theta0,theta1
    
# 3.多次迭代(梯度下降)
def multi_BGD(data,theta0,theta1,alpha,count,cost_list):
    for i in range(count):
        theta0,theta1 = batch_gredient_descend(data,theta0,theta1,alpha)
        if i % 100 == 0:
            # 每100次输出一下
            print(f'BGD {i}: θ0 = {theta0:.4f}    θ1 = {theta1:.4f}    cost = {cost_function(data,theta0,theta1):.4f}')
        cost = cost_function(data,theta0,theta1)    
        cost_list.append(cost) # 将每一次的cost记录下来,便于后续绘图观察有无收敛
    return theta0,theta1

# 4.绘制拟合曲线
def plot_linear(data,theta0,theta1):
    x = data[:,0]
    y = data[:,1]
    plt.title('Results Of Iterations')
    plt.xlabel('x'); plt.ylabel('y')
    plt.scatter(x,y)
    plt.plot(x,theta0+theta1*x,'r')
    plt.show()

def linear_regression():
    data = np.loadtxt('data.csv',dtype=np.float32,delimiter=',') # loadtxt读取csv文件
    plot_sample(data)
    # 参数初始化
    theta0 = theta1 = 10
    alpha = 0.01
    count = 10000 # 梯度下降1000次
    cost_list = []
    # 输出最开始的参数
    print(f'Begin para:θ0 = {theta0:.4f}    θ1 = {theta1:.4f}    alpha = {alpha:.4f}')
    theta0,theta1 = multi_BGD(data,theta0,theta1,alpha,count,cost_list)
    # 输出变化后的参数
    print(f'End para:θ0 = {theta0:.4f}    θ1 = {theta1:.4f}    alpha = {alpha:.4f}')
    plt.plot(cost_list) #绘制cost变化
    plt.show()
    plot_linear(data,theta0,theta1)
    
if __name__ == '__main__':
    linear_regression()

data.csv

1,3
1.2,3
1.2,4
1.5,4.5
1.6,4.3
6.5,12
3.6,7.1
2.5,9
5.7,14
6,11
9,17
8.9,17
7.1,15
7,14
2.5,4
0.8,2
0.5,2
3.4,7
3.6,9
5.6,12
6.7,15
6.9,15
7.1,14
7.5,17
7.8,16
8.1,15
8.3,15
8.5,15
8.7,16
8.7,17
8.8,18
8.8,20
8,16
9,19
9.2,18
10.1,20
1.1,3.2
1.6,4.2
4,9
12,25
9.5,20
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/979972.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号