<file_sep># -*- coding: utf-8 -*-
import numpy as np
from os import listdir
from sklearn.svm import SVC

def im2vector(filname):#对数据进行扁平化处理
    returnvect=np.zeros((32,32))
    file=open(filname)
    linestr=file.readlines()
    index=0
    for i in linestr:
        words=i.strip()
        words=list(words)
        returnvect[index,:]=words[0:32]
        index+=1
    returnvect=returnvect.flatten()    
    return returnvect

def handwritingclasstest():
    hwlabels=[]
    trainingflilelist=listdir('trainingdigits')
    m=len(trainingflilelist)
    trainningmat=np.zeros((m,1024))
    for i in range(m):
        filenamestr=trainingflilelist[i]
        classnumber=int(filenamestr.split('_')[0])
        hwlabels.append(classnumber)
        trainningmat[i,:]=im2vector('trainingDigits/'+filenamestr)
    clf=SVC(C=200)
    clf.fit(trainningmat,hwlabels)
    testfilelist=listdir('testDigits')
    errorcount=0
    mtest=len(testfilelist)
    for i in range(mtest):
        filenamestr=testfilelist[i]
        classnumber=int(filenamestr.split('_')[0])
        vectorundertest=im2vector('testDigits/'+filenamestr)
        classifiterresult=clf.predict(vectorundertest.reshape(1,-1))
        print("分类返回结果为%d\t真实结果为%d" % (classifiterresult, classnumber))
        if classifiterresult!=classnumber:
            errorcount+=1
    print('共预测错误%d个数\n错误率为%f%%'%(errorcount,errorcount/mtest*100))

if __name__=="__main__":
    
#    filename='D:/程序/python/dl/trainingDigits/0_0.txt'
#    im2vector(filename)
    handwritingclasstest()<file_sep># ML
常用机器学习算法，原理及实战
<file_sep>import matplotlib.pyplot as plt
import numpy as np 
import random

def loaddataset(filename):
    datamat = []
    labelmat =[]
    fr = open(filename)
    for line in fr.readlines():
        linearr = line.strip().split('\t')
        datamat.append([float(linearr[0]),float(linearr[1])])
        labelmat.append(float(linearr[2]))
    return datamat,labelmat

    
def selectjrand(i,m):
    j=i
    while(j==i):
        j=int(random.uniform(0,m))
    return j

def clipalpha(aj,h,l):
    if aj>h:
        aj=h
    if l > aj:
        aj=l
    return aj

def smosimple(datamatin,classlabels,c,toler,maxiter):
    datamatrix = np.mat(datamatin)
    labelmat = np.mat(classlabels).transpose()
    b=0
    m,n=np.shape(datamatrix)
    alphas = np.mat(np.zeros((m,1)))
    iter_num = 0
    while (iter_num < maxiter):
        alphapairschanged = 0
        for i in range(m):
            fxi = float(np.multiply(alphas,labelmat).T * (datamatrix * datamatrix[i,:].T))+b
            ei=fxi - float(labelmat[i])
            if ((labelmat[i]*ei < -toler) and (alphas[i] <c)) or ((labelmat[i] * ei >toler) and (alphas[i] > 0)):
                j = selectjrand(i,m)
                fxj= float(np.multiply(alphas,labelmat).T * (datamatrix * datamatrix[j,:].T))+b
                ej=fxj - float(labelmat[j])
            
                alphaiold = alphas[i].copy()
                alphajold = alphas[j].copy()
                if (labelmat[i] != labelmat[j]):
                    l= max(0,alphas[j]-alphas[i])
                    h= min(c,c+alphas[j] - alphas[i])
                else :
                    l = max(0,alphas[j] + alphas[i] -c)
                    h = min(c, alphas[j] + alphas[i])
                if l == h :
                    print('l==h')
                    continue
                eta = 2.0 * datamatrix[i,:]*datamatrix[j,:].T - datamatrix[i,:] * datamatrix[i,:].T - datamatrix[j,:] * datamatrix[j,:].T
                if eta >=0:
                    print('eta>=0')
                    continue
                alphas[j] -=labelmat[j] * (ei - ej)/eta
                alphas[j] = clipalpha(alphas[j],h,l)
                if (abs(alphas[j] - alphajold) < 0.00001):
                    print('alpha_ja too small')
                    continue
                alphas[i] +=labelmat[j] * labelmat[i] * (alphajold - alphas[j])
                b1 = b - ei -labelmat[i]*(alphas[i]-alphaiold)*datamatrix[i,:]*datamatrix[i,:].T - labelmat[j]*(alphas[j]-alphajold)*datamatrix[i,:]*datamatrix[j,:].T
                b2 = b - ei -labelmat[i]*(alphas[i]-alphaiold)*datamatrix[i,:]*datamatrix[j,:].T - labelmat[j]*(alphas[j]-alphajold)*datamatrix[j,:]*datamatrix[j,:].T
                if (0 < alphas[i]) and (c > alphas[i]):
                    b=b1
                elif (0 < alphas[j]) and (c > alphas[j]):
                    b=b2
                else:
                    b=(b1+b2)/2.0
                
                alphapairschanged +=1
    
                print('the %d iteration,sampel:%d,aplpha optimize num:%d'%(iter_num,i,alphapairschanged))
        if (alphapairschanged == 0):
            iter_num +=1
        else:
            iter_num = 0
        print('iteration num : %d:'% iter_num)
    return b,alphas

