I wrote FizzBuzz in python using a support vector machine (library LIVSVM).

Introduction

I wanted to use a support vector machine, so I decided to write FizzBuzz as a starting point.

What is a support vector machine?

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".

To use a support vector machine with python (example)

  1. 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.

  2. 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

What should be the training data and what should be the unlearned data?

Rough movement of support vector machine

  1. Create a model by learning based on the training data.
  2. Judge unlearned data based on the model.

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.

Data parameterization

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]

Expected value

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()

result

(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.

001.png

The big picture of the created code

# 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()

Reference site

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

I wrote FizzBuzz in python using a support vector machine (library LIVSVM).
A memo that I wrote a quicksort in Python
I wrote a class in Python3 and Java
I wrote a Japanese parser in Japanese using pyparsing.
I wrote python in Japanese
[Python] Sort apples and pears from pixel values using a support vector machine (SVM)
I made a quick feed reader using feedparser in Python
Try building a neural network in Python without using a library
I tried to make a stopwatch using tkinter in python
I made a Line-bot using Python!
I wrote Fizz Buzz in Python
I wrote the queue in Python
I wrote the stack in Python
I wrote a function to load a Git extension script in Python
I wrote a script to extract a web page link in Python
I get a can't set attribute when using @property in python
[Python] I wrote a REST API using AWS API Gateway and Lambda.
I made a payroll program in Python!
I tried using Bayesian Optimization in Python
Scraping a website using JavaScript in Python
Draw a tree in Python 3 using graphviz
I created a password tool in Python.
I wrote a code to convert quaternions to z-y-x Euler angles in Python
[Python] I forcibly wrote a short Perlin noise generation function in Numpy.
I wrote a graph like R glmnet in Python for sparse modeling in Lasso
Create a GIF file using Pillow in Python
I want to create a window in Python
I tried playing a typing game in Python
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
I wrote "Introduction to Effect Verification" in Python
View drug reviews using a list in Python
I wrote a design pattern in kotlin Prototype
I tried reading a CSV file using Python
[Python] I thoroughly explained the theory and implementation of support vector machine (SVM)
FizzBuzz in Python
I tried adding a Python3 module in C
Get a glimpse of machine learning in Python
Create a MIDI file in Python using pretty_midi
I made a Caesar cryptographic program in Python.
Calculation of support vector machine (SVM) (using cvxopt)
A note on the library implementation that explores hyperparameters using Bayesian optimization in Python
What I learned about AI / machine learning using Python (1)
Create a data collection bot in Python using Selenium
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I made a prime number generation program in Python
Basics of I / O screen using tkinter in python3
I made a login / logout process using Python Bottle.
I tried using TradeWave (BitCoin system trading in Python)
I want to write in Python! (2) Let's write a test
I wrote a design pattern in kotlin Singleton edition
I wrote a design pattern in kotlin Adapter edition
I want to randomly sample a file in Python
I want to work with a robot in python.
What I learned about AI / machine learning using Python (3)
[Python] I made a classifier for irises [Machine learning]
[Python] I tried running a local server using flask
I tried drawing a pseudo fractal figure using Python
I made a prime number generation program in Python 2
I made a python library to do rolling rank
Using the National Diet Library Search API in Python