src/data/encode_labels.py
def main(train_path, test_path,
         output_dir, remove_nan=False,
         label_dict_name="label_encoding.yaml"):
    """Encode categorical labels as numeric, save the processed
    dataset the label encoding dictionary"""

    output_dir = Path(output_dir).resolve()
    assert (os.path.isdir(output_dir)), NotADirectoryError

    # load data
    train_df, test_df = load_data([train_path, test_path], sep=",", header=0,
                                  index_col="PassengerId")

    # load params
    params = load_params()

    # update params for column data types
    param_dtypes = params["dtypes"]
    param_dtypes["Pclass"] = pd.api.types.CategoricalDtype(categories=[1, 2, 3],
                                                           ordered=True)
    # concatenate df
    df = pd.concat([train_df, test_df], sort=False)
    df = df.astype(param_dtypes)

    # drop unnecessary columns
    df = df.drop(columns=params["drop_cols"])

    # convert to categorical
    encoding_dict = {}
    for elem, col in zip(df.dtypes, df.columns):
        if isinstance(elem, pd.CategoricalDtype):
            # save mapping of category to integer class
            encoding_dict[col] = {key: val for key, val in enumerate(elem.categories)}

            # transform to categorical codes
            df[col] = df[col].cat.codes

    # return datasets to train and test
    train_df = df.loc[train_df.index, df.columns]
    test_df = df.loc[test_df.index, df.columns[1:]]

    # remove nan (if applicable
    if remove_nan:
        train_df = train_df.dropna(axis=0, how="any")

    # save data
    save_as_csv([train_df, test_df],
                [train_path, test_path],
                output_dir,
                replace_text=".csv",
                suffix="_categorized.csv",
                na_rep="nan")

    # save and encoding dictionaries
    encoding_dict = yaml.safe_dump(encoding_dict)
    with open(os.path.join(output_dir, label_dict_name), "w") as writer:
        writer.writelines(encoding_dict)
Encode categorical labels as numeric, save the processed
    dataset the label encoding dictionary
Encode categorical labels as numeric, save the processed
dataset the label encoding dictionary
Encode categorical labels as numeric save the processed dataset the label encoding dictionary
def main(train_path, test_path,
         output_dir, remove_nan=False,
         label_dict_name="label_encoding.yaml"):
    output_dir = Path(output_dir).resolve()
    assert (os.path.isdir(output_dir)), NotADirectoryError
    train_df, test_df = load_data([train_path, test_path], sep=",", header=0,
                                  index_col="PassengerId")
    params = load_params()
    param_dtypes = params["dtypes"]
    param_dtypes["Pclass"] = pd.api.types.CategoricalDtype(categories=[1, 2, 3],
                                                           ordered=True)
    df = pd.concat([train_df, test_df], sort=False)
    df = df.astype(param_dtypes)
    df = df.drop(columns=params["drop_cols"])
    encoding_dict = {}
    for elem, col in zip(df.dtypes, df.columns):
        if isinstance(elem, pd.CategoricalDtype):
            encoding_dict[col] = {key: val for key, val in enumerate(elem.categories)}
            df[col] = df[col].cat.codes
    train_df = df.loc[train_df.index, df.columns]
    test_df = df.loc[test_df.index, df.columns[1:]]
    if remove_nan:
        train_df = train_df.dropna(axis=0, how="any")
    save_as_csv([train_df, test_df],
                [train_path, test_path],
                output_dir,
                replace_text=".csv",
                suffix="_categorized.csv",
                na_rep="nan")
    encoding_dict = yaml.safe_dump(encoding_dict)
    with open(os.path.join(output_dir, label_dict_name), "w") as writer:
        writer.writelines(encoding_dict)
def main ( train_path , test_path , output_dir , remove_nan = False , label_dict_name = "label_encoding.yaml" ) : output_dir = Path ( output_dir ) . resolve ( ) assert ( os . path . isdir ( output_dir ) ) , NotADirectoryError train_df , test_df = load_data ( [ train_path , test_path ] , sep = "," , header = 0 , index_col = "PassengerId" ) params = load_params ( ) param_dtypes = params [ "dtypes" ] param_dtypes [ "Pclass" ] = pd . api . types . CategoricalDtype ( categories = [ 1 , 2 , 3 ] , ordered = True ) df = pd . concat ( [ train_df , test_df ] , sort = False ) df = df . astype ( param_dtypes ) df = df . drop ( columns = params [ "drop_cols" ] ) encoding_dict = { } for elem , col in zip ( df . dtypes , df . columns ) : if isinstance ( elem , pd . CategoricalDtype ) : encoding_dict [ col ] = { key : val for key , val in enumerate ( elem . categories ) } df [ col ] = df [ col ] . cat . codes train_df = df . loc [ train_df . index , df . columns ] test_df = df . loc [ test_df . index , df . columns [ 1 : ] ] if remove_nan : train_df = train_df . dropna ( axis = 0 , how = "any" ) save_as_csv ( [ train_df , test_df ] , [ train_path , test_path ] , output_dir , replace_text = ".csv" , suffix = "_categorized.csv" , na_rep = "nan" ) encoding_dict = yaml . safe_dump ( encoding_dict ) with open ( os . path . join ( output_dir , label_dict_name ) , "w" ) as writer : writer . writelines ( encoding_dict )
Encode categorical labels as numeric, save the processed
dataset the label encoding dictionary
Encode categorical labels as numeric save the processed dataset the label encoding dictionary
"""Encode categorical labels as numeric, save the processed
    dataset the label encoding dictionary""" # load data # load params # update params for column data types # concatenate df # drop unnecessary columns # convert to categorical # save mapping of category to integer class # transform to categorical codes # return datasets to train and test # remove nan (if applicable # save data # save and encoding dictionaries
train_path
test_path
output_dir
remove_nan
label_dict_name