Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in ImputationKernel: Handling of imputation_order #90

Open
nmehran opened this issue Sep 18, 2024 · 0 comments
Open

Bug in ImputationKernel: Handling of imputation_order #90

nmehran opened this issue Sep 18, 2024 · 0 comments

Comments

@nmehran
Copy link

nmehran commented Sep 18, 2024

First off, thank you for maintaining such a useful library, I truly appreciate it.

A bug has been identified in the ImputationKernel class regarding the imputation_order parameter. The handling of "ascending" and "descending" orders is incorrect due to a typo and improper sorting configuration.

Problematic Code

# imputation_kernel.py
if imputation_order in ["ascending", "descending"]:
    _na_counts = {
        key: value
        for key, value in self.na_counts.items()
        if key in self.imputed_variables
    }
    self.imputation_order = list(
        Series(_na_counts).sort_values(ascending=False).index
    )
    if imputation_order == "decending":
        self.imputation_order.reverse()

Issues

  • Typo: "decending" should be "descending".
  • Incorrect Sorting Configuration: The parameter ascending=False should be ascending=True for ascending order.

Proposed Fix

Update the code as follows:

if imputation_order in ["ascending", "descending"]:
    _na_counts = {
        key: value
        for key, value in self.na_counts.items()
        if key in self.imputed_variables
    }
    self.imputation_order = list(
        Series(_na_counts).sort_values(ascending=True).index
    )
    if imputation_order == "descending":
        self.imputation_order.reverse()

Steps to Verify

Test the following code to compare imputation_order for "ascending" and "descending":

import pandas as pd
import numpy as np
import miceforest as mf
from sklearn.datasets import load_iris

# Load the Iris dataset
iris_data = load_iris()
iris_df = pd.DataFrame(iris_data.data, columns=iris_data.feature_names)

# Introduce missing values
np.random.seed(42)
missing_indices = np.random.choice(iris_df.index, size=10, replace=False)
iris_df.loc[missing_indices, 'sepal length (cm)'] = np.nan
iris_df.loc[np.random.choice(iris_df.index, size=8, replace=False), 'sepal width (cm)'] = np.nan
iris_df.loc[np.random.choice(iris_df.index, size=4, replace=False), 'petal length (cm)'] = np.nan
iris_df.loc[np.random.choice(iris_df.index, size=2, replace=False), 'petal width (cm)'] = np.nan

# Initialize ImputationKernel with 'ascending' order
kernel_ascending = mf.ImputationKernel(iris_df, imputation_order='ascending')

# Initialize ImputationKernel with 'descending' order
kernel_descending = mf.ImputationKernel(iris_df, imputation_order='descending')

# Compare imputation orders
ascending_order = kernel_ascending.imputation_order
descending_order = kernel_descending.imputation_order

# Print equality comparison result
print("Are the imputation orders equal?")
print(ascending_order == descending_order)

Additional Information

  • Version: 6.0.3
nmehran added a commit to nmehran/miceforest that referenced this issue Sep 18, 2024
- Fixed typo "decending" to "descending".
- Updated sorting configuration for ascending order to use ascending=True.

Related to issue AnotherSamWilson#90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant