Blog

ProjectBlog: A Fun and Super Easy introduction to Artificial Intelligence and Machine Learning

Blog: A Fun and Super Easy introduction to Artificial Intelligence and Machine Learning


After reading this article you will understand the core of AI!

Welcome aboard friend, my name is David, and I’ll try to explain a very difficult thing in a different way.

It’s not that difficult, I’m trying to look interesting here.

I am a computer engineer, and even after years of coding and studying mathematics, Artificial Intelligence seemed like the most difficult thing in the entire universe.

Many professors are more interested into looking intelligent than in explaining with simple words.

That’s why I created a gamification startup to train developers.

But that’s another story.

Let’s dive into Artificial Intelligence right away before you think I want to tell you the story of my life.

Spoiler: at the end you can code your first Machine Learning program.


Concepts

Artificial Intelligence is the intelligence demonstrated by machines.

There are two types of AI:

Strong AI: a machine that exhibits behavior at least as skillful and flexible as humans do.

Weak AI: is artificial intelligence that is focused on one narrow task.

Terminator uses Strong AI. Facebook Algorithm uses Weak AI.

These are the main concepts of AI:

Machine Learning

This is the study of algorithms that computers use to perform a specific task without using explicit instructions.

In easy words: Machine Learning is to train a computer with many examples, so it can predict new samples. Like when the Instagram algorithm is trying to predict things you like based on your likes.

Training a machine to recognize fruits

Deep Learning

A subset of Machine Learning. Used for computer vision, speech recognition, natural language processing and others.

Natural Language Processing

The process of “understanding” natural human language. The use of various common techniques in order to construct a model as accurate as possible of what was said and what those words correspond to.

Like the algorithm I wanted to create to understand my ex girlfriend.

It wasn’t posible.

Artificial Neural Networks

Artificial Neural Networks are computing systems vaguely inspired by the biological neural networks that constitute animal brains.

We’ll see how they work later. I promise, I’m not a bad politician.

Other fields:

Image Processing, Pattern Recognition, some Data Science algorithms.


Machine Learning

Machine Learning is basically taking n samples to predict new ones.

There are two categories of Machine Learning problems:

Supervised Learning

The samples have features and labels.

For example, imagine we have a computer program trying to predict the type of vehicle in an image.

The computer can extract two different features from the images:

1- The number of wheels of the vehicle

2- The answer to whether the vehicle has windows or not

Including arrows in case you can’t see the windows or the wheels.

These are some samples to train the computer:

With that information, using statistics, the computer now can predict that (4,Yes) is a car, or that (1,No) is probably a Motorcycle.

Unsupervised Learning

In this case, the computer input is a set of samples, but without labels. Now we want the computer to predict the possible groups.

If we use the same example of Cars and Motorcycles, now we are trying the computer to understand that there are two different groups of objects with different features.

The algorithm can identify group 1 and group 2.

Trying to say: there seem to be two groups, with different features.

This is exactly like when you say: I do not know why, but I don’t like that guy.

Neural Networks

A Neural Network is a graph with nodes. As the neurons in a brain:

Neural Networks have three layers: the input, the hidden layer (or layers) and the output.

Neural Networks are a technique for supervised learning.

Let’s imagine we are trying to identify a hand written number.

We can use the Neural Network to run a lot of examples of hand written numbers and their labels (5,3,1,etc).

For the computer, every image is just a set of pixels. Imagine you translate every pixel to a number from 0 to 1, according to the amount of black.

0 is white, 1 is black.

In an extremely short summary, what the Neural Network does is to load every pixel of the image in the input layer (784 pixels in this example). And try to predict the output in the output layer (0 to 9, the hand written number).

How? Imagine the connections between nodes as knobs that can change to any number between 0 and 1.

With your samples, the network will change many times those knobs until having the best prediction machine for hand written numbers.

Ths will be the proccess:

  • Load one sample (an image of a hand written number)
  • Run the network (multiplying the number of every pixel by the numbers in the “knobs”) following the paths.
  • Test the result (The result will be multiple numbers (between 0 and 1) in the output layer, meaning the probability of that number to be the one in the image)

In this example, the Neural Network is saying we have a probability of 0.8 (80%) of having a 2 in the image.

And how do you know you have good values in the knobs? Because you know the real label, the real written number, if the network is not giving a good result, change the knobs values and start again.

Yes… that’s practically brute force. But, mathematicians found efficient ways to train that network, so it works.


Let’s play with Machine Learning

Do you want to practice your new skills? We can start with an easy example so you can create your first Machine Learning program.

Pears vs. Apples

We will create a program that will recognize pears and apples.

For this, we will be using the language Python and a library called sklearn.

You can use Python 3 Trinkets to run your program.

First of all, delete the default program.

Now, we will import a Decision Tree from sklearn. A Decision Tree is a structure created to train the computer.

What we will do is to introduce a series of examples of pears and apples, and then predict whether a new sample is a pear or an apple.

The Decision Tree will store the information after the training.

Sklearn has pre-programed many of the structures that we will use with Machine Learning.

Now, let’s input some features to train the machine.

Our features have this form:

[weight,roundness]

[150,1] is 150 grams and “yes, it is round”

[160,0] is 160 grams and “no, it’s not round”

Imagine we have these 4 features:

features = [[150,1],[170,1],[140,0],[130,0]]

Our labels have this form:

0 = pear

1 = apple

And now, we have these 4 labels:

labels = [1,1,0,0]

Which means that the first feature [150,1] is an apple.

Now, we are going to create the actual tree. Just calling a function (sklearn) and assign it to the variable clf.

clf = tree.DecisionTreeClassifier()

Now, the most important part, the actual training.

We will use a function of the tree, with two inputs: the features and the labels. The function fit.

clf = clf.fit(features,labels)

Adding up everything, this will be your code:

But what was that last line?

Magic.

We are printing a prediction.

Our tree is ready and trained. And we are trying to predict the label of a new sample [134,0].

Now, you can run your program here and see the result.

The result should be [0], the tree is predicting our sample is a pear.

But why?

Just check the features.

features = [[150,1],[170,1],[140,0],[130,0]]

labels = [1,1,0,0]

Apples seem to have more grams (150, 170) than pears (140, 130). Also, Apples are round (1), and pears aren’t (0).

[134,0] looks exactly like a pear.

Now try with a new sample:

print( clf.predict([[163,1]]) )

Does the tree says it is an apple?


Framework

Via UpX Academy

When dealing with Machine Learning problems you must follow the approach of this framework:

1- Get Data

This is one of the most difficult tasks. A problem can be solved with Machine Learning only if we have the data to train the computer.

2- Clean, Prepare & Manipulate Data

Look for errors in the data, delete unnecessary information for your problem, change the way it is presented.

3- Train Model

Select your Classifier and train it, the best here is to use a tool provided by a library.

4- Test Data

With your model trained, you can use your libraries to run hundreds of tests in seconds. The libraries even plot statistics about how accurate is your model.

5- Improve

Either with better data or with different classifiers, your model can be improved to be more accurate.

Next Steps

Now, you can try with more features, different examples or different classifiers (including Neural Networks).

I’ll launch a gamified free course on Artificial Intelligence next week, if you are interested please sign up here.

These are the essential libraries for Machine Learning:

Scikit-learn

Tensorflow

Matplotlib

Seaborn

Pandas

NumPy

SciPy

Now you can understand the core of all of those libraries, so you are free to learn and play with them.

Source: Artificial Intelligence on Medium

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
a

Display your work in a bold & confident manner. Sometimes it’s easy for your creativity to stand out from the crowd.

Social