【机器学习Python实战】线性回归

简介: 【机器学习Python实战】线性回归

一元线性回归

  • 设计思路

首先,class LinearRegression(object):定义一个LinearRegression类,继承自object类。

在这个类中,首先def __init__(self):定义类的构造函数

在构造函数中,初始化线性回归模型的参数self.__Mself.__theta0self.__theta1,以及梯度下降中的步长(学习率)self.__alpha

线性回归模型是要不断计算输出的,所以定义函数def predict(self, x),用于预测给定输入x对应的输出

同时线性回归的目的是通过迭代,不断的修改参数θ \thetaθ,所以需要定义函数用来做这个工作,它是通过梯度下降的方法来求解的,所以def __cost_theta0(self, X, Y)def __cost_theta1(self, X, Y)这两个方法用于计算代价函数关于参数θ 0 \theta_0θ0θ 1 \theta_1θ1的偏导数

下面,def train(self, features, target)把上面的每个步骤和到了一起,定义了一个训练方法train,用于通过梯度下降算法找到最优的模型参数θ 0 \theta_0θ0θ 1 \theta_1θ1,使得代价函数的平方误差最小。在训练过程中,通过迭代更新参数,并输出每次迭代后的参数值

while的每一次迭代中,通过更新参数self.__theta0self.__theta1来逐渐最小化代价函数的平方误差。

if "0:o.5f".format(prevt0) == "0:o.5f".format(self.__theta0) and "0:o.5f".format(prevt1) == "0:o.5f".format(self.__theta1):判断是否达到收敛条件,即两次迭代的参数值没有改变,如果满足条件,则退出循环。

最后,输出最终得到的参数值。

  • 总体代码实现

定义LinearRegression的class

#!/usr/bin/env python3
# 这是一个Shebang,指定了此脚本要使用的解释器为python3。
import numpy
class LinearRegression(object):
    # Constructor. Initailize Constants.
    def __init__(self):
        super(LinearRegression, self).__init__()
        self.__M = 0
        self.__theta0 = 2
        self.__theta1 = 2
        # defining Alpha I.E length of steps in gradient descent Or learning Rate.
        self.__alpha = 0.01
    def predict(self,x):
        return (self.__theta0 + x * self.__theta1)
    # Cost Function fot theta0.
    def __cost_theta0(self,X,Y):
        sqrerror = 0.0
        for i in range(0,X.__len__()):
            sqrerror += (self.predict(X[i]) - Y[i])
        return (1/self.__M * sqrerror)
    # Cost Function fot theta1.
    def __cost_theta1(self,X,Y):
        sqrerror = 0.0
        for i in range(0,X.__len__()):
            sqrerror += (self.predict(X[i]) - Y[i]) * X[i]
        return (1/self.__M * sqrerror)
    # training Data :
    # Finding Best __theta0 and __theta1 for data such that the Squared  Error is Minimized.
    def train(self,features,target):
        # Validate Data
        if not features.__len__() == target.__len__():
            raise Exception("features and target should be of same length")
        # Initailize M with Size of X and Y
        self.__M = features.__len__()
        # gradient descent
        prevt0, prevt1 = self.__theta0 , self.__theta1
        while True:
            tmp0 = self.__theta0 - self.__alpha * (self.__cost_theta0(features,target))
            tmp1 = self.__theta1 - self.__alpha * (self.__cost_theta1(features,target))
            self.__theta0, self.__theta1 = tmp0, tmp1
            print("theta0(b) :", self.__theta0)
            print("theta1(m) :", self.__theta1)
            if "0:o.5f".format(prevt0) == "0:o.5f".format(self.__theta0) and "0:o.5f".format(prevt1) == "0:o.5f".format(self.__theta1):
                break
            prevt0, prevt1 = self.__theta0 , self.__theta1
        # Training Completed.
        # log __theta0 __theta1
        print("theta0(b) :", self.__theta0)
        print("theta1(m) :", self.__theta1)

样例测试

from LinearRegression_OneVariables import LinearRegression
import numpy as np
X = np.array([1,2,3,4,5,6,7,8,9,10])
# Y = 0 + 1X
Y = np.array([1,2,3,4,5,6,7,8,9,10])
modal = LinearRegression.LinearRegression()
modal.train(X,Y)
print(modal.predict(14))

多元线性回归

  • 设计思路

首先,将文件导入,打乱顺序并选择训练集。

