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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask