Machine Learning Algorithms


Machine Learning Algorithms For Beginners with Code Examples in Python


Machine learning (ML) is rapidly changing the world, from diverse kinds of applications and research pursued in industry and the scholarly community. Machine learning is influencing every part of our day by day lives. From voice associates utilizing NLP and machine learning to make arrangements, check our calendar and play music, to programmatic advertisements — that are precise to such an extent that they can predict what we will require before we even consider it.

More frequently than not, the complexity of the logical field of machine learning can be overwhelming, causing keeping to up with “what is important” a very testing task. However, to ensure that we provide a learning way to the individuals who try to learn machine learning, yet are new to these ideas. In this article, we take a gander at the most critical essential algorithms that ideally make your machine learning journey less testing.


  • Introduction to Machine Learning.
  • Major Machine Learning Algorithms.
  • Supervised vs. Unsupervised Learning.
  • Linear Regression.
  • Multivariable Linear Regression.
  • Polynomial Regression.
  • Exponential Regression.
  • Sinusoidal Regression.
  • Logarithmic Regression.

What is machine learning?

Machine learning acts similarly to the growth of a kid. As a youngster grows, her experience E in performing task T increases, which results in higher performance measure(P).

For example, we give a “shape sorting block” toy to a youngster. (Presently we as a whole realize that in this toy, we have different shapes and shape gaps). For this situation, our undertaking T is to locate an appropriate shape opening for a shape. Afterward, the kid observes the shape and tries to fit it in a molded gap. Let us state that this toy has three shapes: a circle, a triangle, and a square. In her first endeavor at finding a formed gap, her performance measure(P) is 1/3, which implies that the youngster discovered 1 out of 3 correct shape openings.

Second, the youngster tries it another time and notification that she is a little experienced in this assignment. Considering the experience picked up (E), the youngster tries this errand another time, and when measuring the performance(P), it turns out to be 2/3. After repeating this assignment (T) multiple times, the child presently figured out which shape goes into which shape opening.

So her experience (E) increased, her performance(P) additionally increased, and afterward we notice that as the number of endeavors at this toy increases. The performance likewise increases, which results in higher accuracy.

Such execution is similar to machine learning. What a machine does is, it takes an assignment (T), executes it, and measures its performance (P). Presently a machine has a large number of data, so as it processes that data, its experience (E) increases over time, resulting in a higher performance measure (P). So after experiencing all the data, our machine learning model’s accuracy increases, which implies that the predictions made by our model will be very accurate.

Let us try to understand this definition: It states “learn without being explicitly programmed” — which implies that we won’t show the computer with a particular set of rules, however rather, what we will do is feed the computer with enough data and give it an opportunity to learn from it, by committing its own errors and improve upon those. For instance, We didn’t show the youngster how to fit in the shapes, however by performing a similar assignment multiple times, the kid learned to fit the shapes in the toy without anyone else.

Therefore, we can say that we didn’t explicitly show the kid how to fit the shapes. We do something very similar with machines. We give it enough data to work on and feed it with the information we need from it. So it processes the data and predicts the data accurately.

Why do we need machine learning?

For instance, we have a set of images of cats and dogs. What we want to do is classify them into a group of cats and dogs. To do that we need to find out different animal features, such as:

  1. How many eyes does each animal have?
  2. What is the eye color of each animal?
  3. What is the height of each animal?
  4. What is the weight of each animal?
  5. What does each animal generally eat?

We form a vector on each of these questions’ answers. Next, we apply a set of rules such as:

If height > 1 feet and weight > 15 lbs, then it could be a cat.

Presently, we need to make such a set of rules for every data point. Furthermore, we place a choice tree of if, else if, else explanations and register whether it falls with one of the categories.

Let us expect that the result of this experiment was not fruitful as it misclassified a significant number of the creatures, which gives us a fantastic opportunity to utilize machine learning.

What machine learning does is process the data with different sorts of algorithms and reveals to us which feature is more important to determine whether it is a feline or a canine. So as opposed to applying numerous sets of rules, we can streamline it dependent on a few features, and as a result, it gives us a higher accuracy. The previous strategy was not generalized enough to make predictions.

Machine learning models helps us in many tasks, such as:

  • Object Recognition
  • Summarization
  • Prediction
  • Classification
  • Clustering
  • Recommender systems
  • And others

What is a machine learning model?

A machine learning model is an inquiry/answering framework that deals with processing machine-learning related assignments. Consider it an algorithm framework that represents data when taking care of problems. The strategies we will handle underneath are useful for industry-related purposes to handle business problems.

For example, let us envision that we are working on Google Adwords’ ML framework, and our assignment is to actualizing a ML algorithm to pass on a particular demographic or area utilizing data. Such an errand intends to go from utilizing data to gather significant bits of knowledge to improve business results.

Major Machine Learning Algorithms:

1. Regression (Prediction)

We use regression algorithms for predicting continuous values.

