导包
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
数据
data, target = make_blobs(centers=2)
plt.scatter(data[:,0], data[:,1], c=target)
逻辑斯蒂函数
# 目标函数
def aim(X,theta):
return np.dot(X,theta.T)
# 逻辑斯蒂函数
def logistic(X,theta):
return 1/(1+np.exp(-aim(X,theta)))
损失函数
# 损失函数
def loss(y,X,theta):
p = logistic(X,theta)
return np.sum(np.multiply(y,np.log(p)) + np.multiply(1-y,np.log(1-p))) / -len(X)
损失导函数
# 损失导函数
def dloss(y,X,theta):
p = logistic(X,theta)
derivative = np.zeros(theta.shape)
for i in range(len(theta.ravel())):
temp = np.multiply((p-y).ravel(),X[:,i])
derivative[0,i] = np.sum(temp) / len(X)
return derivative
梯度下降法
# 梯度下降法
def gradient_descent(X,y,theta,learning_rate=0.1,gradient=1e-4,mmax=100000):
"""
X_C:自变量, y:函数, theta:b_w的初始值, learning_rate:学习力,gradient:梯度值,mmax:最大运行次数,防止死循环
"""
while mmax:
theta = theta - learning_rate * (dloss(y,X,theta))
mmax -= 1
if abs(loss(y,X,theta)) < gradient:
return theta
return theta
测试数据
X = np.hstack((np.ones(shape=(len(data),1)),data))
y = target.reshape(-1,1)
theta = np.array([0,np.random.randn()*0.01,np.random.randn()*0.01]).reshape(1,3)
X.shape,y.shape,theta.shape
求出theta(θ)最小值
θ = gradient_descent(X,y,theta)
b,w1,w2= θ[0]
画图
plt.scatter(data[:,0], data[:,1], c=target)
x_test = np.linspace(4, 12, 100)
y = -w1/w2 * x_test - b/w2
plt.plot(x_test, y, c='r')