Sabtu, 11 Juli 2020

Python Code Simple LVQ

algo_LVQ_lat2
In [14]:
import numpy as np

class LVQTrain():

    def __init__(self):
        self.train_data = np.transpose(DATATRAINING) #Train Data
        self.weight = np.transpose(np.matrix([[1,0,1,0],[0,1,0,1]])) #Weight
        print ('Weight: \n', self.weight)
        self.train_T = DATATARGET #Actual Class
        self.r_alpha = 0.9 #r-learning rate
        self.alpha = 0.001 #learning rate
        self.epoch = 10 #epoch
        
    def getWeight(self):
        for i in range(0, self.epoch): #loop from 1 - epoch
            for j in range(0, self.train_data.shape[1]):
                temp = np.zeros((1, self.weight.shape[1]))
                for k in range(0, self.weight.shape[1]):
                    for l in range(0, self.weight.shape[0]):
                        temp[0, k] = temp [0, k] + (self.weight[l, k] - self.train_data[l, j])**2
                
                Cj = np.argmin(temp)
                if Cj == self.train_T[0, j]:
                    temp2 = self.weight[:, Cj]
                    temp2 = temp2 + self.alpha * (self.train_data[:, j] - temp2)
                    self.weight[:, Cj] = temp2
                else:
                    temp2 = temp2[:, Cj]
                    temp2 = temp2 - self.alpha * (self.train_data[:, j] - temp2)
                    self.weight[:, Cj] = temp2
                    
            self.alpha = self.r_alpha * self.alpha
        
        print ('Updated Weight: \n', self.weight)
                                          
                
class LVQTest(LVQTrain):
    def getClassification(self, test_data, test_T):
        self.getWeight()
        self.test_data = np.transpose(test_data)
        self.test_T = test_T
        self.classification = np.zeros((1, self.test_data.shape[1]))
        self.conf_matrix = np.zeros((2, 2))

        for j in range(0, self.test_data.shape[1]):
                temp = np.zeros((1, self.weight.shape[1]))
                for k in range(0, self.weight.shape[1]):
                    for l in range(0, self.weight.shape[0]):
                        temp[0, k] = temp [0, k] + (self.weight[l, k] - self.test_data[l, j])**2
                
                Cj = np.argmin(temp)
                self.classification[0, j] = Cj
                self.conf_matrix[self.test_T[0, j], Cj] = self.conf_matrix[self.test_T[0, j], Cj] + 1
                
        self.accuration = sum(np.diag((self.conf_matrix)))/sum(sum(self.conf_matrix))
        print ('\nClassification Result:\n', self.classification)
        print ('Confussion Matrix:\n', self.conf_matrix)
        print ('Accuration:\n', self.accuration*100,'%')


if __name__ == '__main__':
    DATATRAINING=np.matrix([[1,0,0,1],[1,1,1,0],[0,0,0,1],[0,1,1,1]])
    DATATARGET= np.matrix([0,0,1,1])
    DATAOUT = np.matrix([0,1]) 
        
In [15]:
    #PENGUJIAN 1
    test_data = np.matrix([0,1,1,0]) #Data Test
    print ('Test Data1: \n', np.transpose(test_data))
    
    objTrain = LVQTest()
    objTrain.getClassification(test_data, DATAOUT)
Test Data1: 
 [[0]
 [1]
 [1]
 [0]]
Weight: 
 [[1 0]
 [0 1]
 [1 0]
 [0 1]]
Updated Weight: 
 [[1 0]
 [0 0]
 [0 0]
 [0 1]]

Classification Result:
 [[0.]]
Confussion Matrix:
 [[1. 0.]
 [0. 0.]]
Accuration:
 100.0 %
In [16]:
    #PENGUJIAN 2
    test_data = np.matrix([0,0,1,1]) #Data Test
    print ('Test Data2: \n', np.transpose(test_data))
    
    #Data Test Target Class
    objTrain = LVQTest()
    objTrain.getClassification(test_data, DATAOUT)
Test Data2: 
 [[0]
 [0]
 [1]
 [1]]
Weight: 
 [[1 0]
 [0 1]
 [1 0]
 [0 1]]
Updated Weight: 
 [[1 0]
 [0 0]
 [0 0]
 [0 1]]

Classification Result:
 [[1.]]
Confussion Matrix:
 [[0. 1.]
 [0. 0.]]
Accuration:
 0.0 %
In [17]:
    #PENGUJIAN 3
    test_data = np.matrix([[0,1,1,0],[0,0,1,1]]) #Data Test
    print ('Test Data3: \n', np.transpose(test_data))
    
    #Data Test Target Class
    objTrain = LVQTest()
    objTrain.getClassification(test_data, DATAOUT)
Test Data3: 
 [[0 0]
 [1 0]
 [1 1]
 [0 1]]
Weight: 
 [[1 0]
 [0 1]
 [1 0]
 [0 1]]
Updated Weight: 
 [[1 0]
 [0 0]
 [0 0]
 [0 1]]

Classification Result:
 [[0. 1.]]
Confussion Matrix:
 [[1. 0.]
 [0. 1.]]
Accuration:
 100.0 %
In [18]:
def mtrim(s):
    if s.endswith(" "): s = s[:-1]
    if s.startswith(" "): s = s[1:]
    return s


def uk1(img):
    width = int(img.shape[0])
    height = int(img.shape[1])
    print(width ,' x ' , height)
    return (width ,' x ' , height)

def uk2(img):
    width =len(img[0])
    height = len(img[1])
    print(width ,' x ' , height)
    return (width ,' x ' , height)

def model(img):
    lap=type(img)
    print(lap)
    return lap

def ubahUKuran(cv2,img):
    W=150
    H=150
    dim = (W, H)
    resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
    return resized

def attr(db):
    print(db.shape)
    print(type(db))
    print(db)
    return 1
In [ ]: