I wanted to use a support vector machine, so I decided to write FizzBuzz as a starting point.
The support vector machine (SVM) is one of the pattern recognition models that uses supervised learning. It can be applied to identification and regression analysis. The support vector machine is one of the learning models with excellent recognition performance among the currently known methods. The reason why the support vector machine can exhibit excellent recognition performance is that there is a device for obtaining high discrimination performance for unlearned data.
http://ja.wikipedia.org/wiki/%E3%82%B5%E3%83%9D%E3%83%BC%E3%83%88%E3%83%99%E3%82%AF%E3%82%BF%E3%83%BC%E3%83%9E%E3%82%B7%E3%83%B3
My understanding is that support vector machines can identify unlearned data by biting (basically binary?) Answered learning data (basically multidimensional parameters). A place called "pattern recognition model".
Install LIBSVM. http://www.csie.ntu.edu.tw/~cjlin/libsvm/ Please note that the README windows item does not seem to be maintained. Basically, it seems to work if you pass the path to the python subfolder under the download folder.
Use the svm wrapper in your program. For the specific usage of the module, I referred to the following site. http://tkoyama1988.hatenablog.com/entry/2013/12/09/125143
Rough movement of support vector machine
I want to use the original inputs 1 to 100 as unlearned data. ↓ It seems that more than 200 numbers should be used as learning data.
If you simply use more than 200 numbers as training data and 1 to 100 as unlearned data, it will not work because there is nothing in common. ↓ If the remainder (150 dimensions) when each number is divided by 1 to 150 is used as the parameter for each number, it seems to work somehow?
So I made the following function.
def parameterize(n):
RANGE = range(1,150)
return [n]+[n%i for i in RANGE]
I basically only know binary judgments. ↓ There are 4 types of FizzBuzz output (number itself, Fizz, Buzz, FizzBuzz) ↓ Let's make 3 filters.
image Number or letter? → Number ↓ Fizz or Buzz?→ Fizz ↓ FizzBuzz or Buzz?→Buzz ↓ FizzBuzz
So I made the following function. A function that converts the input value n to the expected value. One expected value is 1 and the other expected value is -1.
def string_or_number(n):
if (n % 3) == 0 or (n % 5) == 0:
return 1
else:
return -1
def fizz_or_buzz(n):
if (n % 5) == 0:
return 1
else:
return -1
def buzz_or_fizzbuzz(n):
if (n % 15) == 0:
return 1
else:
return -1
I also made a function to find the expected value by parameterizing the input value n.
def create_train_data_and_label(n):
data = parameterize(n)
label1 = string_or_number(n)
label2 = fizz_or_buzz(n)
label3 = buzz_or_fizzbuzz(n)
return data, label1, label2, label3
This is a function that converts the output values p1, p2, p3 to any of numbers, Fizz, Buzz, and FizzBuzz.
def output(n, p1, p2, p3):
if p1 < 0:
return n, n
else:
pass
if p2 < 0:
return n, 'Fizz'
else:
pass
if p3 < 0:
return n, 'Buzz'
else:
return n, 'FizzBuzz'
For svm, we created a function that trains from the parameters of training data and expected values and returns a model.
def study(data, label):
prob = svm_problem(label, data)
param = svm_parameter('-s 0 -t 0')
m = svm_train(prob, param)
return m
And I made main () as follows.
def main():
START = 1
FINISH = 101
TEST_START = 101
TEST_FINISH = 3001
#Creating training data
data, label1, label2, label3 = create_trainers(TEST_START,TEST_FINISH)
#Learning
m1 = study(data,label1)
m2 = study(data,label2)
m3 = study(data,label3)
#Creation of unlearned data
params, expected_label1, expected_label2, expected_label3 = create_trainers(START,FINISH)
#Getting output from untrained data
p1_labels, p1_acc, p1_vals = svm_predict(expected_label1, params, m1)
p2_labels, p2_acc, p2_vals = svm_predict(expected_label2, params, m2)
p3_labels, p3_acc, p3_vals = svm_predict(expected_label3, params, m3)
#View results
for n in range(START,FINISH): print output(n, p1_vals[n-1][0], p2_vals[n-1][0], p3_vals[n-1][0]),
main()
(Input value, (converted) output value) When the training data is set to 200 to 3000, it works well in some cases such as 5 and 20 although it does not work well. When I reduced the learning data to 200-300, it didn't work at all.
# coding: utf-8
import sys
sys.path.append('./libsvm-3.20/python')
from svm import *
from svmutil import *
def parameterize(n):
RANGE = range(1,150)
return [n]+[n%i for i in RANGE]
def create_train_data_and_label(n):
data = parameterize(n)
label1 = string_or_number(n)
label2 = fizz_or_buzz(n)
label3 = buzz_or_fizzbuzz(n)
return data, label1, label2, label3
def create_trainers(start,finish):
data_list, label1_list, label2_list, label3_list = [], [], [], []
for n in range(start,finish):
data, label1, label2, label3 = create_train_data_and_label(n)
data_list.append(data)
label1_list.append(label1)
label2_list.append(label2)
label3_list.append(label3)
return data_list, label1_list, label2_list, label3_list
def string_or_number(n):
if (n % 3) == 0 or (n % 5) == 0:
return 1
else:
return -1
def fizz_or_buzz(n):
if (n % 5) == 0:
return 1
else:
return -1
def buzz_or_fizzbuzz(n):
if (n % 15) == 0:
return 1
else:
return -1
def study(data, label):
prob = svm_problem(label, data)
param = svm_parameter('-s 0 -t 0')
m = svm_train(prob, param)
return m
def output(n, p1, p2, p3):
if p1 < 0:
return n, n
else:
pass
if p2 < 0:
return n, 'Fizz'
else:
pass
if p3 < 0:
return n, 'Buzz'
else:
return n, 'FizzBuzz'
def main():
START = 1
FINISH = 101
TEST_START = 101
TEST_FINISH = 3001
#Creating training data
data, label1, label2, label3 = create_trainers(TEST_START,TEST_FINISH)
#Learning
m1 = study(data,label1)
m2 = study(data,label2)
m3 = study(data,label3)
#Creation of unlearned data
params, expected_label1, expected_label2, expected_label3 = create_trainers(START,FINISH)
#Get output
p1_labels, p1_acc, p1_vals = svm_predict(expected_label1, params, m1)
p2_labels, p2_acc, p2_vals = svm_predict(expected_label2, params, m2)
p3_labels, p3_acc, p3_vals = svm_predict(expected_label3, params, m3)
#View results
for n in range(START,FINISH): print output(n, p1_vals[n-1][0], p2_vals[n-1][0], p3_vals[n-1][0]),
main()
Libsvm distributor http://www.csie.ntu.edu.tw/~cjlin/libsvm/
A site that taught me how to use libsvm in python http://tkoyama1988.hatenablog.com/entry/2013/12/09/125143
Recommended Posts