def showclassifer(datamat,labelmat,w,b):
    data_plus=[]
    data_minus = []
    for i in range(len(datamat)):
        if labelmat[i]>0:
            data_plus.append(datamat[i])
            data_minus.append(datamat[i])
    data_plus_np = np.array(data_plus)
    data_minus_np = np.array(data_minus)
    plt.scatter(np.transpose(data_plus_np)[0],np.transpose(data_plus_np)[1])
    plt.scatter(np.transpose(data_minus_np)[0],np.transpose(data_minus_np)[1])
    x1 = max (datamat)[0]
    x2 = min(datamat)[0]
    a1,a2 = w
    b = float(b)
    a1= float(a1[0])
    a2 = float(a2[0])
    y1,y2 = (-b-a1*x1)/a2,(-b-a1*x2)/a2
    plt.plot([x1,x2],[y1,y2])
    for i ,alpha in enumerate(alphas):
        if abs(alpha) > 0:
            x,y= datamat[i]
            plt.scatter([x],[y],s=150,c='none',alpha=0.7,linewidths=1.5,edgecolors='red')
    plt.show()
            
def get_w(datamat,labelmat,alphas):
    alphas,datamat,labelmat=np.array(alphas),np.array(datamat),np.array(labelmat)
    w = np.dot((np.tile(labelmat.reshape(1,-1).T,(1,2))*datamat).T,alphas)
    return w.tolist()
if __name__=='__main__':
    datamat,labelmat = loaddataset('testSet.txt')
    b,alphas = smosimple(datamat,labelmat,0.6,0.001,40)
    w = get_w(datamat,labelmat,alphas)
    showclassifer(datamat,labelmat,w,b)
<file_sep># -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import matplotlib.pyplot as plt
import numpy as np 
import random
import time

class optstruct:
    def __init__(self,datamatin,classlabels,c,toler):
        self.x = datamatin
        self.labelmat = classlabels
        self.c = c
        self.tol = toler
        self.b = 0 
        self.m = np.shape(datamatin)[0]
        self.alphas = np.mat(np.zeros((self.m , 1)))
        self.ecache = np.mat(np.zeros((self.m,2)))

def loaddataset(filename):
    datamat = []
    labelmat =[]
    fr = open(filename)
    for line in fr.readlines():
        linearr = line.strip().split('\t')
        datamat.append([float(linearr[0]),float(linearr[1])])
        labelmat.append(float(linearr[2]))
    return datamat,labelmat

def calcek(os,k):
    fxk = float(np.multiply(os.alphas,os.labelmat).T * (os.x * os.x[k,:].T))+os.b
    ek = fxk - float(os.labelmat[k])
    return ek

def selectjrand(i,m):
    j=i
    while(j==i):
        j=int(random.uniform(0,m))
    return j

def selectj(i,os,ei):
    maxk = -1 
    maxdeltae=0
    ej=0
    os.ecache[i] = [1,ei]
    validecachelist = np.nonzero(os.ecache[:,0].A)[0]
    if (len(validecachelist))>1:
        for k in validecachelist:
            if k == i:
                continue
            ek = calcek(os,k)
            deltae = abs(ei - ek)
            if (deltae > maxdeltae):
                maxk = k 
                maxdeltae = deltae
                ej = ek
        return maxk , ej
    else:
        j=selectjrand(i,os.m)
        ej = calcek(os,j)
    return j,ej

def updataek(os,k):
    ek = calcek(os,k)
    os.ecache[k] = [1,ek]
    
def clipalpha(aj,h,l):
    if aj>h:
        aj=h
    if l > aj:
        aj=l
    return aj

