With over a decade of research and growth, artificial intelligence has started to show its promise. As a learner, this is probably the best possible time for you to learn AI. By 2021, $2.9 trillion of business will be generated by AI-enabled tools.
Artificial intelligence is used almost everywhere. The most common use-case is social media, where AI works behind the scene to learn your viewing habits and recommend content that is more likely to like, comment, and share. Artificial intelligence is also ideal for predicting the weather.
Have you heard about driverless cars? They are all powered by AI.
All of these bring us to a single point: learning AI. We are going to choose Python as the core tool to explore artificial intelligence. Python is easy to use a programming language that works wonders for beginners.
Artificial Intelligence can be best defined as a way of making machines intelligent. According to John McCarthy, it is defined as below.
“The science and engineering of making intelligent machines.”
Right now, there are three types of AI that you can find.
Artificial Narrow Intelligence is the most basic AI that is used to do basic tasks by machines. General AI, on the other hand, is more advanced and can do tasks that require human-level intelligence. The last type of intelligence is superintelligence, which can surpass humans.
But, will they be intelligent enough to take over the human race? Well, that’s the topic of another day. For now, you should know that AI is kind of bad as they fail to basic things in most cases. So, no need to worry as of now!
Python is a preferred programming language used by Data Scientists, Machine Learning experts, or anyone related to AI. The reason behind it being the top choice is as below.
Python being beginners friendly gives you the ability to stay at pace with the artificial intelligence concepts. Always remember, Python is just a tool that you are using to execute your AI projects. The minimalist language enables you to focus on getting the solution done, rather than fiddle with the problems associated with it.
Artificial Intelligence consists of two subset technologies, i.e., Machine Learning and Deep Learning.
Machine Learning deals with making machines smarter by feeding them data. Deep learning, on the other hand, is used to solve complex problems using neural networks.
In this beginner’s guide, we are going to see a machine learning example with Python.
Before we get started, let’s look at some of the popular and useful Python libraries for AI. It is a good idea to explore the libraries to make the most out of Python’s ecosystem.
Before we try to code our small project, we first need to learn more about machine learning. Machine learning is a three-step process. They are as below.
Machine learning can also be divided into three types, including supervised learning, unsupervised learning, and reinforcement learning.
One last thing that you need to know about machine learning is the type of problems it can solve.
There is a seven-step process that you need to follow if you are trying to solve a problem using Machine Learning. In the end, you need to create a predictive model that solves the problem at hand.
The seven steps required for Machine Learning Process are as follows:
As a learner, you do not have to re-invent the wheel when solving problems. There are many Machine Learning algorithms.
There are some prerequisites before you start machine learning with Python. The first step is to visit your math skills. Artificial intelligence is heavily dependent on math, especially the statistics field. However, there is no need to redo everything about maths. In short, you need to cover the following:
You also need to learn Python syntax basics. However, you do not have to become a Python expert before you start your journey. Any basic online course on Python will do. We suggest checking out Coursera and Udacity for free python courses.
You can also read books for learning purposes. We suggest reading Automate The Boring Stuff With Python. It is a great book that covers everything you need to know about Python for using it in your AI projects.
Now that our basics are clear and we now have a clear understanding of AI, let’s try to solve a problem statement using Python programming language.
Problem: Find out if there will be rain tomorrow or not
Problem Type: Classification (as there can be only two answers, Yes or NO)
Solution: The solution is to build a prediction model that can predict whether the rain will occur tomorrow or not.
The first step is to import the required libraries for our machine learning process. For now, we currently need linear algebra and data processing. To do so, we need to import NumPy and pandas library.
# For Linear Algebra → import numpy as np # For data processing → import panda as pd
These two lines of code will import the necessary libraries.
Now, we need to load the data set.
You should be able to get the dataset from any of the available online weather repositories. Let’s call the data set file, “localweather.csv” for easy reference.
It’s now time to load the data set. To do so, you need to run the following command.
→ pv = pd.read_csv(.../filelocation/localweather.csv) → print ('The data set size is: ', pv.shape) → print(pv[0:10])
The first line is pretty obvious. We simply load the data set into a variable known as “pv.”
Next, we list the data set site with the help of pv.shape property.
The third line aims to print the first 10 entries in the data set.
Now, we need to process the data so that we can remove the data/variables that are not required or are non-significant. For example, there can be some columns that are empty or do not have proper data. By doing data processing, we remove them and filter our data.
→ print (pv.count().sort_values()) → pv = pv.dropna(how=’any’) //removes null values
You can also opt to remove the unnecessary variable in the data set. By doing so, you will make computations faster and save time and energy. In the localweather.csv dataset, you will find two unnecessary variables, including “location” and “type” variable. If you see any other unnecessary variable, then you can do so by using the pv.drop()
pv = pv.drop(columns=[‘variable1’, ‘variable2’,’variable3’’])
You can change the variable1, variable2, and variable3 to the variables that you want to remove.
Once the trimming is done, do not forget to drop null values again.
You may also want to get rid of the outliers. They are values that simply do not fit in the data set. To remove them, you need to use the abs() method in the NumPy library. The following four-line of code will get rid of the outliers for you.
→ from scipy import stats → ab = np.abs(stats.zscore(pv._get_numeric_data())) → pv = pv[(ab<3).all(axis=1)]
Finally, we will change the value of Yes to ‘1’ and No to ’0’ so that it will become easy for us to work with the data set.
→ pv[‘RainToday’].replace({‘No’: 0,’Yes’: 1}, inplane = True) → pv[‘RainTommorrow’].replace({‘No’:0, ‘Yes’: 1}, inplace = True)
In this step, we are going to find the variables that can have a significant outcome when it comes to solving the problem.
→ from sklearn.feature_selection import SelectKBest, chi2 → a = df.loc[:df.columns!=’RainTommorrow’] → b = df[[‘RainTommorrow’]]
We first import the needed libraries. Next, we store the columns that have RainTommorrow and !RainTommorrow. They are stored in ‘b’ and ‘a’ respectively.
Now, let’s run variables through the SelectKBest function.
→ sit = SelectKBest(chi2, k = 3) → sit.fit(a,b) → a_new = sit.transform(a)
Once you print the ‘a_new’ variable, you will be able to see three preditor variables, i.e., Rainfall, Humidity, and RainToday. The Humidity variable can be anything depending on your data set. This means that your predictor variable can be Humity6AM or Humity5PM. It all depends on the dataset you are using.
The last step is to build a Machine Learning Model. You can the classification model by using any one of the four algorithms mentioned below.
We suggest you research your way on how to use the algorithms as they are beyond the scope of the article.
This leads us to the end of our beginner’s guide to artificial intelligence with python. From here, it is best to learn a deep learning basis and try to implement your learning by doing practical projects.