<jupyter_start><jupyter_text>加载数据* 按y列分层采用 训练集80% 测试集20%* 为了快速演示，只抽取部分样本<jupyter_code>df = dsutils.load_bank().head(5000)
df.drop(['id'], axis=1, inplace=True)
y = df.pop('y')
X_train, X_test, y_train,y_test = train_test_split(df,y, test_size=0.2, random_state=42, stratify=y)
print(f'X_train:{X_train.shape}, y_train:{y_train.shape}, X_test:{X_test.shape}, y_test:{y_test.shape}')<jupyter_output>X_train:(4000, 16), y_train:(4000,), X_test:(1000, 16), y_test:(1000,)<jupyter_text>数据分析<jupyter_code>#columns_info(df)<jupyter_output><empty_output><jupyter_text>定义搜索空间  初始化搜索算法和HyperDT* EarlyStopping 50* 开启预处理数据缓存<jupyter_code>disk_trial_store = DiskTrialStore(f'trial_store')

#searcher = MCTSSearcher(search_space, max_node_space=5,optimize_direction='max')
searcher = EvolutionSearcher(mini_dt_space, 50, 20, regularized=True, optimize_direction=OptimizeDirection.Maximize)

hdt = HyperDT(searcher,
              callbacks=[SummaryCallback(), FileLoggingCallback(searcher),EarlyStoppingCallback(10, 'max')],
              reward_metric='AUC',
              dnn_params={
                  'hidden_units': ((256, 0, False), (256, 0, False)),
                  'activation': 'relu',
              },
             cache_preprocessed_data=True, #cache_home='pre_cache',
             )<jupyter_output><empty_output><jupyter_text>开始搜索* Trial次数 3* Epochs 100<jupyter_code>%%time

hdt.search(X_train, y_train, X_test, y_test, trial_store=disk_trial_store, max_trials=3, epochs=100)
best_trial = hdt.get_best_trial()
print(f'best reward:{best_trial.reward}')<jupyter_output>05-24 16:10:02 I hypernets.u.common.py 107 - 2 class detected, {'no', 'yes'}, so inferred as a [binary classification] task
05-24 16:10:02 I hypernets.c.meta_learner.py 22 - Initialize Meta Learner: dataset_id:5a9fb0b3bd940966b51926eb66c9523d
05-24 16:10:02 I hypernets.c.callbacks.py 192 - 
Trial No:1
--------------------------------------------------------------
(0) Module_DTModuleSpace_1.nets:      ['dnn_nets', 'fm_nets']
(1) Module_DTModuleSpace_1.auto_categorize:              True
(2) Module_DTModuleSpace_1.cat_remain_numeric:           True
(3) Module_DTModuleSpace_1.auto_discrete:               False
(4) Module_DTModuleSpace_1.apply_gbm_features:           True
(5) Module_DTModuleSpace_1.gbm_feature_type:            dense
(6) Module_DTModuleSpace_1.embeddings_output_dim:           4
(7) Module_DTModuleSpace_1.embedding_dropout:               0
(8) Module_DTModuleSpace_1.stacking_op:                   add
(9) Module_DTModuleSpace_1.output_use_bias:              True
(10) Module_DT[...]<jupyter_text>提取排名最高的模型配置并重新训练<jupyter_code>%%time

estimator = hdt.final_train(best_trial.space_sample, X_train, y_train, epochs=100)<jupyter_output>05-24 16:10:26 I deeptables.m.deeptable.py 335 - X.Shape=(4000, 16), y.Shape=(4000,), batch_size=128, config=ModelConfig(name='conf-1', nets=['fm_nets', 'dnn_nets'], categorical_columns='auto', exclude_columns=[], task='auto', pos_label=None, metrics=['AUC'], auto_categorize=True, cat_exponent=0.5, cat_remain_numeric=True, auto_encode_label=True, auto_imputation=True, auto_discrete=False, auto_discard_unique=True, apply_gbm_features=True, gbm_params={}, gbm_feature_type='dense', fixed_embedding_dim=True, embeddings_output_dim=4, embeddings_initializer='uniform', embeddings_regularizer=None, embeddings_activity_regularizer=None, dense_dropout=0, embedding_dropout=0, stacking_op='add', output_use_bias=True, apply_class_weight=False, optimizer='auto', loss='auto', dnn_params={'hidden_units': ((256, 0, False), (256, 0, False)), 'activation': 'relu'}, autoint_params={'num_attention': 3, 'num_heads': 1, 'dropout_rate': 0, 'use_residual': True}, fgcnn_params={'fg_filters': (14, 16), 'fg_heigh[...]<jupyter_text>模型评估<jupyter_code>estimator.evaluate(X_test, y_test)<jupyter_output>05-24 16:10:29 I deeptables.m.preprocessor.py 239 - Transform [X]...
05-24 16:10:29 I deeptables.m.preprocessor.py 246 - transform_X taken 0.038956403732299805s
05-24 16:10:29 I deeptables.m.preprocessor.py 227 - Transform [y]...
05-24 16:10:29 I deeptables.m.preprocessor.py 233 - transform_y taken 0.0s
05-24 16:10:29 I deeptables.m.deepmodel.py 168 - Performing evaluation...
TF metrics names is ['loss', 'AUC'] and user's is ['AUC']<jupyter_text>模型预测<jupyter_code>pred = estimator.model.predict(X_test)
proba = estimator.model.predict_proba(X_test)<jupyter_output>05-24 16:10:29 I deeptables.m.deeptable.py 658 - Perform prediction...
05-24 16:10:29 I deeptables.m.preprocessor.py 239 - Transform [X]...
05-24 16:10:29 I deeptables.m.preprocessor.py 246 - transform_X taken 0.04300069808959961s
05-24 16:10:29 I deeptables.m.deepmodel.py 142 - Performing predictions...
05-24 16:10:29 I deeptables.m.deeptable.py 537 - predict_proba taken 0.2089996337890625s
05-24 16:10:29 I deeptables.m.deeptable.py 572 - Reverse indicators to labels.
05-24 16:10:29 I deeptables.m.deeptable.py 658 - Perform prediction...
05-24 16:10:29 I deeptables.m.preprocessor.py 239 - Transform [X]...
05-24 16:10:29 I deeptables.m.preprocessor.py 246 - transform_X taken 0.04300546646118164s
05-24 16:10:29 I deeptables.m.deepmodel.py 142 - Performing predictions...
05-24 16:10:30 I deeptables.m.deeptable.py 537 - predict_proba taken 0.11600327491760254s<jupyter_text>模型解释<jupyter_code>dt_explainer = DeepTablesExplainer(estimator.model, X_train, 100)
shap_values = dt_explainer.get_shap_values(X_train[:1])
shap.force_plot(dt_explainer.explainer.expected_value, shap_values[0], X_train[:1])
shap.decision_plot(dt_explainer.explainer.expected_value, shap_values[0], X_train[:1])
shap.summary_plot(shap_values, X_train[:1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14389509126351233, 'fraction_numerical': 0.08895977317029949, 'mean_word_length': 5.479908151549942, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '215450', 'license': 'Apache-2.0', 'n_tokens_mistral': 2555, 'n_tokens_neox': 2189, 'n_words': 349, 'path': 'examples/automl.ipynb', 'repo_name': 'lixfz/DeepTables', 'size': '5643'}