使用梯度下降优化方法,编程实现 Logistic Regression 算法

#梯度下降法
import pandas as pd
from numpy import dot
from sklearn import datasets
import numpy as np
from matplotlib import pyplot as plt
# w未知参数
from sklearn.preprocessing import StandardScaler

plt.rcParams['font.sans-serif'] = ['SimHei']

def logit(x):

  return 1./(1+np.exp(-x))

def loss_function(w,x,y):
    r_y = dot(y.T,logit(x.dot(w)))+dot((1-y).T,logit(1-x.dot(w)))
    return r_y.T


def get_grad(w,x,y):

    grad = (logit(x.dot(w))-y).T.dot(x)
    return grad.T


def gradient_function(w0,x,y,theata):
    k = 0
    grad = get_grad(w0,x,y)
    list=[]
    w1 = np.random.random((x.shape[1],1))

    while k<50000:
            w0 = w1
            grad = get_grad(w0,x,y)
            w1 = w0 - theata * grad
            list.append(loss_function(w1,x,y)[0])
            k+=1
    return w1,loss_function(w1,x,y),k,list


#
# 数据采用网络上的数据
# 需要归一化处理
# 获取所需数据集
iris=datasets.load_iris()
#每行的数据,一共四列,每一列映射为feature_names中对应的值
X=iris.data
#每行数据对应的分类结果值(也就是每行数据的label值),取值为[0,1,2]
Y = iris.target
Y = iris.target
X = X[np.where(Y<2),:].reshape(-1,4)
y = Y[np.where(Y<2)].reshape(-1,1)


#归一化处理
x = StandardScaler().fit_transform(X)

x = np.hstack((x, np.ones((x.shape[0], 1))))
theata=0.0001

w0 = np.random.random((x.shape[1],1))


w,loss,k,list=gradient_function(w0, x, y, theata)
#
print("参数",w)
print("损失函数值:",loss)
print("迭代次数:",k)


r_y = logit(dot(x, w))


plt.subplot(2,1,1)
plt.plot(y, color="red", marker="o", label="实际")
plt.plot(r_y, color="blue", marker=".", label="预测")
plt.xlabel("Sample", fontsize=14)
plt.ylabel("y", fontsize=14)
plt.subplot(2,1,2)
plt.plot(list, color="blue", marker=".", label="损失值")
plt.legend()
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值