yp = y-prediction
上一部分讲到,逻辑回归的公式
用代码复现:
from math import exp
def prediction(row,coefficients):
yp = coefficients[0]
for i in range(len(row)-1):
yp += coefficients[i+1]*row[i]
return 1/(1+exp(-yp))
#数据集的格式,X,Y,类别(0或1)
dataset = [[2, 3, 0],
[2, 1, 0],
[3, 2, 0],
[4, 3, 0],
[8, 1, 1],
[8.4, 3.2, 1],
[9, 1.5, 1],
[10, 3.8, 1]]
#系数手动配
coefficients = [-0.4,0.8,1]
for row in dataset:
yp = prediction(row,coefficients)
print("The actual value is %.3f,the predicted value is %.3f [%d] " % (row[-1],yp,round(yp)))
在手动设定系数的情况下
导入数据并运行:
得到以下结果