How implement Machine Learning algorithms with Python?
Python is also one of the most used programming languages in the world.
In fact, this language offers essential libraries for making statistics, for processing images or even for data analysis.
Furthermore, Python is characterized by having a simple but feature-rich syntax. Certainly being an interpreted language it has a lower execution speed but many of its modules have been developed in C language.
Recall that Machine Learning is a branch of Artificial Intelligence and studies algorithms that are able to process the input data and then be able to make predictions. Today, these algorithms are very useful if you think about the large amount of data that exists on the web and offer an alternative to traditional algorithms.
The development community around Machine Learning in Python is increasingly large and therefore it is easy to find information or tutorials to get explanations. The modules and libraries are in large quantities and we will see them in detail in the next paragraph.
So we just have to start studying and have fun putting into practice what we have learned!
Machine Learning with Python – What Tools Do You Need?
So what are the tools we need in Pytnon?
First of all, a development environment like Jupyter Notebook, very easy to download and install.
In fact, on the installation page you will find the instructions:
pip install notebook
To execute it, just type:
A module that allows you to do Machine Learning with Python is Numpy.
In fact, thanks to the many properties and methods for scientific calculation, it allows you to easily work with arrays and matrices.
It simply installs like this:
pip install numpy
In Linux systems it is necessary to write:
sudo apt-get install python3-numpy
The installation will not last long.
It is then used in Python scripts simply by writing:
One tool that allows you to easily manage DataFrame and Series is the Pandas library.
To install it simply write:
pip install pandas
It is then called in Python scripts like this:
Matplotlib e Seaborn
As a tool for machine learning in Python, the Matplotlib and Seaborn libraries cannot be missing, which allow you to graphically view the processed data.
Installation takes place by writing on the command line:
pip install matplotlib
and we import it as before using import:
pip install seaborn
To be able to use them then I write:
We then use import as usual:
The scikit-learn library implements Machine in Learning algorithms with Python.
Intuitively, the installation takes place as follows:
pip install scikit-learn
To use it, therefore, we import it:
The tensorflow library implements Machine in Learning and Deep Learning algorithms in Python.
pip install tensorflow
We then import the library to be able to use it for our Python scripts:
We have listed only some of the modules to do Machine Learning in Python, there are others that we will talk about later in the guide.
If you already have some basic Python I suggest you choose the module you don’t know, otherwise start with the basics. Here, then, is the link to the complete tutorial: Python language tutorial index.
Some useful links
Python lambda function
Use Python dictionaries
Today we often hear about Machine Learning and Artificial Intelligence. Machine Learning algorithms are currently used in various fields. For example, we find applications in online shopping, in interactions with social media, in financial services, in health care, in the marketing sector to manage targeted advertising, etc.
Therefore it is essential to know the term of Machine Learning and understand where it is applied.
Machine Learning definition
Machine Learning, abbreviated ML, is a branch of Artificial Intelligence. The basic idea is that robots (or more generally systems) can perform actions as if they were humans or animals.
Knowledge is acquired through machine learning programs, as with humans who acquire knowledge based on experience.
Machine Learning algorithms use mathematical-computational methods that analyze data thanks to which the construction of analytical models is automated.
Machine Learning example
After giving the definition of Machine Learning, let’s analyze some examples.
Let’s first look at this simple artificial intelligence program created on the Program the Future site: Artificial Intelligence for the sea.
After logging in at the time of the code, start the game which is based on the identification of waste by an Artificial Intelligence.
You will therefore be presented with an AI that must be trained. In fact, the Artificial Intelligence does not know if an object is a fish or a waste but we can give it instructions, labeling images or diagrams.
Each image can be labeled as fish or as waste. This acts as training and will then allow the Artificial Intelligence to recognize a refusal from a fish when new images are presented to it.
Machine Learning algorithms
We continue, in the context of Machine Learning, to give some other definitions by presenting the algorithms that determine the approach to a given problem.
The main types of algorithms are:
- Semi supervised
- For reinforcement
What is the difference? Simply in the way they acquire data to make predictions.
In this type of algorithm, which is the most used one of Machine Learning algorithms, learning is managed just like a child memorizes animals by looking at some example images. As in the example seen on Program the future, where Artificial Intelligence is educated with images.
The algortimo therefore learns from a labeled data set. For example, this can be used in marketing, identifying products that the user may like based on the images he or she has chosen in the past. It is therefore based on experience.
Among these algorithms we will study, later in the guide, the linear and logistic regression algorithms.
Unsupervised learning, on the other hand, is a Machine Learning alghoritm used on data that does not have a classification, that is, on non-labeled data.
Therefore, the right answer is not provided but the goal is to leave the algorithm to search for hidden characteristics or data.
The use of this algorithm occurs above all with transactional data. So, for example, the algorithm can identify users who have similar needs to address ad hoc marketing campaigns. In fact, by analyzing this data, for us human beings there may not be a sense that the algorithm is able to give.
Let’s take a purely explanatory example: the algorithm could identify that the population that buys more organic food needs less medical care (because for example they never buy drugs), therefore the algorithm could recommend a greater purchase of organic food, to safeguard health, to the people who need it most.
Semi Supervised Machine Learning
As we can easily guess, it is a ‘hybrid’ model that has the same applications as the supervised model, that is, a set of data is provided where certain inputs correspond to outputs, but also data with no output as in the unsupervised one.
An example of application is found for example in cameras that are able to identify a human face.
Machine Learning for Reinforcement
In this case we move on to a reinforcement learning according to which we learn from experiments and errors to reach a goal. The idea is to improve the functionality of the system by reinforcement, that is, the reward. Kind of like when you promise children candy if they reach a goal.
So when you reach the goal you have a reward, while if you don’t reach it, you don’t give anything or give you a punishment. The aim is to maximize the rewards by choosing the correct actions.
This type of approach is used in robotics and video games for example where a computer learns to beat an opponent based on the moves that have given him the most benefits previously.
Deep Learning algorithms are part of it.
In this lesson we talked about Machine Learning and we gave its definition by also talking about the different algorithms used in this sector. In future lessons I will continue to talk about this topic again.
Some useful links
Python lambda function
Use Python dictionaries