How to impute missing class labels in Python?

How to impute missing class labels in Python?

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

Relevant Projects

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.