×

Feature Extraction in Machine Learning

With the advancement of technology in the databases, everyone can store a vast amount of data with hundreds of thousands of features these days. Features contain information about the dependent or target variable. So, we may assume that a more significant number of features means more details on the target variable. But this may not be the case all the time. Because the datasets consist of relevant, irrelevant, and correlated features. While building a machine learning model with datasets which consist of a massive number of features, it becomes complicated for us to visualize and analyze. It also consumes a lot of time and memory, increasing the model's time and space complexity. Sometimes, the model may perform poorly on the testing data because of the irrelevant features in the dataset.

So, here comes the importance of Feature Reduction in Machine Learning. Feature reduction is a data pre-processing technique.

Feature reduction is reducing the number of features available in the dataset. This feature reduction is classified into the following types. They are,

  • Feature Selection
  • Feature Extraction

In this tutorial, let us discuss about Feature extraction in detail.

Note:

One of the important terms that are often used in Machine Learning is dimensionality.

Dimensionality is equal to the number of features in a dataset.

A dataset with three features can be visualized in a three-dimensional plane.

Feature reduction is also known as dimensionality reduction.

Feature Extraction:

Feature extraction is taking/considering the features from the given dataset and mapping it to a lower dimensional set in which each feature is obtained as a function of the original feature set.

Let us assume that initially, there are ‘m’ number of features in the original dataset. Now the number features say ‘n’ in the lower dimensional set must be less than the value of ‘m’.

Feature Extraction in Machine Learning

In general, feature extraction is the process of establishing a relation among several features to get a new feature. 

For example, let us consider an IPL dataset which consists of three features MatchesPlayed, RunsScored, and BallsFaced. Now the value of m is three. Here we can establish a relation between RunsScored, BallsFaced to obtain a new feature known as StrikeRate, which is given by the formula,

StrikeRate = RunsScored/BallsFaced   

Now the number of features has become two (i.e., n=2), which is less than m.

Some of the characteristics that we expect from the new features are,

  • When we establish a relation among several features, the derived new feature should be uncorrelated and cannot be reduced further.
  • The new features must have larger variances because you want the features to be able to distinguish between different instances.

Advantages of Feature Extraction:

  • Improvement in accuracy of our model
  • No redundancy in the data
  • Better visualization of data
  • Reduces the risk of overfitting
  • Increase in speed of training our model by reducing time complexity
  • Reduction in the computation of our model.
  • Memory management

Practical Applications of Feature Extraction:

  • Feature extraction is commonly used in Machine Learning while dealing with a dataset which consists of a massive number of features.
  • In Natural language Processing (NLP), feature extraction is used to identify specific keywords based on their frequency of occurrence in a sentence or a file.
  • Feature extraction is also used in the field of Image Processing in order to detect some features of a particular image.

Principal Component Analysis:

Principal Component Analysis, shortly PCA, plays a key role in feature extraction.

In PCA, we take our original dataset as input and try to establish a relation among certain features or combine certain features to create a new feature ensuring that no information is lost during the process.

In PCA, our data is projected into a set of orthogonal axes and drawn, some principal components and select one with minor information loss. Let us understand this concept with an example.

Consider a dataset with two features (Gene1, Gene2) and scatter the data points in a two-dimensional plane.

Feature Extraction in Machine Learning

Now let us try to draw two components in the graph (you can draw any number of components) and project these datapoints onto the components. We select the best component which has lower information loss.

In PCA, all the principal components drawn will be perpendicular to each other, as shown in the above figure.

Implementation of PCA on a sklearn Dataset:

The steps involved in implementing PCA in Machine Learning are

  • Importing the required libraries
  • Loading the dataset
  • Analyzing the dataset
  • Creating a dataframe
  • Scaling all the values in all columns
  • Transforming all the columns
  • Implementing PCA
  • Visualization

Code:

#importing required libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#loading the data
from sklearn.datasets import load_wine
wine = load_wine()
#analyzing the dataset
wine.keys() 

Output:

dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names'])

Input:

#creating a dataframe
df = pd.DataFrame(wine['data'],columns = wine['feature_names'])	
df.head(10)

Output:

alcoholmalic_acidashalcalinity_of_ashmagnesiumtotal_phenolsflavanoidsnonflavanoid_phenolsproanthocyaninscolor_intensityhueod280/od315_of_diluted_winesproline
014.231.712.4315.6127.02.803.060.282.295.641.043.921065.0
113.201.782.1411.2100.02.652.760.261.284.381.053.401050.0
213.162.362.6718.6101.02.803.240.302.815.681.033.171185.0
314.371.952.5016.8113.03.853.490.242.187.800.863.451480.0
413.242.592.8721.0118.02.802.690.391.824.321.042.93735.0
514.201.762.4515.2112.03.273.390.341.976.751.052.851450.0
614.391.872.4514.696.02.502.520.301.985.251.023.581290.0
714.062.152.6117.6121.02.602.510.311.255.051.063.581295.0
814.831.642.1714.097.02.802.980.291.985.201.082.851045.0
913.861.352.2716.098.02.983.150.221.857.221.013.551045.0

Input:

