<jupyter_start><jupyter_text>Import Module<jupyter_code>import pandas as pd
from simager.ml import ConfigData, ConfigPreprocess, ConfigModel, AutoClassifier, load_model<jupyter_output><empty_output><jupyter_text>Import Data<jupyter_code>df = pd.read_csv("../data/heart_disease.csv", index_col=False)
df.head()
# df = df[['chest_pain_type','exercise_induced_angina', 'st_slope','thalassemia','max_heart_rate_achieved','st_depression', 'num_major_vessels','target']]<jupyter_output><empty_output><jupyter_text>ML Configuration<jupyter_code># DATA CONFIGURATION

config_data = ConfigData(
    target="target",
    cat_features = ['sex','chest_pain_type','exercise_induced_angina', 'st_slope','thalassemia'],
    num_features = ['max_heart_rate_achieved','st_depression', 'num_major_vessels'],
    test_size = 0.2,
    random_state = 42
)
#     Args:
#         target (str, required): Name of the target column
#         cat_features (list, required): Column names of the categorical fields. Defaults to []
#         num_features (list, required): Column names of the numerical fields. Defaults to []
#         test_size (float, optional): Number of test size when spliting the data. Defaults to 0.2
#         random_state (int, optional): The random state that you provide is used as a seed to the random number generator. Defaults to 42
# PREPROCESSING CONFIGURATION

config_preprocess = ConfigPreprocess(
    cat_imputer="SimpleImputer", 
    num_imputer="SimpleImputer",
    scaler="RobustScaler",
    encoder="OneHotEncoder",
#     resample="oversampling"
)

# Args:
#     cat_imputer (str, optional): Imputation method for categorical fields. Defaults to None
#         you can only provide: `None` `SimpleImputer` method

#     num_imputer (str, optional): Imputation method for numerical fields. Defaults to None
#         you can choose: `None` `SimpleImputer` `KNNImputer`method

#     scaler (str, optional): Standardize a dataset along any axis. Defaults to None
#         you can choose: `None` `StandardScaler` `MinMaxScaler` `RobustScaler` `QuantileTransformer` `PowerTransformer` `Normalizer`

#     encoder (str, optional): Encoder method for categorical fields. Defaults to None
#         you can choose: `None` `OrdinalEncoder` `OneHotEncoder`

#     resample (str, optional): resampling method for handling unbalance data. Defaults to None
#         you can choose: `None` `oversampling` by SMOTE `undersampling` by AllKNN
config_model=ConfigModel(
    algorithm=[
    "DecisionTreeClassifier",
    "KNeighborsClassifier",
    "LogisticRegression",
    "SVC",
    "RandomForestClassifier",
    "AdaBoostClassifier",
    "XGBClassifier",
    "LGBMClassifier",
    "CatBoostClassifier"
    ],
    metrics="accuracy"
)

# Args:
#     algorithm (list, optional): Machine learning algorithm that you want to compare.
#         If not provide, this will be execute all algorithm
#         Available algorithms:
#             DecisionTreeClassifier
#             KNeighborsClassifier
#             LogisticRegression
#             SVC
#             RandomForestClassifier
#             AdaBoostClassifier
#             XGBClassifier
#             LGBMClassifier
#             CatBoostClassifier

#     metrics (str, optional): Evaluation metrics from the models. Default to `accuracy`
#         you can choose:
#             accuracy
#             precision
#             recall
#             f1

model = AutoClassifier(
    config_data = config_data,
    config_preprocess=config_preprocess,
    config_model=config_model
)<jupyter_output><empty_output><jupyter_text>Fit Model<jupyter_code>model.fit(df)<jupyter_output>2022-01-01 14:39:46,602 - ==================
2022-01-01 14:39:46,629 - DecisionTreeClassifier - accuracy: 0.711864406779661
2022-01-01 14:39:46,659 - KNeighborsClassifier - accuracy: 0.7966101694915254
2022-01-01 14:39:46,692 - LogisticRegression - accuracy: 0.8135593220338984
2022-01-01 14:39:46,722 - SVC - accuracy: 0.847457627118644
2022-01-01 14:39:46,880 - RandomForestClassifier - accuracy: 0.7627118644067796
2022-01-01 14:39:46,979 - AdaBoostClassifier - accuracy: 0.7796610169491526
/opt/anaconda3/envs/tikbot/lib/python3.7/site-packages/xgboost/sklearn.py:888: UserWarning: The use of label encoder in XGBClassifier is deprecated and will be removed in a future release. To remove this warning, do the following: 1) Pass option use_label_encoder=False when constructing XGBClassifier object; and 2) Encode your labels (y) as integers starting with 0, i.e. 0, 1, 2, ..., [num_class - 1].
  warnings.warn(label_encoder_deprecation_msg, UserWarning)
2022-01-01 14:39:47,057 - XGBClassifier -[...]<jupyter_text>Hyperparameters Tuning<jupyter_code>model.hp_tuning(
    search="bayes",
    params=None, # using recommendation from simager, you can specified if you want
    n_jobs=None,
    cv=5,
    verbose=1
)

# Args:
#     search(str, optional): Searching method to run hyperparameters tunning. Default to `bayes`
#         `bayes` (BayesSearchCV): Bayesian optimization over hyper parameters.
#         `grid` (GridSearchCV): Grid optimization over hyper parameters.

#     params (dict, optional): Parameters tunning, default to None
#         If None, parameter will be use recommendation from simager

#     n_jobs (int, optional): Number of jobs to run in parallel. Default to None
#         If None, means that (cpu_count - 1)

#     cv (int, optional): Determines the cross-validation splitting strategy. Defaults to 5

#     return_model (bool, optional): Return model object. Default to False

#     verbose (int, optional): Controls the verbosity: the higher, the more messages. Default to 0
#         Values: 0-3
# Calculate features importance from the best model
model.features_importance()
# Calculate confusion matrix from the best model
model.confusion_matrix()
# Save model
model.save("model/best_model.pkl")

# Args:
#     model_path (str, optional): Path to file model name, default to `model/best_model.pkl`
# Load model
model = load_model("model/best_model.pkl")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09962002312902693, 'fraction_numerical': 0.04377994382950603, 'mean_word_length': 3.986820428336079, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '166162', 'license': 'MIT', 'n_tokens_mistral': 2071, 'n_tokens_neox': 1871, 'n_words': 517, 'path': 'simager/example/auto_classification.ipynb', 'repo_name': 'ulwan/simager', 'size': '6053'}