<jupyter_start><jupyter_text>autogluon for ptbdb dataset - see how autoML does on ptbdb dataset- docs can be found [here](https://auto.gluon.ai/stable/tutorials/tabular_prediction/tabular-quickstart.html)---  setup<jupyter_code>#@markdown add auto-Colab formatting with `IPython.display`
from IPython.display import HTML, display
# colab formatting
def set_css():
    display(
        HTML(
            """
  <style>
    pre {
        white-space: pre-wrap;
    }
  </style>
  """
        )
    )

get_ipython().events.register("pre_run_cell", set_css)
#@title print out GPU info
#@markdown this is the Colab-allocated GPU. If the output here says it fails, no
#@markdown GPU is being used. go to runtime at the top of your colab to set runtime to GPU.


!nvidia-smi
!pip install autogluon -q 
#@title define source data parameters

#@markdown - these can also be loaded from gdrive, but I am lazy and `wget` does not require login

ptbdb_train_url = "https://www.dropbox.com/s/nxwz0rlyckcfq0v/torchfmt_ptbdb_train.csv?dl=1" #@param {type:"string"}
ptbdb_train_filename = "ptbdb_train.csv" #@param {type:"string"}
ptbdb_test_url = "https://www.dropbox.com/s/ysey3qdpvgqvoz2/torchfmt_ptbdb_test.csv?dl=1" #@param {type:"string"}
ptbdb_test_filename = "ptbdb_test.csv" #@param {type:"string"}
#@title autogluon parameters 

#@markdown - `max_time_fitting` is in seconds

max_time_fitting =  10800#@param {type:"integer"}
fit_preset = "best_quality" #@param ["best_quality", "high_quality"]
metric = 'accuracy'  #@param ["roc_auc", "accuracy", "f1"]<jupyter_output><empty_output><jupyter_text>util functions<jupyter_code>from pathlib import Path
import os
def scour_for_file(
                    file_name:str, 
                    search_root=None, 
                    return_first=True, 
                    verbose=False,
                ):
    """
    scour_for_file - search every possible location for a file name recursively.
     Return the full path to file(s)
    """
    search_root = Path.cwd() if search_root is None else Path(search_root)
    matches = []
    for root, dirs, files in os.walk(search_root.resolve()):
        if file_name in files:
            _path2file = Path(root) / file_name
            if return_first:
                return _path2file.resolve()
            else:
                matches.append(_path2file.resolve())
    if len(matches) < 1:
        print(f"NOTE: zero matches found for {file_name} in {search_root}")
        return None
    else:
        print(f"{len(matches)} matches found for {file_name} in {search_root}")

        if verbose:
            print(f"matched paths are as follows:\n{matches}")
            if return_first: print("will return only topmost path")
        return matches[0] if return_first else matches

from google.colab import files
from pathlib import Path
import pathlib
import time
import warnings
from tqdm.auto import tqdm

def download_colab(my_files):
    """
    helper function to download things out of colab. 
    
    input can be a list, string, or Path() that refers to the file.
    """
    if my_files is None:
        warnings.warn(message=f"received {my_files}, nothing to DL")
        return None

    
    if isinstance(my_files, list):
        filepaths = [f for f in my_files if Path(f).exists() and Path(f).is_file()]
        pbar = tqdm(total=len(filepaths), desc="downloading files..")
        for filepath in filepaths:
            files.download(filepath)
            pbar.update(1)
            time.sleep(3) # prevent browsers auto-blocking downloads
        pbar.close()
    else:
        assert isinstance(my_files, str) or isinstance(my_files, pathlib.Path), "non-list needs to be str or Path()"
        single_file = Path(my_files)
        files.download(single_file)<jupyter_output><empty_output><jupyter_text>data<jupyter_code>!wget $ptbdb_train_url -O $ptbdb_train_filename -q
!wget $ptbdb_test_url -O $ptbdb_test_filename -q
import pandas as pd
example_df = pd.read_csv(ptbdb_train_filename)
data_cols = list(example_df.columns)
_target = data_cols[-1]
data_cols.pop()
_predictors = data_cols # all other columns are numerical predictors

print(f"the target colname is {_target} and\nthe predictor colnames 5 of {len(_predictors)} are {_predictors[:5]}")<jupyter_output><empty_output><jupyter_text>fit model<jupyter_code>import torch
from autogluon.tabular import TabularDataset, TabularPredictor
train_data = TabularDataset(ptbdb_train_filename)
test_data = TabularDataset(ptbdb_test_filename)
predictor = TabularPredictor(
                            label=_target,
                             eval_metric=metric,
                             ).fit(
                                    train_data, 
                                    time_limit=max_time_fitting,
                                    presets=fit_preset,
                                   ag_args_fit={'num_gpus': torch.cuda.device_count()}
                                   )  

results = predictor.fit_summary(show_plot=True)

summary_report = scour_for_file('SummaryOfModels.html')
download_colab(summary_report)
extra_metrics=['accuracy', 'roc_auc', 'log_loss', 'f1']

leaderboard = predictor.leaderboard(test_data, 
                                    # extra_metrics=extra_metrics
                                    )

leaderboard
feat_importance = predictor.feature_importance(
                                                test_data,
                                               time_limit=600,
                    )
feat_importance<jupyter_output><empty_output><jupyter_text>export results<jupyter_code>!pip install openpyxl -q
from google.colab import files
import pandas as pd
from pathlib import Path

_cwd = Path.cwd()

csv_path = _cwd / "ptbdb_autogluon_results.csv"
xlsx_path = _cwd / "ptbdb_autogluon_results.xlsx"

leaderboard.to_csv(csv_path)
leaderboard.to_excel(xlsx_path)

download_colab([csv_path, xlsx_path])

def export_model_files():
    learner_path = Path(scour_for_file("learner.pkl", verbose=True))
    predictor_path = Path(scour_for_file("predictor.pkl", verbose=True))

    learner_renamed_path = learner_path.parent / f"ptbdb_{learner_path.name}"
    predictor_renamed_path = predictor_path.parent / f"ptbdb_{predictor_path.name}"
    old_paths = [learner_path, predictor_path]
    new_paths = [learner_renamed_path, predictor_renamed_path]

    for old, new in zip(old_paths, new_paths):
        os.rename(old, new)

    download_colab(new_paths)

export_model_files()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09613022113022113, 'fraction_numerical': 0.003992628992628993, 'mean_word_length': 2.9329710144927534, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 23, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '255373', 'license': 'Apache-2.0', 'n_tokens_mistral': 2137, 'n_tokens_neox': 2074, 'n_words': 477, 'path': 'notebooks/colab/automl-baseline/autogluon_tabular_ptbdb.ipynb', 'repo_name': 'pszemraj/ml4hc-s22-project01', 'size': '6512'}