×

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 Engineer Salary in Different Organisation

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. This branch...

3 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.

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.

Supervised Machine Learning

What is Supervised Machine Learning? In Supervised learning, the machine is trained with the help of well-labeled training data, i.e., the data is tagged with the truthful answer. In other words,...

8 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.

Heart Disease Prediction Using Machine Learning

The world uses machine learning in many different fields. This is also true in the healthcare sector. Machine learning may be crucial in determining if locomotor disorders, heart illnesses, and...

12 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.

Pattern Recognition and Machine Learning a MATLAB Companion

The cognitive process that occurs in the brain when it compares the information that we see with the information stored in our memories is called pattern recognition. It is what...

10 minutes read.

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...

4 minutes read.

K-means clustering Algorithm

Introduction to K-means clustering K-mean clustering comes under the unsupervised based learning, is a process of splitting an unlabeled dataset into the clusters based on some similarity patterns present in the data. Given...

9 minutes read.

Machine Learning Life Cycle

Machine learning has made it possible for computer systems to learn without explicit programming. However, how do machine learning systems operate? Therefore, it might be explained by the machine learning...

5 minutes read.

Best Python Libraries for Machine Learning

Machine Learning is an important technology in modern days. It helps us to do the things which were not possible in previous days. If you have interest in Machine Learning...

8 minutes read.

Azure Machine Learning

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...

3 minutes read.

Kaggle Machine Learning Project

What is Kaggle? Data scientists and machine learning enthusiasts connect online at Kaggle. Users of Kaggle can work together, access and share datasets, use notebooks with GPU integration, and compete with...

6 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.

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.

Feature Selection in Machine Learning

Feature selection Feature selection is the methodology of selecting some particular dataset instead of all the datasets, which is relevant to reducing the noise of machine learning models. In the machine...

3 minutes read.

Recommendation System in Machine Learning

In today's world, we are surrounded by machines and different types of gadgets. Machine learning is a technique which prepares machines to think and learn. A recommendation system is an...

2 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.

Diabetes Prediction using Machine Learning

Diabetes Mellitus (shortly known as Diabetes) is one of the fastest-growing diseases. Nowadays, many people are affected with diabetes for many reasons, irrespective of age. Recently, many people who belong...

6 minutes read.