Pytorch实现Logistic回归

Pytorch实现Logistic回归

1.导入相关api

import torch
import torch.nn as nn
import numpy as np
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

2.准备数据

bc = datasets.load_breast_cancer()
X, y = bc.data, bc.target

n_samples, n_features = X.shape#获取样本数量以及样本特征数量

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1234)#设置测试集为20%

3.数据预处理

3.1 特征缩放

# 必须先用fit_transform(trainData),之后再transform(testData)
# 如果直接transform(testData),程序会报错
# 如果fit_transfrom(trainData)后,使用fit_transform(testData)而不transform(testData),虽然也能归一化,但是两个结果不是在同一个“标准”下的,具有明显差异。(一定要避免这种情况)
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

3.2 数据类型转换

X_train = torch.from_numpy(X_train.astype(np.float32))
X_test = torch.from_numpy(X_test.astype(np.float32))
y_train = torch.from_numpy(y_train.astype(np.float32))
y_test = torch.from_numpy(y_test.astype(np.float32))

3.3 数据格式调整

tensor.view()类似于array.resize(),此处是将标签行调整为1列

#类似于resize()
y_train = y_train.view(y_train.shape[0], 1)
y_test = y_test.view(y_test.shape[0], 1)

4.搭建模型

4.1定义基本网络模型

class Model(nn.Module):
    def __init__(self, n_input_features):
        super(Model, self).__init__()#用于初始化nn.module
        self.linear = nn.Linear(n_input_features, 1)#定义一个线性感知机,输入为n_input_features,输出为单个值

    def forward(self, x):
        y_pred = torch.sigmoid(self.linear(x))#使用sigmoid函数输出0~1的一个值
        return y_pred

model = Model(n_features)

4.2定义损失和优化器

num_epochs = 100
lr = 0.01
criterion = nn.BCELoss()#因为此处是二分类,所以使用Binary Cross Entropy
optimizer = torch.optim.SGD(model.parameters(), lr=lr)#lr为学习率,超参数

5.模型训练

for epoch in range(num_epochs):
    #前向迭代并计算误差
    y_pred = model(X_train)
    loss = criterion(y_pred, y_train)

    #反向迭代、更新参数
    loss.backward()
    optimizer.step()

    # 在一次更新参数前清空梯度
    optimizer.zero_grad()

    if (epoch+1) % 10 == 0:
        print(f'epoch: {epoch+1}, loss = {loss.item():.4f}')

6.模型测试

with torch.no_grad():
    y_predicted = model(X_test)
    y_predicted_cls = y_predicted.round()#对0~1之间的结果进行四舍五入,得到分类值
    acc = y_predicted_cls.eq(y_test).sum() / float(y_test.shape[0])
    print(f'accuracy: {acc.item():.4f}')

y_predicted_cls.eq(y_test)当预测类别等于测试数据的类别时为1,并将所有正确分类的次数进行统计,计算测试集中的准确率。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Miracle Fan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值