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
Exemple #2
0
    def test_removes_row_all_nulls_exception(self):
        df = pd.DataFrame({'a': [1, None, 2, 3],
                           'b': ['m', 'f', None, 'f'],
                           'c': [3, 4, 5, None],
                           'd': [None, 8, 1, 3],
                           'label': ['Y', 'N', 'Y', 'N']})

        self.assertRaises(HealthcareAIError, filters.DataframeNullValueFilter().fit_transform, df)
Exemple #3
0
    def test_removes_row_with_all_nulls(self):
        df = pd.DataFrame({
            'category': ['a', None, None],
            'gender': ['F', 'M', None],
            'age': [1, 5, None]
        })

        result = filters.DataframeNullValueFilter().fit_transform(df)
        self.assertEqual(len(result), 1)
Exemple #4
0
    def test_removes_nothing_when_no_nulls_exist(self):
        df = pd.DataFrame({
            'category': ['a', 'b', 'c'],
            'gender': ['F', 'M', 'F'],
            'age': [1, 5, 4]
        })

        result = filters.DataframeNullValueFilter().fit_transform(df)
        self.assertEqual(len(result), 3)