def innerl(i,os):
    ei = calcek(os,i)
    if ((os.labelmat[i]*ei < -os.tol) and (os.alphas[i] < os.c)) or ((os.labelmat[i]*ei > -os.tol) and (os.alphas[i] > 0)):
        j,ej = selectj(i,os,ei)
        alphaiold = os.alphas[i].copy()
        alphajold = os.alphas[j].copy()
        if (os.labelmat[i]) != os.labelmat[j]:
            l = max(0,os.alphas[j] + os.alphas[i])
            h = min(os.c ,os.c+os.alphas[j] - os.alphas[i])
        else:
            l = max(0,os.alphas[j] + os.alphas[i] - os.c)
            h = min(os.c,os.alphas[j]+os.alphas[i])
        if l == h:
            print('l==h')
            return 0
        eta = 2.0 * os.x[i,:] * os.x[j,:].T - os.x[i,:] * os.x[i,:].T - os.x[j,:] * os.x[j,:].T
        if eta >= 0:
            print("eta>=0")
            return 0
        #步骤4：更新alpha_j
        os.alphas[j] -= os.labelmat[j] * (ei - ej)/eta
        #步骤5：修剪alpha_j
        os.alphas[j] = clipalpha(os.alphas[j],h,l)
        #更新Ej至误差缓存
        updataek(os, j)
        if (abs(os.alphas[j] - alphajold) < 0.00001):
            print("alpha_j变化太小")
            return 0
        os.alphas[i] += os.labelmat[j] * os.labelmat[i] * (alphaiold - os.alphas[j])
        updataek(os,i)
        b1 = os.b - ei - os.labelmat[i] * (os.alphas[i] - alphaiold) * os.x[i,:] * os.x[i,:].T- os.labelmat[j]*(os.alphas[j]-alphajold)*os.x[i,:]*os.x[j,:].T
        b2 = os.b - ej - os.labelmat[i] * (os.alphas[i] - alphaiold) * os.x[i,:] * os.x[j,:].T- os.labelmat[j]*(os.alphas[j]-alphajold)*os.x[j,:]*os.x[j,:].T
        if (0 < os.alphas[i]) and (os.c > os.alphas[i]):
            os.b =b1
        elif (0 < os.alphas[j]) and (os.c > os.alphas[j]):
            os.b = b2
        else:
            os.b = (b1 + b2)/2.0
        return 1
    else:
        return 0

def smop(datamatin,classlabels,c,toler,maxiter):
    os = optstruct(np.mat(datamatin),np.mat(classlabels).transpose(),c,toler)
    iter = 0
    entireset = True
    alphapairschanged = 0
    while (iter < maxiter) and ((alphapairschanged > 0) or (entireset)):
        alphapairschanged= 0
        if entireset:
            for i in range(os.m):
                alphapairschanged += innerl(i,os)
                print('all sample:the %d iteration,sampel:%d,aplpha optimize num:%d'%(iter,i,alphapairschanged))
            iter +=1
        else:
            nonboundis = np.nonzero((os.alphas.A >0) * (os.alphas.A < c))[0]
            for i in nonboundis:
                alphapairschanged +=innerl(i,os)
                print('nonboundis:the %d iteration,sampel:%d,aplpha optimize num:%d'%(iter,i,alphapairschanged))        
            iter +=1
        if entireset:
            entireset = False
        elif (alphapairschanged == 0):
            entireset = True
        print('iteration num : %d'%iter)
    return os.b,os.alphas

def showclassifer(datamat,labelmat,w,b):
    data_plus=[]
    data_minus = []
    for i in range(len(datamat)):
        if labelmat[i]>0:
            data_plus.append(datamat[i])
        else:
            data_minus.append(datamat[i])
    data_plus_np = np.array(data_plus)
    data_minus_np = np.array(data_minus)
    plt.scatter(np.transpose(data_plus_np)[0],np.transpose(data_plus_np)[1])
    plt.scatter(np.transpose(data_minus_np)[0],np.transpose(data_minus_np)[1])
    x1 = max (datamat)[0]
    x2 = min(datamat)[0]
    a1,a2 = w
    b = float(b)
    a1= float(a1[0])
    a2 = float(a2[0])
    y1,y2 = (-b-a1*x1)/a2,(-b-a1*x2)/a2
    plt.plot([x1,x2],[y1,y2])
    for i ,alpha in enumerate(alphas):
        if abs(alpha) > 0:
            x,y= datamat[i]
            plt.scatter([x],[y],s=150,c='none',alpha=0.7,linewidths=1.5,edgecolors='red')
    plt.ylim(-20,20)
    plt.show()

def get_w(alphas,dataarr,classlabels):
     x = np.mat(dataarr)
     labelmat = np.mat(classlabels).transpose()
     m,n = np.shape(x)
     w = np.zeros((n,1))
     for i in range(m):
         w +=np.multiply(alphas[i] * labelmat[i],x[i,:].T)
     return w
   
if __name__=='__main__':
    datamat,labelmat = loaddataset('testSet.txt')
    start = time.time()
    b,alphas = smop(datamat,labelmat,0.6,0.001,40)
    w = get_w(alphas,datamat,labelmat)
    showclassifer(datamat,labelmat,w,b)
    end = time.time()
    print('times is:%d'%(end-start) )