Regression algorithms:

  • Linear Regression
  • Polynomial Regression
  • Exponential Regression
  • Logistic Regression
  • Logarithmic Regression

2. Classification

We use classification algorithms for predicting a set of items’ class or category.

Classification algorithms:

  • K-Nearest Neighbors
  • Decision Trees
  • Random Forest
  • Support Vector Machine
  • Naive Bayes

3. Clustering

We use clustering algorithms for summarization or to structure data.

Clustering algorithms:

  • K-means
  • Mean Shift
  • Hierarchical

4. Association

We use association algorithms for associating co-occurring items or events.

Association algorithms:

  • Apriori

5. Anomaly Detection

We use anomaly detection for discovering abnormal activities and unusual cases like fraud detection.

6. Sequence Pattern Mining

We use sequential pattern mining for predicting the next data events between data examples in a sequence.

7. Dimensionality Reduction

We use dimensionality reduction for reducing the size of data to extract only useful features from a dataset.

8. Recommendation Systems

We use recommenders algorithms to build recommendation engines.


  • Netflix recommendation system.
  • A book recommendation system.
  • A product recommendation system on Amazon.

Nowadays, we hear many buzz words like artificial intelligence, machine learning, deep learning, and others.

What are the fundamental differences between Artificial Intelligence, Machine Learning, and Deep Learning?

Artificial Intelligence (AI):

Artificial intelligence (AI), as defined by Professor Andrew Moore, is the science and engineering of making computers behave in ways that, until recently, we thought required human intelligence [4].

These include:

  • Computer Vision
  • Language Processing
  • Creativity
  • Summarization

Machine Learning (ML):

As defined by Professor Tom Mitchell, machine learning refers to a scientific branch of AI, which focuses on the study of computer algorithms that allow computer programs to automatically improve through experience [3].

These include:

  • Classification
  • Neural Network
  • Clustering

Deep Learning:

Deep learning is a subset of machine learning where layered neural networks, combined with high computing power and large datasets, can create powerful machine learning models.

Why do we prefer Python to implement machine learning algorithms?

Python is a popular and general-purpose programming language. We can write machine learning algorithms utilizing Python, and it works well. The reason why Python is so popular among data researchers is that Python has a diverse variety of modules and libraries already executed that make our life more comfortable.

Let us have a brief look at some exciting Python libraries.

  1. Numpy: It is a math library to work with n-dimensional arrays in Python. It enables us to do computations effectively and efficiently.
  2. Scipy: It is a collection of numerical algorithms and domain-specific tool-box, including signal processing, optimization, statistics, and much more. Scipy is a functional library for scientific and high-performance computations.
  3. Matplotlib: It is a trendy plotting package that provides 2D plotting as well as 3D plotting.
  4. Scikit-learn: It is a free machine learning library for python programming language. It has most of the classification, regression, and clustering algorithms, and works with Python numerical libraries such as Numpy, Scipy.

Machine learning algorithms classify into two groups :

  • Supervised Learning algorithms
  • Unsupervised Learning algorithms

I. Supervised Learning Algorithms:

Supervised learning is a branch of machine learning(perhaps it is the mainstream of machine/deep learning for now) related to inferring a capacity from named training data. Training data comprises of a set of *(input, target)* pairs, where the info could be a vector of features, and the target instructs what we desire for the capacity to yield. Contingent upon the kind of the *target*, we can roughly separate supervised learning into two categories: characterization and regression. Arrangement includes categorical targets; models ranging from some straightforward cases, for example, picture order, to some serious subjects, for example, machine translations and picture inscription. Regression includes nonstop targets. Its applications incorporate stock prediction, picture veiling, and others-which all fall in this category.

To understand what supervised learning is, we will utilize a model. For example, we give a kid 100 toys in which there are ten creatures of every sort like ten lions, ten monkeys, ten elephants, and others. Next, we train the child to recognize the different sorts of creatures dependent on different characteristics (features) of a creature. For example, in the event that its color is orange, at that point it may be a lion. In the event that it is a big creature with a trunk, at that point it might be an elephant.

We show the child how to differentiate creatures, this can be a case of supervised learning. Presently when we give the child different creatures, he ought to have the option to order them into an appropriate creature group.

For the purpose of this model, we notice that 8/10 of his characterizations were correct. So we can say that the child has done a pretty great job. The equivalent applies to computers. We provide them with a huge number of data focuses with its real named values (Labeled data is characterized data into different groups alongside its feature values). At that point it learns from its different characteristics in its training period. After the training period is over, we can utilize our trained model to make predictions. Remember that we already took care of the machine with named data, so its prediction algorithm depends on supervised learning. In short, we can say that the predictions by this model depend on named data.

Example of supervised learning algorithms :

  • Linear Regression
  • Logistic Regression
  • K-Nearest Neighbors
  • Decision Tree
  • Random Forest
  • Support Vector Machine

II. Unsupervised Learning:

In contrast to supervised learning. Unsupervised learning infers from unlabeled data, a capacity that describes shrouded structures in data.

