Logstic

# -*- 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)

    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值