#scaling
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(df)
#tranforming the values
scaled_data = scaler.transform(df)
#implementing PCA
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(scaled_data)

Output:

PCA(n_components=2)

Input:

x_pca = pca.transform(scaled_data)
#identifying the number of columns
print("The shape of original dataset:",scaled_data.shape)
print("The shape of dataset after implementing PCA:",x_pca.shape)

Output:

The shape of original dataset: (178, 13)
The shape of dataset after implementing PCA: (178, 2)

Implementing PCA has reduced the number of features from 13 to 2.

Input:

#visualization
plt.scatter(x_pca[:,0],x_pca[:,1],c = wine['target'])
plt.xlabel("First Principal Component")
plt.ylabel("Second Principal Component")
plt.show()

Output:

Feature Extraction in Machine Learning

Here comes to the end of implementing PCA. Now we can use any machine learning classification algorithm to train the model.


Related Topics

Machine Learning Classification Algorithm

A supervised machine learning technique called classification uses a model to attempt to predict the right label for a given set of input data. Before being used to make predictions...

6 minutes read.

Decision Tree Algorithm in Machine Learning

We are very familiar with the word Machine Learning nowadays. Machine Learning technologies are used everywhere. Many IT companies use this type of technologies to improve their product. Decision tree...

6 minutes read.

Machine Learning Tutorial

What is Machine Learning? As all of us are very clear about the learning concept of humans, they learn from their past experiences. But can we expect the same from computers...

8 minutes read.

Convolutional Neural Network (CNN) in Machine Learning

Machine Learning  is one of the most used modern technologies in our world. Machine Learning  helps human a lot to do their jobs at ease. Today almost every big tech...

3 minutes read.

Basics Vectors in Linear Algebra in ML

First, to learn Machine Learning sincerely, we must know about vectors in Linear Algebra. The principle of Linear Algebra is very much important here. Linear Algebra is the study of...

3 minutes read.

Decision Trees in Machine Learning

Introduction to Decision Trees Decision trees are one of the most powerful classification algorithm that falls under supervised learning-based algorithms. It is used as a tool for making predictions and can...

6 minutes read.

Machine Learning Algorithms

Machine learning algorithms are powerful methods and techniques which are high in terms of probability and used to give computers high power to compute the solution for large numbers of...

4 minutes read.

Linear Regression in Machine learning

What is Linear Regression? Linear regression is the most important statistical algorithm in machine learning to learn the correlation between a dependent variable and one or more independent features. So, we...

13 minutes read.

Applications of Machine Learning

If you have connection with technical world then you have must heard about Machine Learning. It is one of the modern technologies. Machine Learning is the future of our tech...

6 minutes read.

Hands-on Machine Learning with Scikit-Learn, TensorFlow, and Keras

"Hands-On Machine Learning with Scikit-Learn and TensorFlow Keras" by Aurélien Géron is the best for you if you're comfortable with Python coding and want a fast introduction to both traditional...

4 minutes read.

Python Anaconda setup

Python programming language is used in this tutorial to get hands-on machine learning. A compatible IDE (Integrated Development Environment) is needed to be installed on the computer system before using...

3 minutes read.

Top 10 Books on Machine Learning

If you are looking to explore some new domains in engineering field then you may like ML or Machine Learning. The popularity of Machine Learning is increasing gradually day by...

5 minutes read.

Dimensionality Reduction in machine learning

What is Dimensionality? This word is generated from the word ‘dimension'. So as we know, machine learning is entirely dependent on a vast dataset, and there are a lot of variables...

3 minutes read.

Machine Learning Gesture Recognition

Strong attempts have recently been made to create user-computer interfaces that are intelligent, natural and based on human gestures. Both human and computers may use gestures as an interactive design. Therefore,...

12 minutes read.

Logistic Regression in Machine learning

The Logistic regression model is a supervised learning model that is used to forecast the possibility of a target variable. The dependent variable would have two classes, or we can...

9 minutes read.

Naïve Bayes Algorithm in Machine Learning

Introduction to Naïve Bayes Algorithm in Machine Learning The Naïve Bayes algorithm is a classification algorithm that is based on the Bayes Theorem, such that it assumes all the predictors are independent of...

7 minutes read.

Machine Learning Clustering Algorithm

Introduction to ML Clustering Algorithm Clustering falls under unsupervised learning methods. In this, the machine is provided with a set of unlabeled data, and the machine is required to extract the structure from...

2 minutes read.

Random Forest Algorithm for Machine Learning

Introduction to Random Forest Random forest is an ensemble-based supervised learning model. The concept of random forest is used in both classifications as well as in the regression problems. Basically, in...

7 minutes read.

Standardization in Machine Learning

In machine learning, we train our data to anticipate or categorize things in ways that aren't pre-programmed into the computer. As a result, firstly, the dataset or input data must...

6 minutes read.

Hierarchical Clustering Algorithm

Introduction to Hierarchical Clustering The other unsupervised learning-based algorithm used to assemble unlabeled samples based on some similarity is the Hierarchical Clustering. There are two types of hierarchical clustering algorithm: 1. Agglomerative Hierarchical Clustering...

7 minutes read.