Data imputation is an essential technique in data science that involves filling in missing values in a dataset. Missing values can affect the accuracy of predictive models and cause biased results. In this article, we will explore various data imputation techniques to help you choose the best approach for your project.

There are several methods of data imputation:

- Mean/Median/Mode imputation: Mean imputation is a simple technique that involves replacing missing values with the mean value of the variable. This method is easy to implement but may lead to biased results if the missing values are not missing at random. Mode imputation involves replacing missing values with the most frequently occurring value of the variable. This technique is useful for categorical variables and is straightforward to implement. Median imputation is similar to mean imputation but uses the median value instead of the mean value. This technique is useful when the variable has a skewed distribution.
- Hot-deck imputation: Hot-deck imputation involves replacing missing values with a randomly selected value from another similar observation. This method preserves the distribution of the variable and is useful when the missing values are related to other variables in the dataset.
- K-Nearest Neighbors (KNN) imputation:
- K-nearest neighbors imputation involves finding the K most similar observations to the one with missing values and using their values to fill in the missing values. This technique is useful when the missing values are related to other variables in the dataset.
- Regression imputation: This method uses regression models to predict missing values based on the values of other features in the dataset. This method is more complex than other methods, but it can result in more accurate imputations if the relationships between the features are well-defined.
- Multiple imputation: This method involves creating multiple imputations for each missing value and combining them to create a final imputation. This method can account for the uncertainty in the imputations and result in more accurate estimates.

The choice of imputation method depends on the characteristics of the data, the pattern of missingness, and the desired level of accuracy. It is important to carefully evaluate the performance of the imputation method and the resulting impact on downstream analyses or machine learning models.

By using data imputation techniques, you can improve the accuracy and robustness of your predictive models. However, it’s important to choose the right imputation technique based on the nature of the missing values and the variables in the dataset. Data preprocessing and cleaning play a critical role in ensuring the quality of your data, and accurate predictive models rely on high-quality data.

## Mean/Median/Mode imputation:

Below is an example of how to perform Mean/Median/Mode imputation on the Titanic dataset using Python:

```
import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer
# Load the Titanic dataset
df = pd.read_csv('titanic.csv')
# Replace missing values with NaN
df = df.replace('?', np.nan)
# Create a SimpleImputer object for each imputation method
mean_imputer = SimpleImputer(strategy='mean')
median_imputer = SimpleImputer(strategy='median')
mode_imputer = SimpleImputer(strategy='most_frequent')
# Impute missing values using each method
df['Age_mean'] = mean_imputer.fit_transform(df[['Age']])
df['Age_median'] = median_imputer.fit_transform(df[['Age']])
df['Embarked_mode'] = mode_imputer.fit_transform(df[['Embarked']])
# Print the first few rows of the imputed dataframe
print(df.head())
```

In this example, we first load the Titanic dataset and replace any missing values with NaN. We then create a `SimpleImputer`

object for each imputation method (mean, median, and mode). We fit each imputer to the corresponding feature with missing values (Age and Embarked in this case), and use the `transform`

method to impute the missing values using each method.

Finally, we create new columns in the dataframe for each imputed feature, and print the first few rows of the imputed dataframe to confirm that the imputation was successful.

## KNN Imputation

KNN Imputation is a technique for imputing missing values in a dataset based on the values of its nearest neighbors. The idea behind KNN imputation is to identify the K nearest neighbors of a sample with missing values and then impute the missing value with the average (for continuous variables) or mode (for categorical variables) of the values of these neighbors.

The KNN imputation method works in the following way:

- Identify the K nearest neighbors of the sample with missing values based on the values of the other features.
- Calculate the average (for continuous variables) or mode (for categorical variables) of the values of these neighbors.
- Impute the missing value with the calculated average or mode.

KNN imputation is a non-parametric method, meaning that it makes no assumptions about the distribution of the data. It is also computationally efficient and does not require training a separate model for imputation. However, it can be sensitive to outliers and may not work well with high-dimensional datasets.

KNN imputation is implemented in several libraries in Python, including scikit-learn and fancy impute. It can be used for imputing missing values in both continuous and categorical variables.

Here’s an example of how to implement KNN imputation of missing values on the Titanic dataset from Kaggle using Python:

