def full_pipeline(model_type, predicted_column, grain_column, impute=True, verbose=True): """ Builds the data preparation pipeline. Sequentially runs transformers and filters to clean and prepare the data. Note advanced users may wish to use their own custom pipeline. """ # Note: this could be done more elegantly using FeatureUnions _if_ you are not using pandas dataframes for # inputs of the later pipelines as FeatureUnion intrinsically converts outputs to numpy arrays. pipeline = Pipeline([ ('remove_DTS_columns', hcai_filters.DataframeColumnSuffixFilter()), ('remove_grain_column', hcai_filters.DataframeColumnRemover(grain_column)), # Perform one of two basic imputation methods # TODO we need to think about making this optional to solve the problem of rare and very predictive values ('imputation', hcai_transformers.DataFrameImputer(impute=impute, verbose=verbose)), ('null_row_filter', hcai_filters.DataframeNullValueFilter(excluded_columns=None)), ('convert_target_to_binary', hcai_transformers.DataFrameConvertTargetToBinary( model_type, predicted_column)), ('prediction_to_numeric', hcai_transformers.DataFrameConvertColumnToNumeric(predicted_column)), ('create_dummy_variables', hcai_transformers.DataFrameCreateDummyVariables( excluded_columns=[predicted_column])), ]) return pipeline
def test_does_nothing_on_regression(self): df = pd.DataFrame({ 'category': ['a', 'b', 'c'], 'gender': ['F', 'M', 'F'], 'outcome': [1, 5, 4], 'string_outcome': ['Y', 'N', 'Y'] }) result = transformers.DataFrameConvertTargetToBinary('regression', 'string_outcome').fit_transform(df) self.assertTrue(df.equals(result))
def test_converts_y_n_for_classification(self): df = pd.DataFrame({ 'category': ['a', 'b', 'c'], 'gender': ['F', 'M', 'F'], 'outcome': [1, 5, 4], 'string_outcome': ['Y', 'N', 'Y'] }) expected = pd.DataFrame({ 'category': ['a', 'b', 'c'], 'gender': ['F', 'M', 'F'], 'outcome': [1, 5, 4], 'string_outcome': [1, 0, 1] }) result = transformers.DataFrameConvertTargetToBinary('classification', 'string_outcome').fit_transform(df) self.assertTrue(expected.equals(result))