Unit-III Advanced Machine Learning
Unit-III Advanced Machine Learning
Machine learning algorithms are a subset of artificial intelligence (AI) that imitates the
human learning process.
Machine learning algorithms develop multiple models (usually using multiple datasets) and
each model is analogous to an experience
Machine learns with respect to a particular task T, performance metric P following experience
E, if the system reliably improves its performance P at task T following experience E.
The major difference between statistical learning and machine learning is that statistical
learning depends heavily on validation of model assumptions and hypothesis testing, whereas
the objective of machine learning is to improve prediction accuracy
Supervised Learning: In supervised learning, the datasets have the values of input variables
(feature values) and the corresponding outcome variable. The algorithms learn from the
training dataset and predict the outcome variable for a new record with values of input
variables.Ex:-Regression
Unsupervised Learning: The datasets will have only input variable values, but not the output.
The algorithm learns the structure in the inputs. Ex:- Clustering
How Machines Learn?
In supervised learning, the algorithm learns using a function called loss function, cost function
or error function, which is a function of predicted output and the desired output. If h(Xi ) is the
predicted output and yi is the desired output, then the loss function is
where n is the total number of records for which the predictions are made. The function sum of
squared error (SSE) is the loss function for a regression model.
Machine learning uses optimization algorithms which can be used for minimizing the loss
function. Most widely used optimization technique is called the Gradient Descent.
GRADIENT DESCENT ALGORITHM
Gradient descent (GD) algorithm can be used for estimating the values of regression parameters,
given a dataset with inputs and outputs.
The functional form of a simple linear regression model is given by
The values of B0 and B1 at the minimum cost points are best estimates of the model parameters
Developing a Gradient Descent Algorithm for Linear Regression Model
To implement the GD algorithm using the dataset Advertising.csv
The dataset has the following elements:
1. TV – Spend on TV advertisements
2. Radio – Spend on radio advertisements
3. Newspaper – Spend on newspaper advertisements
4. Sales – Sales revenue generated
Loading the Dataset
Load the dataset using Pandas’ library.
import pandas as pd
import numpy as np
import warnings warnings.
filterwarnings(‘ignore’)
sales_df = pd.read_csv(‘Advertising.csv’)
# Printing first few records
sales_df.head()
Set X and Y Variables
X = sales_df[[‘TV’, ‘Radio’, ‘Newspaper’]]
Y = sales_df[‘Sales’]
Standardize X and Y
It is important to convert all variables into one scale.
Y = np.array( (Y - Y.mean() ) / Y.std() )
X = X.apply( lambda rec: ( rec - rec.mean() ) / rec.std(), axis = 0 )
where α is the learning parameter that decides the magnitude of the update to be done to the bias and
weights
Finding the Optimal Bias and Weights
The updates to the bias and weights need to be done iteratively, until the cost is minimum. It can take
several iterations and is time-consuming. There are two approaches to stop the iterations:
1. Run a fixed number of iterations and use the bias and weights as optimal values at the end these
iterations.
2. 2. Run iterations until the change in cost is small, that is, less than a predefined value (e.g., 0.001).
plot the cost aer every iteration for dierent learning rate parameters (alpha values).
plt.plot( alpha_df_1[‘iteration’], alpha_df_1[‘cost’], label = “alpha = 0.01” );
plt.plot( alpha_df_2[‘iteration’], alpha_df_2[‘cost’], label = “alpha = 0.001”);
The plot in Figure shows that the learning is faster for alpha value 0.01 compared to 0.001. For smaller
values, the learning could be slower whereas higher learning rate could lead to skipping the minima of
cost function. It is imperative to search for the optimal learning parameter.
The scikit-learn library in Python, which is primarily an open-source Python library for building machine
learning models. scikit-learn provides a comprehensive set of algorithms for the following kind of
problems:
1. Regression
2. Classification
3. Clustering
scikit-learn also provides an extensive set of methods for data pre-processing and feature selection