欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python实现逻辑回归的示例

程序员文章站 2022-04-06 19:49:42
代码import numpy as npimport matplotlib.pyplot as pltfrom sklearn.datasets.samples_generator import ma...

代码

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_classification


def initialize_params(dims):
  w = np.zeros((dims, 1))
  b = 0
  return w, b

def sigmoid(x):
  z = 1 / (1 + np.exp(-x))
  return z

def logistic(x, y, w, b):
  num_train = x.shape[0]
  y_hat = sigmoid(np.dot(x, w) + b)
  loss = -1 / num_train * np.sum(y * np.log(y_hat) + (1-y) * np.log(1-y_hat))
  cost = -1 / num_train * np.sum(y * np.log(y_hat) + (1 - y) * np.log(1 - y_hat))
  dw = np.dot(x.t, (y_hat - y)) / num_train
  db = np.sum(y_hat - y) / num_train
  return y_hat, cost, dw, db


def linear_train(x, y, learning_rate, epochs):
  # 参数初始化
  w, b = initialize_params(x.shape[1])

  loss_list = []
  for i in range(epochs):
    # 计算当前的预测值、损失和梯度
    y_hat, loss, dw, db = logistic(x, y, w, b)
    loss_list.append(loss)

    # 基于梯度下降的参数更新
    w += -learning_rate * dw
    b += -learning_rate * db

    # 打印迭代次数和损失
    if i % 10000 == 0:
      print("epoch %d loss %f" % (i, loss))

    # 保存参数
    params = {
      'w': w,
      'b': b
    }

    # 保存梯度
    grads = {
      'dw': dw,
      'db': db
    }

  return loss_list, loss, params, grads

def predict(x, params):
  w = params['w']
  b = params['b']
  y_pred = sigmoid(np.dot(x, w) + b)
  return y_pred


if __name__ == "__main__":
  # 生成数据
  x, labels = make_classification(n_samples=100,
                  n_features=2,
                  n_informative=2,
                  n_redundant=0,
                  random_state=1,
                  n_clusters_per_class=2)
  print(x.shape)
  print(labels.shape)

  # 生成伪随机数
  rng = np.random.randomstate(2)
  x += 2 * rng.uniform(size=x.shape)

  # 划分训练集和测试集
  offset = int(x.shape[0] * 0.9)
  x_train, y_train = x[:offset], labels[:offset]
  x_test, y_test = x[offset:], labels[offset:]
  y_train = y_train.reshape((-1, 1))
  y_test = y_test.reshape((-1, 1))
  print('x_train=', x_train.shape)
  print('y_train=', y_train.shape)
  print('x_test=', x_test.shape)
  print('y_test=', y_test.shape)

  # 训练
  loss_list, loss, params, grads = linear_train(x_train, y_train, 0.01, 100000)
  print(params)

  # 预测
  y_pred = predict(x_test, params)
  print(y_pred[:10])

以上就是python实现逻辑回归的示例的详细内容,更多关于python 逻辑回归的资料请关注其它相关文章!