# Introduction

As a study of machine learning, I am reading "* Learning from the basics: Artificial intelligence textbook *".

The feature of this book is that the end-of-chapter problem contains a simple program of `Python`.

Here, it is copied with `Ruby`.

# Chapter 3 End of Chapter Problem

#### `kneighbor.rb`

``````
itemdata = [[30, 50, 'A'], [65, 40, 'B'],
[90, 100, 'A'], [90, 60, 'B'],
[70, 60, 'B'], [40, 50, 'A'],
[80, 50, 'B']]

print 'Please enter the height to be classified:'
h = gets.to_i
print 'Enter the top surface area to be classified:'
a = gets.to_i

print itemdata.sort_by{|x, y, _| (x - h) ** 2 + (y - a) ** 2}
``````

This is a problem to create a program that solves the classification problem by the k-nearest neighbor method.

• [K-nearest neighbor method-wikipedia](https://ja.wikipedia.org/wiki/K%E8%BF%91%E5%82%8D%E6%B3%95#:~:text=k%E8% BF% 91% E5% 82% 8D% E6% B3% 95% EF% BC% 88% E3% 82% B1% E3% 82% A4% E3% 81% 8D% E3% 82% 93,% E6% 8E % A2% E7% B4% A2% E5% 95% 8F% E9% A1% 8C% E3% 81% AE% E4% B8% 80% E3% 81% A4% E3% 80% 82) *
``````Please enter the height to be classified: 50
Enter the top surface area to be classified: 50
[[40, 50, "A"], [65, 40, "B"], [30, 50, "A"], [70, 60, "B"], [80, 50, "B"], [90, 60, "B"], [90, 100, "A"]]
``````

For example, data with a `height 50 top surface area 50` is classified as ʻA`.

# Summary

• Read up to Chapter 3