Into the world of Artificial Intelligence (AI)
Artificial intelligence or in simpler words the ability of an inanimate object to possess human like intelligence. The origins of Artificial intelligence can be traced back as early as 1956, at the Dartmouth conference and it was at this conference the name “Artificial Intelligence” was formally adopted.
The wondrous world from the discovery of AI that has been opened to us allowing us infinite possibilities, granted not all good possibilities, however with such possibilities have come advancements in any field that AI has touched, i.e. Healthcare, Marketing, Human resources, Education, etc.
How does AI work?
Now you’re probably expecting a very difficult answer like most other articles on AI however, I’ll try to explain a very difficult thing in a different way. Let’s dive right into the working of Artificial Intelligence before you think I want to tell you the story of my life.
Spoiler: by the end of this article you’ll be able to code your very first machine learning program.
As said above, 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 or a specific task.
Example of Strong and Weak AI
These are the main concepts of Artificial Intelligence:
This is the study of algorithms that computers use to perform a specific task without using explicit instructions.
Or in simpler 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
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.
A subset of Machine Learning. Used for computer vision, speech recognition, natural language processing and others.
Artificial Neural Networks
Artificial Neural Networks are computing systems vaguely inspired by the biological neural networks that constitute animal brains.
Image Processing, Pattern Recognition, some Data Science algorithms.
. . . .
Machine Learning is basically taking n samples to predict new ones.
There are two categories of Machine Learning problems:
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:
- The number of wheels of the vehicle
- The answer to whether the vehicle has windows or not
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.
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.
This algorithm can identify between group 1 and group 2.
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 as many times as those knobs until having the best prediction machine for hand written numbers.
This will be the process:
- 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.
. . . .
How to use memory 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:
[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? 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 and see the result.
The result should be , the tree is predicting our sample is a pear.
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?
When dealing with Machine Learning problems you must follow the approach of this framework:
2. 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.
Either with better data or with different classifiers, your model can be improved to be more accurate.
Now, you can try with more features, different examples or different classifiers (including Neural Networks).
These are the essential libraries for Machine Learning:
Now you can understand the core of all of those libraries, so you are free to learn and play with them.
Written by: Abubaker Sattar