首页 > 其他分享 >线性回归与Logistic回归(代码实现)

线性回归与Logistic回归(代码实现)

时间:2024-04-05 21:36:00浏览次数:16  
标签:回归 torch np train Logistic 线性 model numpy self

线性回归

一维线性回归

最小二乘法,偏导数为0

import torch
from torch.autograd import Variable
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torch.optim as optim

x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168], [9.779], 						[6.182], [7.59], [2.167], [7.042],
                    [10.791], [5.313], [7.997], [3.1]], dtype=np.float32)
y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573], 						[3.366], [2.596], [2.53], [1.221],
                    [2.827], [3.465], [1.65], [2.904],										[1.3]],dtype=np.float32)

x_train = torch.from_numpy(x_train)
y_train = torch.from_numpy(y_train)

class LinearRegression(nn.Module):
    def __init__(self):
        super(LinearRegression, self).__init__()
        self.linear = nn.Linear(1, 1)

    def forward(self, x):
        assert isinstance(x, object)
        out = self.linear(x)
        return out
if torch.cuda.is_available():
    model = LinearRegression().cuda()
else:
    model = LinearRegression()
    
    
# 定义损失函数和优化函数,使用均方误差作为优化函数,使用梯度下降进行优化

criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# 开始训练模型
num_epochs = 1000
for epoch in range(num_epochs):
    inputs = Variable(x_train)
    target = Variable(y_train)

    # forward
    out = model(inputs)
    loss = criterion(out, target)
    # backward
    optimizer.zero_grad() # 归零梯度 
    loss.backward()
    optimizer.step()

    if (epoch+1) % 20 == 0:
        print('Epoch[{}/{}],loss:{:.6f}'.format(epoch+1, num_epochs, loss.item()))

if __name__ == '__main__':
    model.eval()
    predict = model(Variable(x_train))
    predict = predict.data.numpy()
    plt.plot(x_train.numpy(), y_train.numpy(), 'ro', label='Original data')
    plt.plot(x_train.numpy(), predict, label='Fitting Line')
    plt.show()

多维线性回归

import torch
import numpy as np
import matplotlib.pyplot as plt
from torch import nn

# -------------------------------------数据准备--------------------------------------
# 目标权重和偏置
w = torch.FloatTensor([2.0, 3.0, 4.0]).unsqueeze(1)
b = torch.FloatTensor([0.5])


# 一次生成32个数据
def create_data(batch_size=32):
    random = torch.randn(batch_size)
    random = random.unsqueeze(1)  # 添加一个维度
    # 纵向连接tensor
    x = torch.cat([random ** i for i in range(1, 4)], 1)  # b/x/^2/x^3
    # 矩阵乘法
    y = x.mm(w) + b[0]  # mm表示矩阵相乘,mul为对应元素相乘
    if torch.cuda.is_available():
        return x.cuda(), y.cuda()
    return x, y


# -------------------------------------自定义模型--------------------------------------
class PloyRegression(nn.Module):
    def __init__(self):
        super(PloyRegression, self).__init__()
        self.ploy = nn.Linear(3, 1)  # 输入3维(分别表示x/x^2/x^3),输出1维

    def forward(self, x):
        out = self.ploy(x)
        return out


model = PloyRegression()
if torch.cuda.is_available():
    model = model.cuda()

# ------------------------损失函数、优化器的选择----------------------------
criterion = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)

# ------------------------开始训练----------------------------
# 使用均方误差,随机梯度下降
epoch = 0
while True:
    # 创建数据
    batch_x, batch_y = create_data()  # 一次生成32个数据
    # 前向传播
    output = model(batch_x)
    # 损失计算
    loss = criterion(output, batch_y)
    # 获取损失值
    loss_value = loss.data.cpu().numpy()
    # 梯度置零
    optimizer.zero_grad()
    # 反向传播
    loss.backward()
    # 更新参数
    optimizer.step()

    epoch += 1
    # 损失函数小于一定的值才会退出来
    if loss_value < 1e-3:
        break
    # 每100步打印一次损失
    if (epoch + 1) % 100 == 0:
        print("Epoch{}, loss:{:.6f}".format(epoch + 1, loss_value))

# -------------------------------------测试--------------------------------------
model.eval()  # 开启验证模式

# 构造数据
x_train = np.array([[i] for i in range(20)], dtype=np.float32)
x_train = torch.from_numpy(x_train)

x = torch.cat([x_train ** i for i in range(1, 4)], 1)
y = x.mm(w) + b
# 绘制数据点
plt.plot(x_train.numpy(), y.numpy(), 'ro')
# 提取拟合参数
w_get = model.ploy.weight.data.T
b_get = model.ploy.bias.data
print('w:{},b:{}'.format(w_get.cpu().numpy(), b_get.cpu().numpy()))
# 计算预测值
Y_get = x.mm(w_get.cpu()) + b_get.cpu()
plt.plot(x_train.numpy(), Y_get.numpy(), '-')
plt.show()

分类问题

二分类算法———Logistic

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# 设置随机种子
seed_value = 2024
np.random.seed(seed_value)


# Sigmoid激活函数
def sigmoid(z):
    return 1 / (1 + np.exp(-z))


# 定义逻辑回归算法
class LogisticRegression:
    def __init__(self, learning_rate=0.003, iterations=100):
        self.learning_rate = learning_rate  # 学习率
        self.iterations = iterations  # 迭代次数

    def fit(self, X, y):
        # 初始化参数
        self.weights = np.random.randn(X.shape[1])
        self.bias = 0

        # 梯度下降
        for i in range(self.iterations):
            # 计算sigmoid函数的预测值, y_hat = w * x + b
            y_hat = sigmoid(np.dot(X, self.weights) + self.bias)

            # 计算损失函数
            loss = (-1 / len(X)) * np.sum(y * np.log(y_hat) + (1 - y) * np.log(1 - y_hat))

            # 计算梯度
            dw = (1 / len(X)) * np.dot(X.T, (y_hat - y))
            db = (1 / len(X)) * np.sum(y_hat - y)

            # 更新参数
            self.weights -= self.learning_rate * dw
            self.bias -= self.learning_rate * db

            # 打印损失函数值
            if i % 10 == 0:
                print(f"Loss after iteration {i}: {loss}")

    # 预测
    def predict(self, X):
        y_hat = sigmoid(np.dot(X, self.weights) + self.bias)
        y_hat[y_hat >= 0.5] = 1
        y_hat[y_hat < 0.5] = 0
        return y_hat

    # 精度
    def score(self, y_pred, y):
        accuracy = (y_pred == y).sum() / len(y)
        return accuracy


# 导入数据
iris = load_iris()
X = iris.data[:, :2]
y = (iris.target != 0) * 1

# 划分训练集、测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=seed_value)

# 训练模型
model = LogisticRegression(learning_rate=0.03, iterations=1000)
model.fit(X_train, y_train)

# 结果
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)

score_train = model.score(y_train_pred, y_train)
score_test = model.score(y_test_pred, y_test)

print('训练集Accuracy: ', score_train)
print('测试集Accuracy: ', score_test)

# 可视化决策边界
x1_min, x1_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
x2_min, x2_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max, 100), np.linspace(x2_min, x2_max, 100))
Z = model.predict(np.c_[xx1.ravel(), xx2.ravel()])
Z = Z.reshape(xx1.shape)
plt.contourf(xx1, xx2, Z, cmap=plt.cm.Spectral)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
plt.xlabel("Sepal length")
plt.ylabel("Sepal width")
plt.show()

标签:回归,torch,np,train,Logistic,线性,model,numpy,self
From: https://www.cnblogs.com/0214jx/p/18116227

相关文章

  • 人工智能基础概念5:使用L1范数惩罚进行Lasso回归(正则化)解决机器学习线性回归模型幻觉和
    一、引言在老猿CSDN的博文《人工智能基础概念3:模型陷阱、过拟合、模型幻觉》中介绍了通过L1或L2正则化来限制模型的复杂度来解决过拟合的问题,老猿当时并不了解这背后的原理,这2天通过查阅资料终于明白了相关知识,在此一L1正则化来分享一下相关原理。二、相关概念2.1、......
  • 线性表基本操作物理实现
    #include<stdio.h>//顺序表的初始化及插入操作实战#defineMaxSize50typedefintElemType;//为什么这样设计,一旦用这种方式下面写代码,方便后续顺序表存储的不是整形方便修改,统一修改typedefstruct{ElemTypedata[MaxSize];intlen;//顺序表长度}Sqlist;/......
  • 回归预测 | Matlab基于CPO-GPR基于冠豪猪算法优化高斯过程回归的多输入单输出回归预测
    回归预测|Matlab基于CPO-GPR基于冠豪猪算法优化高斯过程回归的多输入单输出回归预测目录回归预测|Matlab基于CPO-GPR基于冠豪猪算法优化高斯过程回归的多输入单输出回归预测预测效果基本介绍程序设计参考资料预测效果基本介绍Matlab基于CPO-GPR基于......
  • 基于蜜獾算法优化的核极限学习机(KELM)回归预测
    基于蜜獾算法优化的核极限学习机(KELM)回归预测文章目录基于蜜獾算法优化的核极限学习机(KELM)回归预测1.KELM理论基础2.回归问题数据处理4.基于蜜獾算法优化的KELM5.测试结果6.Matlab代码摘要:本文利用蜜獾算法对核极限学习机(KELM)进行优化,并用于回归预测.1.KEL......
  • 【基于LSTM的多输出回归预测】MATLAB代码分享
    文章目录前言一、LSTM的基本原理二、多输出回归预测模型架构三、示例代码1.读入数据并划分数据集2.运行结果总结前言`在当前的数据驱动时代,预测未来的趋势、需求、资源分配等成为了许多行业关键决策的基石。长短期记忆网络(LongShort-TermMemory,LSTM)作为一种特......
  • 信号与线性系统分析第一章总结
    参考资料:《信号与线性系统分析》第五版吴大正著......
  • 超强来袭 基于卷积神经网络结合双向长短记忆网络CNN-BiLSTM实现风电功率多输入单输出
     ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,代码获取、论文复现及科研仿真合作可私信。......
  • 逻辑回归(Logistic Regression)详解
    逻辑回归是一种用于分类问题的机器学习算法,尤其是在二分类问题中应用广泛。它的名字虽然带有"回归",但实际上是一种分类算法。在本文中,我将详细解释逻辑回归的原理、方法和应用。1.逻辑回归的原理逻辑回归的原理基于统计学和概率论。其基本思想是通过对输入特征的线性组合......
  • 线性代数在AI中的应用
    线性代数在AI中的应用作者:禅与计算机程序设计艺术1.背景介绍人工智能(AI)作为当今技术发展的前沿领域,在近几年中迅速崛起,在各行各业都得到了广泛的应用。这其中,线性代数作为AI算法的基础数学工具,在AI模型的构建、训练和优化中发挥着关键作用。本文将深入探讨线性代......
  • R语言广义线性混合模型GLMMs在生态学中应用可视化2实例合集|附数据代码
    全文链接:https://tecdat.cn/?p=35607原文出处:拓端数据部落公众号在生态学研究领域,广义线性混合模型(GeneralizedLinearMixedModels,简称GLMMs)是一种强大的统计工具,能够同时处理固定效应和随机效应,从而更准确地揭示生态系统中复杂关系的本质。随着数据分析技术的不断发展,R语言......