How to impute missing class labels in Python?

This recipe helps you impute missing class labels in Python

Recipe Objective

In many dataset we find null values in the features so how to manage and fill the null values.

So this is the recipe on how we can impute missing class labels in Python.

Step 1 - Import the library

import numpy as np from sklearn.preprocessing import Imputer

We have imported numpy and Imputer which is needed.

Step 2 - Creating Data

We have created a matrix with different values in it and also with null values. X = np.array([[2, 2.15, 1.5], [1, 1.64, 1.25], [2, 1.15, 1.45], [0, -0.45, -1.52], [np.nan, 0.54, 1.15], [np.nan, -0.65, -0.61]])

Step 3 - Imputing Missing values

We have created an Object for Imputer with parameters strategy in which we have to pass the method of imputing and 0 or 1 in axis for rows and columns. We have used fit_transform to fit the data and impute values in null. imputer = Imputer(strategy="most_frequent", axis=0) print(X) print(imputer.fit_transform(X))

[[ 2.    2.15  1.5 ]
 [ 1.    1.64  1.25]
 [ 2.    1.15  1.45]
 [ 0.   -0.45 -1.52]
 [  nan  0.54  1.15]
 [  nan -0.65 -0.61]]

[[ 2.    2.15  1.5 ]
 [ 1.    1.64  1.25]
 [ 2.    1.15  1.45]
 [ 0.   -0.45 -1.52]
 [ 2.    0.54  1.15]
 [ 2.   -0.65 -0.61]]

Download Materials

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.