data=pd.read_csv("c:\\windquality.csv")
data_array=data.values
#shuffling for train test spplit
np.random.shuffle(data_array)
train,test=data_array[:1200,:],data_array[1200:,:]
x_train=train[:,:-1]
x_test=test[:,:-1]
y_train=train[:,-1]
y_test=test[:,-1]

然后初始化参数,注意这里是多元的,所以有多个参数需要初始化。包括迭代次数和学习率

coef1=0
coef2=0
coef3=0
coef4=0
coef5=0
coef6=0
coef7=0
coef8=0
coef9=0
coef10=0
coef11=0
epoch=1000
alpha=.0001

然后使用梯度下降算法进行计算

总体代码实现

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data=pd.read_csv("c:\\windquality.csv")
data_array=data.values
#shuffling for train test spplit
np.random.shuffle(data_array)
train,test=data_array[:1200,:],data_array[1200:,:]
x_train=train[:,:-1]
x_test=test[:,:-1]
y_train=train[:,-1]
y_test=test[:,-1]
coef1=0
coef2=0
coef3=0
coef4=0
coef5=0
coef6=0
coef7=0
coef8=0
coef9=0
coef10=0
coef11=0
epoch=1000
alpha=.0001
c=0
n=len(y_train)
for i in range(epoch):
    y_pred=((coef1*x_train[:,0])+(coef2*x_train[:,1])+(coef3*x_train[:,2])+(coef4*x_train[:,3])+
            (coef5*x_train[:,4])+(coef6*x_train[:,5])+(coef7*x_train[:,6])+(coef8*x_train[:,7])+
            (coef9*x_train[:,8])+(coef10*x_train[:,9])+(coef11*x_train[:,10])+c)
    #to predict drivative
    intercept=(-1/n)*sum(y_train-y_pred)
    dev1=(-1/n)*sum(x_train[:,0]*(y_train-y_pred))
    dev2=(-1/n)*sum(x_train[:,1]*(y_train-y_pred))
    dev3=(-1/n)*sum(x_train[:,2]*(y_train-y_pred))
    dev4=(-1/n)*sum(x_train[:,3]*(y_train-y_pred))
    dev5=(-1/n)*sum(x_train[:,4]*(y_train-y_pred))
    dev6=(-1/n)*sum(x_train[:,5]*(y_train-y_pred))
    dev7=(-1/n)*sum(x_train[:,6]*(y_train-y_pred))
    dev8=(-1/n)*sum(x_train[:,7]*(y_train-y_pred))
    dev9=(-1/n)*sum(x_train[:,8]*(y_train-y_pred))
    dev10=-1/n*sum(x_train[:,9]*(y_train-y_pred))
    dev11=-1/n*sum(x_train[:,10]*(y_train-y_pred))
    #line
    c=c-alpha*intercept
    coef1=coef1-alpha*dev1
    coef2=coef2-alpha*dev2
    coef3=coef3-alpha*dev3
    coef4=coef4-alpha*dev4
    coef5=coef5-alpha*dev5
    coef6=coef6-alpha*dev6
    coef7=coef7-alpha*dev7
    coef8=coef8-alpha*dev8
    coef9=coef9-alpha*dev9
    coef10=coef10-alpha*dev10
    coef11=coef11-alpha*dev11
print("\nintercept:",c,"\ncoefficient1:",coef1,"\ncoefficient2:",coef2,"\ncoefficient3:",coef3,"\ncoefficient4:",coef4,
      "\ncoefficient5:",coef5,"\ncoefficient6:",coef6,"\ncoefficient7:",coef7,"\ncoefficient8:",coef8,"\ncoefficient9:",coef9,
      "\ncoefficient10",coef10,   "\ncoefficient11",coef11)
#Calculating the predicted values
predicted_values = []
for i in range(0,399):
    y_pred = ((coef1 * x_test[i,0]) + (coef2 * x_test[i,1]) + 
              (coef3 * x_test[i,2]) + (coef4 * x_test[i,3]) + 
              (coef5 * x_test[i,4]) + (coef6 * x_test[i,5]) + 
              (coef7 * x_test[i,6]) + (coef8 * x_test[i,7]) + 
              (coef9 * x_test[i,8]) + (coef10 * x_test[i,9]) + 
              (coef11 * x_test[i,10]) + intercept)
    predicted_values.append(y_pred)