```
import pandas as pd
import numpy as np
from sklearn.impute import KNNImputer
# Load the Titanic dataset
titanic_df = pd.read_csv("titanic.csv")
# Check for missing values
print(titanic_df.isnull().sum())
# Separate the features and target variable
X = titanic_df.drop("Survived", axis=1)
y = titanic_df["Survived"]
# Convert categorical features to numerical using one-hot encoding
X = pd.get_dummies(X, columns=["Sex", "Embarked"])
# Initialize the KNN imputer with k=5
imputer = KNNImputer(n_neighbors=5)
# Impute the missing values in X
X_imputed = imputer.fit_transform(X)
# Convert X_imputed to a DataFrame
X_imputed_df = pd.DataFrame(X_imputed, columns=X.columns)
# Check for missing values in the imputed dataset
print(X_imputed_df.isnull().sum())
# Now, you can use X_imputed_df for further analysis or modeling
```

## Hot Deck Imputation

Below is an example of how to perform Hot-deck imputation on the Titanic dataset using Python:

```
import pandas as pd
import numpy as np
from sklearn.impute import KNNImputer
# Load the Titanic dataset
df = pd.read_csv('titanic.csv')
# Replace missing values with NaN
df = df.replace('?', np.nan)
# Create a KNNImputer object for Hot-deck imputation
imputer = KNNImputer(n_neighbors=3, weights='distance')
# Impute missing values using Hot-deck imputation
df_imputed = pd.DataFrame(imputer.fit_transform(df), columns=df.columns)
# Print the first few rows of the imputed dataframe
print(df_imputed.head())
```

In this example, we first load the Titanic dataset and replace any missing values with NaN. We then create a `KNNImputer`

object for Hot-deck imputation and set the number of neighbors to 3 and the weighting method to ‘distance’.

We fit the imputer to the entire dataset (including the features with missing values) and use the `transform`

method to impute the missing values using Hot-deck imputation.

Finally, we create a new dataframe with the imputed values and print the first few rows to confirm that the imputation was successful. Note that the `KNNImputer`

uses the values of the `n_neighbors`

closest non-missing values to impute the missing values, so the imputed values may be different each time the imputer is run.

## MICE Imputation

MICE (Multivariate Imputation by Chained Equations) is a technique for imputing missing values in a dataset using a multivariate approach. Unlike KNN imputation, MICE imputation takes into account the correlations between the missing values and the other features in the dataset.

The idea behind MICE imputation is to impute each missing value in the dataset multiple times (usually 10 or more) using a regression model that is trained on the other features in the dataset. The imputed value is then used in the next round of imputation to update the regression model, and the process is repeated until convergence.

MICE imputation works in the following way:

- For each missing value in the dataset, create a regression model using the other features in the dataset as predictors.
- Impute the missing value using the predicted value from the regression model.
- Repeat steps 1 and 2 for a fixed number of iterations (usually 10 or more).
- Combine the results from all the imputations to obtain a final imputed dataset.

MICE imputation is a flexible method that can handle missing data in both continuous and categorical variables, and can also handle missing data in the target variable (i.e., the variable to be predicted). However, it can be computationally expensive and may not work well with very large datasets or highly correlated variables.

MICE imputation is implemented in several libraries in Python, including the `mice`

package in R, and the `fancyimpute`

and `impyute`

libraries in Python.

```
import pandas as pd
import numpy as np
from sklearn.impute import IterativeImputer
# Load the Titanic dataset
df = pd.read_csv('titanic.csv')
# Replace missing values with NaN
df = df.replace('?', np.nan)
# Create an IterativeImputer object for MICE imputation
imputer = IterativeImputer(max_iter=10, random_state=0)
# Impute missing values using MICE imputation
df_imputed = pd.DataFrame(imputer.fit_transform(df), columns=df.columns)
# Print the first few rows of the imputed dataframe
print(df_imputed.head())
```

In this example, we first load the Titanic dataset and replace any missing values with NaN. We then create an `IterativeImputer`

object for MICE imputation and set the maximum number of iterations to 10 and the random seed to 0.

We fit the imputer to the entire dataset (including the features with missing values) and use the `transform`

method to impute the missing values using MICE imputation.

Finally, we create a new dataframe with the imputed values and print the first few rows to confirm that the imputation was successful. Note that the `IterativeImputer`

uses a regression model to impute the missing values based on the non-missing values of the other features, and the imputed values may be different each time the imputer is run.

**Important Notice For College Students**

If youâ€™re a college student and have skills in programming languages, Want to earn through blogging? Mail us at geekycomail@gmail.com

For more Programming related blogs Visit Us Geekycodes. Follow us on Instagram.