0% found this document useful (0 votes)
43 views3 pages

Bab 7

This document describes an experiment using learning vector quantization (LVQ), an algorithm for supervised classification. The goals are to understand and implement LVQ. Instructions are provided to run code in Google Colab that trains an LVQ model on synthesized classification data, tests the model, and calculates accuracy. The accuracy is printed along with a visualization of the training and test data points labeled by the model. Users are asked to run the code multiple times until an accuracy less than 1 is achieved and analyze where errors occur.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views3 pages

Bab 7

This document describes an experiment using learning vector quantization (LVQ), an algorithm for supervised classification. The goals are to understand and implement LVQ. Instructions are provided to run code in Google Colab that trains an LVQ model on synthesized classification data, tests the model, and calculates accuracy. The accuracy is printed along with a visualization of the training and test data points labeled by the model. Users are asked to run the code multiple times until an accuracy less than 1 is achieved and analyze where errors occur.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Praktikum 7

Learning Vector Quantization


Tujuan
1. Praktikan mampu memahami algoritma learning vector quantization
2. Praktikan mampu mengimplementasikan algoritma learning vector
quantization

Dasar Teori
Learning Vector Quantization
Learning vector quantization (LVQ) adalah metode klasifikasi yang
dirancang oleh Kohonen (1990). LVQ memiliki arsitektur yang mirip dengan
self-organizing maps (Kohonen, 1989).

Praktikum
1. Buka Google Colaboratory melalui tautan ini.
2. Tulis kode berikut ke setiap cell pada notebook tersebut.
a. Fungsi training LVQ

import numpy as np

def lvq_fit(train, target, lrate, b, max_epoch):


label, train_idx = np.unique(target, return_index=True)
weight = train[train_idx].astype(np.float64)
train = np.array([e for i, e in enumerate(zip(train, target))
if i not in train_idx])
train, target = train[:, 0], train[:, 1]
epoch = 0

while epoch < max_epoch:


for i, x in enumerate(train):
distance = [sum((w - x) ** 2) for w in weight]
min = np.argmin(distance)
sign = 1 if target[i] == label[min] else -1
weight[min] += sign * lrate * (x - weight[min])

44
lrate *= b
epoch += 1

return weight, label

b. Fungsi testing LVQ

def lvq_predict(X, model):


center, label = model
Y = []

for x in X:
d = [sum((c - x) ** 2) for c in center]
Y.append(label[np.argmin(d)])

return Y

c. Fungsi hitung akurasi

def calc_accuracy(a, b):


s = [1 if a[i] == b[i] else 0 for i in range(len(a))]

return sum(s) / len(a)

d. Percobaan data acak dengan visualisasi

from random import uniform


import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_blobs, make_classification

X, y = make_classification(n_samples=31, n_features=2,
n_redundant=0, n_informative=2, n_classes=3,
n_clusters_per_class=1)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2)

model = lvq_fit(X_train, y_train, lrate=.5, b=.8, max_epoch=50)


output = lvq_predict(X_test, model)
accuracy = calc_accuracy(output, y_test)
colors = 'rgbcmyk'

print('Accuracy:', accuracy)

45
for x, label in zip(X_train, y_train):
plt.plot(x[0], x[1], colors[label] + '.')

for center, label in zip(model[0], model[1]):


plt.plot(center[0], center[1], colors[label] + 'o')

for x, label in zip(X_test, output):


plt.plot(x[0], x[1], colors[label] + 'x')

Analisis
1. Jalankan kode d beberapa kali hingga didapat akurasi kurang dari 1. Amati
dan analisis di mana terjadi error.

46

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy