版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/he_wen_jie/article/details/82598468
# -*- coding: utf-8 -*-
import random
import math
import cv2
import numpy as np
class Logstic:
def __init__(self,n_features=2,lr=1e-4):
self.lr = lr
self.n_features = n_features
self.weight = [random.random() for _ in range(self.n_features)]
self.bias = 0.0
def hypoth(self,x):
return -(self.bias+self.weight[0]*x)/self.weight[1]
def sigma(self,x):
x = sum(i*j for i,j in zip(self.weight,x)) + self.bias
return 1 / (1+math.exp(-x))
def fit(self,x,y):
err = y - self.sigma(x)
grad = map(lambda v:self.lr*err*v, x)
self.weight = map(lambda u,v:u+v,self.weight,grad)
self.bias += self.lr * err
def predict(self,x):
if self.sigma(x) > 0.5:
return 1
return 0
LR = Logstic()
w = 1000
h = 1000
train_x = []
train_y = []
mode = True
def draw_point(event, x, y, flags, param):
global mode
if event == cv2.EVENT_LBUTTONDOWN:
train_x.append([x*1.0/w,y*1.0/h])
if mode:
cv2.circle(img,(x,y),10,(255,0,0),-1)
train_y.append(0)
else:
cv2.rectangle(img,(x,y),(x+10,y+10),(0,255,0),-1)
train_y.append(1)
img = np.zeros((h,w,3),np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_point)
while(1):
cv2.imshow('image',img)
key = cv2.waitKey(20)&0xFF
if key ==27:#esc
break
elif key == ord('m'): # postivate sample or negivate sample
mode = not mode
elif key == ord('p'): # print predict res and train weigth
for x, y in zip(train_x,train_y):
print LR.predict(x),y
print LR.weight,LR.bias
elif key == ord('c'): # train LR model
for epoch in range(10000):
for x, y in zip(train_x,train_y):
LR.fit(x,y)
elif key == ord('d'): # draw line
points = []
for x in range(w):
y = LR.hypoth(x*1.0/w)
if 0 <= y and y <=1.0:
points.append((x,int(y*h)))
s = points[0]
e = points[-1]
cv2.line(img,s,e,(0,0,255),2)