## k Nearest Neighbors with NumPy: make it faster!

In my previous article about k Nearest Neighbor, I have shown you how I see this algorithm. However, it was terribly slow: my computer was calculating it for full 3 days. Some research shown that NumPy is the way to go here.

I know that NumPy is used a lot in Machine Learning, so that is definitely a no-brainer that I took it. Follow me and I show you how I done it!

## k Nearest Neighbor (or kNN): how I see it

There is a big buzz around the whole machine learning and neural networks. But they are pretty difficult at first. So I began with where most people begin. First Machine Learning algorithm that I wrote myself, from first to last character is k Nearest Neighbor (or kNN).

For example, parametric algorithms require mathematic approach. Without it, you can have an idea how it works, but you will not be 100% sure. And I love to be 100% sure with my algorithms!

Let me give you my point of view on this algorithm! Vamos! 🙂

## How I Messed Up when Training Neural Network for First Time

Machine learning for me was like a some mythical black box magic surrounded by a thick mist and small gnomes protecting it. Only most courageous ones with enough work and some mathemagic dust shall pass the protection and enter the land of knowledge.

I never shy away from challenges that support my growth and since I love technology, I started practicing Machine Learning. When I came to CERN for internship, very soon I tried Artificial Neural Networks – the best sounding algorithm in the world!

Let me share my first experience with Artificial Neural Networks.

## How to Get Most out of Coding Bootcamp

I want to share with you a story about one student. He realized that sometimes he was behaving like robot: doing same thing over and over. He wanted to automate his business.

So, he goes to lectures, works his ass off. He comes back home and at least takes a look what he have done. More often than not, he spends few more hours a day on tasks that he comes up himself. Every time we talk, he just bombards me with questions and his experiences.

Needless to say, we both had amazing experience: growing and witnessing such rapid growth.

Let me share my believes and experiences how to get most out of coding bootcamp!

## How I managed to make all cores work in unison

I was conducting this experiment about variance of sample. All I needed was a program that does a lot of calculations. So I did that and it took a long time. I didn’t even wait till the end since I knew that after 2 hours it will take at least as much as it took.

Task manager showed that only single core was being utilized. Fuck that shit! Why did I bought powerful machine if I couldn’t use it to it’s full potential?

But here comes asynchronous programming – my saviour.

## Variance of sample: decreased or not?

One beautiful day I was watching statistics lessons on Khan Academy about variance and standard deviation of sample. To give a quick intro, variance for population is defined by formula , but for sample, it is . You shall not get confused by notation, the only difference is denominator: for sample it is decreased by one. And Sal Khan, the teacher, encouraged me to try and see if it is true. I immediately figured out, that this could be done with not-so difficult program.

Follow me and I’ll show you how I done it!

## Data Structures in Python: tuples and sets

One thing that astonished me when I stated digging into the world of Python was it’s rich support for data structures. While PHP had only arrays (well, SPL allows much more, but who uses SPL?), Python has not only lists. While it supports things like bytearray and buffer, I was mostly attracted to tuples and sets.

So lets get going!

## PHP’s Array vs Python’s List

To begin with, I have never ever written a useful program that didn’t involve some kind of dynamic in-memory storage. Luckily, I do most of the work with high-level programming languages, so data structures like doubly linked list are long gone. 🙂

I will show you differences and similarities of PHP’s array and Python’s list for cases that appear everyday in my life. Prepare for reading some code and let’s go!

## Why Automation is Key to Saving Money and more

I always look for ways to challenge and improve myself. But how can you improve when you are doing same things over and over? Once, Albert Einstein said “Insanity: doing the same thing over and over again and expecting different results”. Putting my workflow through this, I saw how much value automation produces. Think about it: Humans make mistakes and it is natural. However, machine is wrong only if you told it to be wrong. So, if we give correct instructions to it, amount of mistakes made is so little.

### Automation is everywhere!

Take a good look at your typical day. You probably use your smartphone or computer to check emails in the morning. Now imagine what is going behind the scenes: those devices send, receive and interpret information. Would you like to receive your e-mail and would not be able to read it, because it is encrypted? Or have to

Perhaps, then drive your car to office Do you have to worry what the ratio of air and petrol engine should receive? Or you just simply like to get from point A to point B, while listening to Pink Floyd song? I like latter one. But then approach red traffic light. Do you see any people switching it from green to red?

The point I want to make is that automation is everywhere. In my opinion, it is key reason for our growth. Compare how people used to live 100, 10 years ago and now. You don’t even have to look close! It is obvious. If you are young, ask your parents – they can definitely see the difference.

### Some benefits of automation

#### Saves money

This one summarizes all reasons below. If you have more time, then you can do more things! If you don’t have to do small tasks everyday, and focus only on your business, you make more money in the meantime!

#### Reduces mistakes

We both know that feeling when we f**k up. And it is not pleasant at all. It is called Human Factor and we can’t do much about it. Machines are different. They do what they are told. No more, no less.
Lets say we are launching a product. Marketing campaign went amazingly well and we have 1000 people waiting to buy our product for 95 Euros each. That’s 95.000,00 Euros! Now imagine launch going wrong. Some people will think that product will be bad too, since we are late. We are losing potential clients. But we have perfected routine for launch, that was repeated 100 times. Chance of failure is non-existant!

#### Reduces risk

My favourite benefit! I can sleep well because of this! For example, automated tests are guarantees that code is working as intended. Say we work on e-shop payment module. What if there is situation, when client will receive foods before paying? And he makes order for 5000 Euros. We better take care of all scenarios before it happens. And with automated tests, we can repeat those tests anytime. It is written proof, that everything works the way it should.

#### Improves productivity

There are things that we all hate to do. But we have to. Our businesses depend on it, but doesn’t make it grow. For example, I hate accounting. I want to focus on creating things that actually help people. To follow up, I don’t want to spend big amount of my time dealing with bureaucracy and providing my clients with invoices. I choose to pay small amount for system, that deals with it. Now I have more flexibility and I am more happy. I know it is taken care for me, so I can focus on things that make me happy!

#### Saves time

I can relate to this benefit mostly as developer. We tend to automate anything. In one conference, lecturer told “If this is third time you are doing something, you should automate it!” Imagine you want to show product to your client and you have to deploy it to public server. So, after every change you made, you have to do deploy it over and over again. Eventually, in course of the day, you wasted hour of time you could be creating awesome stuff!

### Conclusion

I believe anyone can take something out of this. Take a look at your day, your workflow.

What routines do you usually follow without putting your mind into it? It could be very good possibility for automation!

Write down a comment! Or if you like, you can contact me and we could talk about your case!

## Is it worth to set Eloquent fields explicitly?

You know, I love Eloquent so much! It provides very elegant way to link you database to models. And it does a lot of things for you too, for example:

• guessing table name by class name;
• guessing which field should be used for foreign keys.

Have you ever wondered, how much overhead do these things create? I did. And I figured it out with table name case (I guess should be similar with foreign key column guessing. Take a look at my experiment!

TL;DR: It is worth.

• 1
• 2