Perhaps the most fundamental kind of unsupervised learning is measurement reduction techniques, for example, PCA, t-SNE, while PCA is generally utilized in data preprocessing, and t-SNE normally utilized in data visualization.

A more progressed branch is clustering, which explores the concealed patterns in data and afterward makes predictions on them; models incorporate K-mean clustering, Gaussian mixture models, shrouded Markov models, and others.

Alongside the renaissance of deep learning, unsupervised learning acquires and more consideration since it frees us from physically naming data. Considering deep learning, we consider two sorts of unsupervised learning: representation learning and generative models.

Representation learning plans to distil an elevated level representative feature that is helpful for some downstream undertakings, while generative models expect to reproduce the information data from some concealed parameters.

Unsupervised learning works as it sounds. In this sort of algorithms, we don’t have marked data. So the machine needs to process the information data and try to make decisions about the yield. For instance, remember the child whom we gave a shape toy? For this situation, he would learn from its own errors to locate the perfect shape opening for different shapes.

Yet, the catch is that we are not taking care of the kid by showing the strategies to fit the shapes (for machine learning purposes called named data). However, the youngster learns from the toy’s different characteristics and tries to make decisions about them. In short, the predictions depend on unlabeled data.

Examples of unsupervised learning algorithms:

  • Dimension Reduction
  • Density Estimation
  • Market Basket Analysis
  • Generative adversarial networks (GANs)
  • Clustering

1. Linear Regression:

Linear regression algorithm graph

Linear regression is a factual approach that models the relationship between input features and yield. The information features are known as the free variables, and the yield is known as a reliant variable. Our objective here is to predict the estimation of the yield dependent on the information features by duplicating it with its ideal coefficients.

Some real-life examples of linear regression :

(1) To predict sales of products.

(2) To predict economic growth.

(3) To predict petroleum prices.

(4) To predict the emission of a new car.

(5) Impact of GPA on college admissions.

There are two types of linear regression :

  1. Simple Linear Regression
  2. Multivariable Linear Regression

1.1 Simple Linear Regression:

In simple linear regression, we predict the output/dependent variable based on only one input feature. The simple linear regression is given by:

Linear regression equation.

Step by step implementation in Python:

a. Import required libraries:

Since we are going to use various libraries for calculations, we need to import them.

Image for post

Source: Image created by the author.

b. Read the CSV file:

We check the first five rows of our dataset. In this case, we are using a vehicle model dataset — please check out the dataset on Softlayer IBM.

Image for post

Source: Image created by the author.

c. Select the features we want to consider in predicting values:

Here our goal is to predict the value of “co2 emissions” from the value of “engine size” in our dataset.

Image for post

Source: Image created by the author.

d. Plot the data:

We can visualize our data on a scatter plot.

Image for post

e. Divide the data into training and testing data:
To check the accuracy of a model, we will separate our data into training and testing datasets. We will utilize training data to train our model, and afterward we will check the accuracy of our model utilizing the testing dataset.
Image for post

f. Training our model:

Here is how we can train our model and find the coefficients for our best-fit regression line.

Image for post

g. Plot the best fit line:

Based on the coefficients, we can plot the best fit line for our dataset.

Image for post

Data plot for linear regression based on its coefficients | Source: Image created by the author.

h. Prediction function:

We are going to use a prediction function for our testing dataset.

Image for post

i. Predicting co2 emissions:

Predicting the values of co2 emissions based on the regression line.

Image for post

1.2 Multivariable Linear Regression:

In basic linear regression, we were just ready to consider one info feature for predicting the estimation of the yield feature. However, in Multivariable Linear Regression, we can predict the yield dependent on more than one information feature. Here is the formula for multivariable linear regression.

Image for post

1.3 Polynomial Regression:

Image for post

Some of the time we have data that doesn’t merely follow a linear trend. We now and then have data that follows a polynomial trend. Therefore, we will utilize polynomial regression.

Before delving into its execution, we have to know how the graphs of some primary polynomial data look.

 Sinusoidal Regression:

Image for post

Some real-life examples of sinusoidal regression:

  1. Generation of music waves.
  2. Sound travels in waves.
  3. Trigonometric functions in constructions.
  4. Used in space flights.
  5. GPS location calculations.
  6. Architecture.
  7. Electrical current.
  8. Radio broadcasting.
  9. Low and high tides of the ocean.
  10. Buildings.

Sometimes we have data that shows patterns like a sine wave. Therefore, in such case scenarios, we use a sinusoidal regression. Below we can show the formula for the algorithm:

Image for post

Logarithmic Regression:

Image for post

Some real-life examples of logarithmic growth:

  1. The magnitude of earthquakes.
  2. The intensity of sound.
  3. The acidity of a solution.
  4. The pH level of solutions.
  5. Yields of chemical reactions.
  6. Production of goods.
  7. Growth of infants.
  8. A COVID-19 graph.