numpy实现线性可分支持向量机

import torch
import torch.nn as nn    
import numpy as np    
import pandas as pd
a=np.zeros(128)
b=0
def take(i1,i2):
    global b
    if i1==i2:
        return 0
    w=0
    for i in range(128):
        w=w+a[i]*train[i]*train_label[i]    
    e1=np.dot(w,train[i1])+b-train_label[i1]
    e2=np.dot(w,train[i2])+b-train_label[i2]
    eta=np.dot(train[i1],train[i1])+np.dot(train[i2],train[i2])-2*np.dot(train[i1],train[i2])
    alpha2=a[i2]+train_label[i2]*(e1-e2)/eta
    L=0
    H=0
    old2=a[i2]
    if train_label[i1]!=train_label[i2]:
        L=np.maximum(0,a[i2]-a[i1])
        if alpha2<L:
            a[i2]=L
        else:
            a[i2]=alpha2
    else:
        L=0
        H=np.maximum(0,a[i2]+a[i1])
        if alpha2<L:
            a[i2]=L
        elif alpha2>H:
            a[i2]=H
        else:
            a[i2]=alpha2
    a[i1]=a[i1]+train_label[i1]*train_label[i2]*(old2-a[i2])
    b1=train_label[i1]-np.dot(w,train[i1])
    b2=train_label[i2]-np.dot(w,train[i2])
    if a[i1]>0:
        b=train_label[i1]-np.dot(w,train[i1])
    elif a[i2]>0:
        b=train_label[i2]-np.dot(w,train[i2])
    else:
        b=(b1+b2)/2
    if abs(old2-a[i2])<=0.0001:
        return 0
    return 1
def examineExample(i2):
    for suo in range(128):
        if(a[suo]>0):
            if (take(suo,i2)):
                return 1
    for suo in range(128):
        if(a[suo]==0):
            if (take(suo,i2)):
                return 1
    return 0    
train=np.random.randint(-300,300,(128,4))
test=np.random.randint(-300,300,(30,4))
train=train.astype(float)
test=test.astype(float)
for i in range(4):
    train[:,i]=(train[:,i]-train[:,i].mean())/train[:,i].std()
for i in range(4):
    test[:,i]=(test[:,i]-test[:,i].mean())/test[:,i].std()
print(train)
w=[1,2,3,4]
train_label=np.dot(train,w)+5
test_label=np.dot(test,w)+5
train_label=train_label>0
train_label=train_label.astype(int)
train_label=train_label*2-1
test_label=test_label>0
test_label=test_label.astype(int)
test_label=test_label*2-1


numChanged=0
examineAll=1
while(numChanged>0 or examineAll):
    numChanged=0
    if examineAll:
        for i in range(128):
            numChanged=numChanged+examineExample(i)
    else:
        for i in range(128):
            if(a[i]>0):
                numChanged=numChanged+examineExample(i)
    if examineAll==1:
        examineAll=0
    elif numChanged==0:
        examineAll=1
w=0
for i in range(128):
    w=w+a[i]*train[i]*train_label[i]  
m=0
max=0
li=0
for i in range(128):
    if i==0:
        max=a[i]
        li=0
    if(a[i]!=0):
        b=train_label[i]-np.dot(w,train[i])
        print(b)
        if i==0:
            li=0
            max=a[i]
        elif max<np.float(a[i]):
            max=a[i]
            li=i
b=train_label[li]-np.dot(w,train[li]) 
print(b)
sum1=0
loss=0
for i in range(128):
    targer=0
    if np.dot(w,train[i])+b>0:
        targer=1
    elif  np.dot(w,train[i])+b<0:
        targer=-1
    if targer!=train_label[i]:
        sum1=sum1+1
print("训练出错数",sum1,"训练误差率:",sum1/128)
for i in range(30):
    targer=0
    if np.dot(w,train[i])+b>0:
        targer=1
    elif  np.dot(w,train[i])+b<0:
        targer=-1
    if targer!=train_label[i]:
        loss=loss+1
print("测试出错数",loss,"测试误差率:",loss/30)
print(a)
print(w,b)
posted @ 2021-08-05 21:43  祥瑞哈哈哈  阅读(82)  评论(0编辑  收藏  举报