<jupyter_start><jupyter_text><jupyter_code># https://auto.gluon.ai/stable/install.html
!pip install -q autogluon.tabular[all]
%matplotlib inline

import time

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn
import sklearn.ensemble
import sklearn.model_selection
import sklearn.datasets
import sklearn.metrics
from sklearn.preprocessing import LabelEncoder
from sklearn.inspection import plot_partial_dependence, permutation_importance

# https://auto.gluon.ai/stable/index.html
import autogluon.tabular
from autogluon.tabular import TabularDataset, TabularPredictor
!pip show autogluon.tabular<jupyter_output>Name: autogluon.tabular
Version: 0.2.0
Summary: AutoML for Text, Image, and Tabular Data
Home-page: https://github.com/awslabs/autogluon
Author: AutoGluon Community
Author-email: None
License: Apache-2.0
Location: /usr/local/lib/python3.7/dist-packages
Requires: pandas, autogluon.core, networkx, numpy, psutil, autogluon.features, scipy, pytest, scikit-learn
Required-by:<jupyter_text>Data Loading and Preprocess<jupyter_code># Using Titanic dataset https://www.openml.org/d/40945.
# This example will use the command fetch_openml, which will
# download a properly formatted dataframe if you use as_frame=True.
X, y = sklearn.datasets.fetch_openml(data_id=40945, return_X_y=True, as_frame=True)
# remove unused columns
X.drop(['name', 'cabin', 'ticket', 'body', 'home.dest', 'boat'], inplace=True, axis=1)

X["pclass"] = X["pclass"].astype("int")
X["sibsp"] = X["sibsp"].astype("int")
X["parch"] = X["parch"].astype("int")        
X["age"] = X["age"].astype("float")        
X["fare"] = X["fare"].astype("float")        
X["embarked"] = X["embarked"].astype("category")
X["sex"] = X["sex"].astype("category")
y = y.astype("category")
# check missing values
for column in X.columns:
    print(f"{column}: {str(sum(X[column].isnull()))} missing values")
X.dtypes
X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
    X, y, test_size=0.3, random_state=1
)<jupyter_output><empty_output><jupyter_text>Baseline<jupyter_code># Gender model
# It will predict that all women survive and all men do not survive.
print("Accuracy score", sklearn.metrics.accuracy_score(y_test, X_test["sex"].map(lambda x: '1' if x == "female" else '0')))<jupyter_output>Accuracy score 0.7888040712468194<jupyter_text>Build and Fit a Classifier<jupyter_code># prepare input data
train = X_train.copy()
train['survive'] = y_train

test = X_test.copy()
test["survive"] = y_test
%%time

# https://auto.gluon.ai/dev/api/autogluon.task.html#module-0
model = TabularPredictor(
    label='survive',
    problem_type='binary',
    eval_metric='accuracy',  # 'accuracy' is default for binary/multiclass classification
)
# https://auto.gluon.ai/dev/api/autogluon.task.html#autogluon.tabular.TabularPredictor.fit
model.fit(
    train,
    time_limit=120,  # in sec
    tuning_data=None,  # Do not provide your evaluation test data here!
    ag_args_fit={"num_cpu": 1}
) 
y_train_pred = model.predict(X_train)
print("Accuracy score", sklearn.metrics.accuracy_score(y_train, y_train_pred))
# the model name of the best model by validation score
print(model.get_model_best())
# evaluate all of the models AutoGluon has previously trained on our test data
model.leaderboard(test, silent=True)<jupyter_output><empty_output><jupyter_text>Predict and Evaluate the Final Ensemble<jupyter_code>y_pred = model.predict(X_test)
print("Accuracy score", sklearn.metrics.accuracy_score(y_test, y_pred))<jupyter_output>Accuracy score 0.806615776081425<jupyter_text>Model ExplanationRef: https://auto.gluon.ai/dev/tutorials/tabular_prediction/tabular-indepth.htmlinterpretability-feature-importance<jupyter_code># Interpretability (feature importance)
model.feature_importance(test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12901540872290415, 'fraction_numerical': 0.01593105249412379, 'mean_word_length': 6.3938223938223935, 'pattern_counts': {'":': 1, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 7, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '112812', 'license': 'Apache-2.0', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1351, 'n_words': 287, 'path': 'autogluon_titanic.ipynb', 'repo_name': 'hnishi/hello-automl', 'size': '3829'}