for i in range(len(predicted_values)):
    print(predicted_values[i])


相关文章
|
19天前
|
数据采集 数据可视化 数据挖掘
Python数据分析实战:Pandas处理结构化数据的核心技巧
在数据驱动时代,结构化数据是分析决策的基础。Python的Pandas库凭借其高效的数据结构和丰富的功能,成为处理结构化数据的利器。本文通过真实场景和代码示例,讲解Pandas的核心操作,包括数据加载、清洗、转换、分析与性能优化,帮助你从数据中提取有价值的洞察,提升数据处理效率。
89 3
|
19天前
|
数据可视化 Linux iOS开发
Python脚本转EXE文件实战指南:从原理到操作全解析
本教程详解如何将Python脚本打包为EXE文件,涵盖PyInstaller、auto-py-to-exe和cx_Freeze三种工具,包含实战案例与常见问题解决方案,助你轻松发布独立运行的Python程序。
223 2
|
19天前
|
存储 监控 API
Python实战:跨平台电商数据聚合系统的技术实现
本文介绍如何通过标准化API调用协议,实现淘宝、京东、拼多多等电商平台的商品数据自动化采集、清洗与存储。内容涵盖技术架构设计、Python代码示例及高阶应用(如价格监控系统),提供可直接落地的技术方案,帮助开发者解决多平台数据同步难题。
|
24天前
|
机器学习/深度学习 算法 文件存储
神经架构搜索NAS详解:三种核心算法原理与Python实战代码
神经架构搜索(NAS)正被广泛应用于大模型及语言/视觉模型设计,如LangVision-LoRA-NAS、Jet-Nemotron等。本文回顾NAS核心技术,解析其自动化设计原理,探讨强化学习、进化算法与梯度方法的应用与差异,揭示NAS在大模型时代的潜力与挑战。
221 6
神经架构搜索NAS详解:三种核心算法原理与Python实战代码
|
6天前
|
机器学习/深度学习 文字识别 Java
Python实现PDF图片OCR识别:从原理到实战的全流程解析
本文详解2025年Python实现扫描PDF文本提取的四大OCR方案(Tesseract、EasyOCR、PaddleOCR、OCRmyPDF),涵盖环境配置、图像预处理、核心识别与性能优化,结合财务票据、古籍数字化等实战场景,助力高效构建自动化文档处理系统。
68 0
|
4天前
|
小程序 PHP 图形学
热门小游戏源码(Python+PHP)下载-微信小程序游戏源码Unity发实战指南​
本文详解如何结合Python、PHP与Unity开发并部署小游戏至微信小程序。涵盖技术选型、Pygame实战、PHP后端对接、Unity转换适配及性能优化,提供从原型到发布的完整指南,助力开发者快速上手并发布游戏。
|
6天前
|
JavaScript 前端开发 安全
【逆向】Python 调用 JS 代码实战:使用 pyexecjs 与 Node.js 无缝衔接
本文介绍了如何使用 Python 的轻量级库 `pyexecjs` 调用 JavaScript 代码,并结合 Node.js 实现完整的执行流程。内容涵盖环境搭建、基本使用、常见问题解决方案及爬虫逆向分析中的实战技巧,帮助开发者在 Python 中高效处理 JS 逻辑。
|
12天前
|
开发工具 Android开发 开发者
用Flet打造跨平台文本编辑器:从零到一的Python实战指南
本文介绍如何使用Flet框架开发一个跨平台、自动保存的文本编辑器,代码不足200行,兼具现代化UI与高效开发体验。
105 0
|
14天前
|
算法 安全 数据安全/隐私保护
Python随机数函数全解析:5个核心工具的实战指南
Python的random模块不仅包含基础的随机数生成函数,还提供了如randint()、choice()、shuffle()和sample()等实用工具,适用于游戏开发、密码学、统计模拟等多个领域。本文深入解析这些函数的用法、底层原理及最佳实践,帮助开发者高效利用随机数,提升代码质量与安全性。
69 0
|
21天前
|
设计模式 缓存 运维
Python装饰器实战场景解析:从原理到应用的10个经典案例
Python装饰器是函数式编程的精华,通过10个实战场景,从日志记录、权限验证到插件系统,全面解析其应用。掌握装饰器,让代码更优雅、灵活,提升开发效率。
83 0

热门文章

最新文章

推荐镜像

更多