ML | Handle Missing Data with Simple Imputer Last Updated : 28 Sep, 2021 Comments Improve Suggest changes 12 Likes Like Report SimpleImputer is a scikit-learn class which is helpful in handling the missing data in the predictive model dataset. It replaces the NaN values with a specified placeholder. It is implemented by the use of the SimpleImputer() method which takes the following arguments : missing_values : The missing_values placeholder which has to be imputed. By default is NaN strategy : The data which will replace the NaN values from the dataset. The strategy argument can take the values - 'mean'(default), 'median', 'most_frequent' and 'constant'. fill_value : The constant value to be given to the NaN data using the constant strategy. Code: Python code illustrating the use of SimpleImputer class. Python3 import numpy as np # Importing the SimpleImputer class from sklearn.impute import SimpleImputer # Imputer object using the mean strategy and # missing_values type for imputation imputer = SimpleImputer(missing_values = np.nan, strategy ='mean') data = [[12, np.nan, 34], [10, 32, np.nan], [np.nan, 11, 20]] print("Original Data : \n", data) # Fitting the data to the imputer object imputer = imputer.fit(data) # Imputing the data data = imputer.transform(data) print("Imputed Data : \n", data) Output Original Data : [[12, nan, 34] [10, 32, nan] [nan, 11, 20]] Imputed Data : [[12, 21.5, 34] [10, 32, 27] [11, 11, 20]] Remember: The mean or median is taken along the column of the matrix Comment P prateekb1912 Follow 12 Improve P prateekb1912 Follow 12 Improve Article Tags : Machine Learning AI-ML-DS AI-ML-DS With Python Explore Machine Learning BasicsIntroduction to Machine Learning8 min readTypes of Machine Learning13 min readWhat is Machine Learning Pipeline?7 min readApplications of Machine Learning3 min readPython for Machine LearningMachine Learning with Python Tutorial5 min readNumPy Tutorial - Python Library3 min readPandas Tutorial4 min readData Preprocessing in Python4 min readEDA - Exploratory Data Analysis in Python6 min readFeature EngineeringWhat is Feature Engineering?5 min readIntroduction to Dimensionality Reduction4 min readFeature Selection Techniques in Machine Learning6 min readSupervised LearningSupervised Machine Learning7 min readLinear Regression in Machine learning15+ min readLogistic Regression in Machine Learning11 min readDecision Tree in Machine Learning9 min readRandom Forest Algorithm in Machine Learning5 min readK-Nearest Neighbor(KNN) Algorithm8 min readSupport Vector Machine (SVM) Algorithm9 min readNaive Bayes Classifiers7 min readUnsupervised LearningWhat is Unsupervised Learning5 min readK means Clustering â Introduction6 min readHierarchical Clustering in Machine Learning6 min readDBSCAN Clustering in ML - Density based clustering6 min readApriori Algorithm6 min readFrequent Pattern Growth Algorithm5 min readECLAT Algorithm - ML5 min readPrincipal Component Analysis(PCA)7 min readModel Evaluation and TuningEvaluation Metrics in Machine Learning9 min readRegularization in Machine Learning5 min readCross Validation in Machine Learning5 min readHyperparameter Tuning7 min readML | Underfitting and Overfitting5 min readBias and Variance in Machine Learning10 min readAdvanced TechniquesReinforcement Learning8 min readSemi-Supervised Learning in ML5 min readSelf-Supervised Learning (SSL)6 min readEnsemble Learning8 min readMachine Learning PracticeMachine Learning Interview Questions and Answers15+ min read100+ Machine Learning Projects with Source Code [2025]6 min read Like