纯python代码实现逻辑回归,不调机器学习第三方库,仅调用numpy实现矩阵向量计算和matplotlib实现画图
import numpy as np
import matplotlib.pyplot as plt
# sigmod函数,即分类的函数
def sigmod(x):
return 1/(1+np.exp(-x))
# 代价函数
def cost(hx, y):
return -y * np.log(hx) - (1-y) * np.log(1-hx)
# 梯度下降函数
def gradient(current_para, x, y, learning_rate):
m = len(y)
matrix_gradient = np.zeros(len(x[0]))
for i in range(m):
current_x = x[i]
current_y = y[i]
current_x = np.asarray(current_x)
matrix_gradient += (sigmod(np.dot(current_para, current_x)) - current_y) * current_x
new_para = current_para - learning_rate * matrix_gradient
return new_para
# 误差计算函数
def error(para, x, y):
total = len(y)
error_num = 0
for i in range(total):
current_x = x[i]
current_y = y[i]
hx = sigmod(np.dot(para, current_x))
if cost(hx, current_y) > 0.5:
error_num += 1
return error_num/total