- Knowledge for Developers.
Follow Us
Home Backend Nearest Neighbor Artificial Intelligence with PHP

Nearest Neighbor Artificial Intelligence with PHP

Have you ever heard of the nearest neighbor? In my studies of Artificial Intelligence, this was the first thing (I don’t remember the reason) that I came into contact with! But besides being something that seems simple, it was something that I found super interesting, that I saw N possibilities that could be applied. As I am very fond of aeronautics, in addition to IT, I imagined countless possibilities in this field! 🛫 Afterwards, I want to write an article about the things I imagined regarding this.

And you?! Can you think of something to use this algorithm for?! Do not know what it is????? Keep reading, and I’ll explain!

This was one of the first algorithms used to determine the most obvious problem that the name itself says 😅 “generate shorter paths”. It is also possible to define groups, etc. But it is not always ideal. Oh, but how so?! Well, that will be for another article!

Maybe some R, Java, C++, Py, and JS people want to sacrifice me for saying that, but yes, this example will be done with PHP 😮. That’s right,


The answer is yes! However, it is quite obvious that it has many limitations since PHP was not designed for this. Later I will talk more about the best languages ​​​​for artificial intelligence. The answer is yes!

To facilitate our work, I will use the php/ML library, a simple library that will facilitate our understanding, as I will not go into details of calculations in this article (I will do that in another). Here, I’m just showing how the algorithm works and one of its possibilities.

So that we can calculate who will be the nearest neighbor and better understand the algorithm, we need to use the Cartesian plane! That’s right, remember high school? Bingo! Or were you skipping class?! 😏. For those who don’t remember, this is the famous Cartesian plan:

Now you remember, right?! 😏

To carry out this algorithm, we will need the inputs, which are nothing more than a point within this plane. Our entries will be [[1, 3], [1, 4], [2, 4], [3, 1], [5, 1], [7, 2]], can you see these entries which we will call labels on the plan? No? I’ll help you out then. Remember, the first entry is always on the X axis, and the second data refers to the Y axis. In the plane, our entries would look like this:

A lot better to visualize. Don’t you think? Thank you very much René Descartes 🙌.

Now let’s imagine a real-world situation. Imagine that the points are houses spread out on a map, and A, B, C, and D belong to the “barrudo” neighborhood, and E and F belong to the “path” neighborhood. The city hall classifies the house in the neighborhood according to the proximity of the house to the existing group of houses. A construction company decides to construct a house at point [3,3]. Can you tell which neighborhood this house would belong to?!

Let’s go to the plan to see where this house would be located. We add the input to the plan, and then, can you imagine where it would be? Failed?! Then follow the image:

With the naked eye, it is easy to see in which neighborhood the house will belong! The image below will help us to have a better view:

We see that G [new point added] is within the same neighborhood as houses A, B, C, and D belong to. That would probably generate what we call bias later on (but that’s the subject of another article).

But we did the whole procedure by hand, and if we wanted to automate it, how would it be done? As I said at the beginning of the article, we have an excellent PHP/ML library. It is very simple to install and can be done via composer, simply with this code:

composer require php-ai/php-ml

After installing the library, we will reference and use the following classes, KNearestNeighbors, and Minkowski, to correct the distance. Below is the commented code in PHP so that you can see how this process is simple when we use this library:

Pretty simple, do you agree? I hope you enjoyed this article, and I hope to write more articles about AI. I will use other languages ​​as well. I have no prejudice against language. After all, the best language is the one that meets your needs. I will answer any questions, leave them in the comments or call me on LinkedIn. See you next time.

Written by
Levi Costa

Senior Programmer Analyst at DeliverIT (the PayGo team), PHP, Java, R, and C developer for over 5 years, experienced in payment methods, IoT, and AI lover for all issues of life S2.

Leave a comment

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Related Articles


How to Create a Skill for Amazon’s Virtual Assistant Alexa

If you didn’t know, it is not necessary for an Amazon Echo...


The APIs role in a 5G world

5G is about to revolutionize how we connect and use technology daily....


EF Core using AsNoTracking with Identity Resolution

Today we will see the consequences of using AsNoTracking with the Identity...


Understand key features added in ASP.NET Core 7.0

Version 7.0 of the .NET platform brought many new features in this...