Skip to content

Instantly share code, notes, and snippets.

@cereniyim
Created April 29, 2020 13:40
Show Gist options
  • Save cereniyim/61e5def417a94de32b323e787d40be77 to your computer and use it in GitHub Desktop.
Save cereniyim/61e5def417a94de32b323e787d40be77 to your computer and use it in GitHub Desktop.
median imputer function
def ImputeWithMedian(train_df, test_df, cols=["price", "year"]):
# function to impute price and year
# columns with the median value of each
# median imputer is fitted on train dataset
# transformation done on the train and test set
train_df = pd.DataFrame(train_df[cols])
test_df = pd.DataFrame(test_df[cols])
median_imputer = SimpleImputer(strategy="median")
median_imputer.fit(train_df)
imputed_train_set = median_imputer.transform(train_df)
imputed_train_df = pd.DataFrame(imputed_train_set, columns=train_df.columns)
imputed_test_set = median_imputer.transform(test_df)
imputed_test_df = pd.DataFrame(imputed_test_set, columns=test_df.columns)
return imputed_train_df, imputed_test_df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment