AutoGluon Tabular - 基本功能

Open In Colab Open In SageMaker Studio Lab

通过简单的 fit() 调用,AutoGluon 可以生成高精度的模型,根据数据表中其余列的值预测某一列的值。在处理表格数据时,AutoGluon 可用于分类和回归问题。本教程演示如何使用 AutoGluon 生成一个分类模型,预测一个人的收入是否超过 50,000 美元。

TabularPredictor

首先,导入 AutoGluon 的 TabularPredictorTabularDataset

from autogluon.tabular import TabularDataset, TabularPredictor

CSV 文件加载训练数据到 AutoGluon Dataset 对象中。此对象本质上等同于一个 Pandas DataFrame,两者可以使用相同的方法。

train_data = TabularDataset('https://autogluon.s3.amazonaws.com/datasets/Inc/train.csv')
subsample_size = 500  # subsample subset of data for faster demo, try setting this to much larger values
train_data = train_data.sample(n=subsample_size, random_state=0)
train_data.head()
年龄 工作类型 最终权重 教育程度 教育程度编号 婚姻状况 职业 关系 种族 性别 资本收益 资本损失 每周工时 原籍国 类别
6118 51 私营 39264 大学在读 10 已婚(民事配偶) 执行管理层 妻子 白人 女性 0 0 40 美国 >50K
23204 58 私营 51662 10年级 6 已婚(民事配偶) 其他服务 妻子 白人 女性 0 0 8 美国 <=50K
29590 40 私营 326310 大学在读 10 已婚(民事配偶) 手工业-维修 丈夫 白人 男性 0 0 44 美国 <=50K
18116 37 私营 222450 高中毕业 9 未婚 销售 非家庭成员 白人 男性 0 2339 40 萨尔瓦多 <=50K
33964 62 私营 109190 学士 13 已婚(民事配偶) 执行管理层 丈夫 白人 男性 15024 0 40 美国 >50K

请注意,我们从存储在云端的 CSV 文件加载了数据。如果您已将 CSV 文件下载到本地机器(例如,使用 wget),您也可以指定本地文件路径。表 train_data 中的每一行对应一个训练样本。在此特定数据集中,每一行对应一个人,列中包含人口普查期间报告的各种特征。

我们首先使用这些特征来预测一个人的收入是否超过 50,000 美元,这记录在此表的 class 列中。

label = 'class'
print(f"Unique classes: {list(train_data[label].unique())}")
Unique classes: [' >50K', ' <=50K']

AutoGluon 可处理原始数据,这意味着您在拟合 AutoGluon 之前无需执行任何数据预处理。我们强烈建议您避免执行诸如缺失值插补或独热编码等操作,因为 AutoGluon 具有专门的逻辑来自动处理这些情况。您可以在特征工程教程中了解更多关于 AutoGluon 预处理的信息。

训练

现在,我们仅用一行代码初始化并拟合 AutoGluon 的 TabularPredictor

predictor = TabularPredictor(label=label).fit(train_data)
隐藏代码单元输出
No path specified. Models will be saved in: "AutogluonModels/ag-20250508_205754"
Verbosity: 2 (Standard Logging)
=================== System Info ===================
AutoGluon Version:  1.3.1b20250508
Python Version:     3.11.9
Operating System:   Linux
Platform Machine:   x86_64
Platform Version:   #1 SMP Wed Mar 12 14:53:59 UTC 2025
CPU Count:          8
Memory Avail:       28.77 GB / 30.95 GB (93.0%)
Disk Space Avail:   211.82 GB / 255.99 GB (82.7%)
===================================================
No presets specified! To achieve strong results with AutoGluon, it is recommended to use the available presets. Defaulting to `'medium'`...
	Recommended Presets (For more details refer to https://autogluon.cn/stable/tutorials/tabular/tabular-essentials.html#presets):
	presets='experimental' : New in v1.2: Pre-trained foundation model + parallel fits. The absolute best accuracy without consideration for inference speed. Does not support GPU.
	presets='best'         : Maximize accuracy. Recommended for most users. Use in competitions and benchmarks.
	presets='high'         : Strong accuracy with fast inference speed.
	presets='good'         : Good accuracy with very fast inference speed.
	presets='medium'       : Fast training time, ideal for initial prototyping.
Beginning AutoGluon training ...
AutoGluon will save models to "/home/ci/autogluon/docs/tutorials/tabular/AutogluonModels/ag-20250508_205754"
Train Data Rows:    500
Train Data Columns: 14
Label Column:       class
AutoGluon infers your prediction problem is: 'binary' (because only two unique label-values observed).
	2 unique label values:  [' >50K', ' <=50K']
	If 'binary' is not the correct problem_type, please manually specify the problem_type parameter during Predictor init (You may specify problem_type as one of: ['binary', 'multiclass', 'regression', 'quantile'])
Problem Type:       binary
Preprocessing data ...
Selected class <--> label mapping:  class 1 =  >50K, class 0 =  <=50K
	Note: For your binary classification, AutoGluon arbitrarily selected which label-value represents positive ( >50K) vs negative ( <=50K) class.
	To explicitly set the positive_class, either rename classes to 1 and 0, or specify positive_class in Predictor init.
Using Feature Generators to preprocess the data ...
Fitting AutoMLPipelineFeatureGenerator...
	Available Memory:                    29457.90 MB
	Train Data (Original)  Memory Usage: 0.28 MB (0.0% of available memory)
	Inferring data type of each feature based on column values. Set feature_metadata_in to manually specify special dtypes of the features.
	Stage 1 Generators:
		Fitting AsTypeFeatureGenerator...
			Note: Converting 1 features to boolean dtype as they only contain 2 unique values.
	Stage 2 Generators:
		Fitting FillNaFeatureGenerator...
	Stage 3 Generators:
		Fitting IdentityFeatureGenerator...
		Fitting CategoryFeatureGenerator...
			Fitting CategoryMemoryMinimizeFeatureGenerator...
	Stage 4 Generators:
		Fitting DropUniqueFeatureGenerator...
	Stage 5 Generators:
		Fitting DropDuplicatesFeatureGenerator...
	Types of features in original data (raw dtype, special dtypes):
		('int', [])    : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]
		('object', []) : 8 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]
	Types of features in processed data (raw dtype, special dtypes):
		('category', [])  : 7 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]
		('int', [])       : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]
		('int', ['bool']) : 1 | ['sex']
	0.1s = Fit runtime
	14 features in original data used to generate 14 features in processed data.
	Train Data (Processed) Memory Usage: 0.03 MB (0.0% of available memory)
Data preprocessing and feature engineering runtime = 0.08s ...
AutoGluon will gauge predictive performance using evaluation metric: 'accuracy'
	To change this, specify the eval_metric parameter of Predictor()
Automatically generating train/validation split with holdout_frac=0.2, Train Rows: 400, Val Rows: 100
User-specified model hyperparameters to be fit:
{
	'NN_TORCH': [{}],
	'GBM': [{'extra_trees': True, 'ag_args': {'name_suffix': 'XT'}}, {}, {'learning_rate': 0.03, 'num_leaves': 128, 'feature_fraction': 0.9, 'min_data_in_leaf': 3, 'ag_args': {'name_suffix': 'Large', 'priority': 0, 'hyperparameter_tune_kwargs': None}}],
	'CAT': [{}],
	'XGB': [{}],
	'FASTAI': [{}],
	'RF': [{'criterion': 'gini', 'ag_args': {'name_suffix': 'Gini', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'entropy', 'ag_args': {'name_suffix': 'Entr', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'squared_error', 'ag_args': {'name_suffix': 'MSE', 'problem_types': ['regression', 'quantile']}}],
	'XT': [{'criterion': 'gini', 'ag_args': {'name_suffix': 'Gini', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'entropy', 'ag_args': {'name_suffix': 'Entr', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'squared_error', 'ag_args': {'name_suffix': 'MSE', 'problem_types': ['regression', 'quantile']}}],
	'KNN': [{'weights': 'uniform', 'ag_args': {'name_suffix': 'Unif'}}, {'weights': 'distance', 'ag_args': {'name_suffix': 'Dist'}}],
}
Fitting 13 L1 models, fit_strategy="sequential" ...
Fitting model: KNeighborsUnif ...
	0.73	 = Validation score   (accuracy)
	0.04s	 = Training   runtime
	0.01s	 = Validation runtime
Fitting model: KNeighborsDist ...
	0.65	 = Validation score   (accuracy)
	0.01s	 = Training   runtime
	0.01s	 = Validation runtime
Fitting model: LightGBMXT ...
	0.83	 = Validation score   (accuracy)
	0.26s	 = Training   runtime
	0.0s	 = Validation runtime
Fitting model: LightGBM ...
	0.85	 = Validation score   (accuracy)
	0.21s	 = Training   runtime
	0.0s	 = Validation runtime
Fitting model: RandomForestGini ...
	0.84	 = Validation score   (accuracy)
	0.62s	 = Training   runtime
	0.05s	 = Validation runtime
Fitting model: RandomForestEntr ...
	0.83	 = Validation score   (accuracy)
	0.53s	 = Training   runtime
	0.05s	 = Validation runtime
Fitting model: CatBoost ...
	0.85	 = Validation score   (accuracy)
	0.83s	 = Training   runtime
	0.0s	 = Validation runtime
Fitting model: ExtraTreesGini ...
	0.82	 = Validation score   (accuracy)
	0.54s	 = Training   runtime
	0.06s	 = Validation runtime
Fitting model: ExtraTreesEntr ...
	0.81	 = Validation score   (accuracy)
	0.59s	 = Training   runtime
	0.06s	 = Validation runtime
Fitting model: NeuralNetFastAI ...
	0.84	 = Validation score   (accuracy)
	2.86s	 = Training   runtime
	0.01s	 = Validation runtime
Fitting model: XGBoost ...
	0.85	 = Validation score   (accuracy)
	0.38s	 = Training   runtime
	0.01s	 = Validation runtime
Fitting model: NeuralNetTorch ...
	0.83	 = Validation score   (accuracy)
	2.42s	 = Training   runtime
	0.01s	 = Validation runtime
Fitting model: LightGBMLarge ...
	0.83	 = Validation score   (accuracy)
	0.49s	 = Training   runtime
	0.0s	 = Validation runtime
Fitting model: WeightedEnsemble_L2 ...
	Ensemble Weights: {'LightGBM': 1.0}
	0.85	 = Validation score   (accuracy)
	0.09s	 = Training   runtime
	0.0s	 = Validation runtime
AutoGluon training complete, total runtime = 10.36s ... Best model: WeightedEnsemble_L2 | Estimated inference throughput: 26127.9 rows/s (100 batch size)
Disabling decision threshold calibration for metric `accuracy` due to having fewer than 10000 rows of validation data for calibration, to avoid overfitting (100 rows).
	`accuracy` is generally not improved through threshold calibration. Force calibration via specifying `calibrate_decision_threshold=True`.
TabularPredictor saved. To load, use: predictor = TabularPredictor.load("/home/ci/autogluon/docs/tutorials/tabular/AutogluonModels/ag-20250508_205754")

就是这样!我们现在拥有一个 TabularPredictor,能够在新数据上进行预测。

预测

接下来,加载单独的测试数据,以演示如何在推理时对新样本进行预测

test_data = TabularDataset('https://autogluon.s3.amazonaws.com/datasets/Inc/test.csv')
test_data.head()
Loaded data from: https://autogluon.s3.amazonaws.com/datasets/Inc/test.csv | Columns = 15 / 15 | Rows = 9769 -> 9769
年龄 工作类型 最终权重 教育程度 教育程度编号 婚姻状况 职业 关系 种族 性别 资本收益 资本损失 每周工时 原籍国 类别
0 31 私营 169085 11年级 7 已婚(民事配偶) 销售 妻子 白人 女性 0 0 20 美国 <=50K
1 17 个体经营(非法人) 226203 12年级 8 未婚 销售 亲生子女 白人 男性 0 0 45 美国 <=50K
2 47 私营 54260 职业副学士 11 已婚(民事配偶) 执行管理层 丈夫 白人 男性 0 1887 60 美国 >50K
3 21 私营 176262 大学在读 10 未婚 执行管理层 亲生子女 白人 女性 0 0 30 美国 <=50K
4 17 私营 241185 12年级 8 未婚 专业特长 亲生子女 白人 男性 0 0 20 美国 <=50K

我们现在可以使用训练好的模型对新数据进行预测

y_pred = predictor.predict(test_data)
y_pred.head()  # Predictions
0     <=50K
1     <=50K
2      >50K
3     <=50K
4     <=50K
Name: class, dtype: object
y_pred_proba = predictor.predict_proba(test_data)
y_pred_proba.head()  # Prediction Probabilities
<=50K >50K
0 0.949797 0.050203
1 0.945973 0.054027
2 0.433299 0.566701
3 0.991393 0.008607
4 0.949908 0.050092

评估

接下来,我们可以在(带标签的)测试数据上评估预测器

predictor.evaluate(test_data)
{'accuracy': 0.8397993653393387,
 'balanced_accuracy': np.float64(0.7437076677780596),
 'mcc': np.float64(0.5295565206264157),
 'roc_auc': np.float64(0.8841789655024272),
 'f1': 0.6242496998799519,
 'precision': 0.7038440714672441,
 'recall': 0.5608283002588438}

我们也可以单独评估每个模型

predictor.leaderboard(test_data)
模型 score_test score_val 评估指标 pred_time_test pred_time_val 拟合时间 pred_time_test_marginal pred_time_val_marginal fit_time_marginal 堆叠层级 可推理 拟合顺序
0 RandomForestGini 0.842870 0.84 准确率 0.120806 0.048270 0.623644 0.120806 0.048270 0.623644 1 True 5
1 CatBoost 0.842461 0.85 准确率 0.009868 0.003890 0.831370 0.009868 0.003890 0.831370 1 True 7
2 RandomForestEntr 0.841130 0.83 准确率 0.116266 0.046257 0.527118 0.116266 0.046257 0.527118 1 True 6
3 XGBoost 0.839902 0.85 准确率 0.065022 0.006067 0.378623 0.065022 0.006067 0.378623 1 True 11
4 LightGBM 0.839799 0.85 准确率 0.020771 0.003047 0.212612 0.020771 0.003047 0.212612 1 True 4
5 WeightedEnsemble_L2 0.839799 0.85 准确率 0.022450 0.003827 0.300721 0.001679 0.000780 0.088109 2 True 14
6 LightGBMXT 0.836421 0.83 准确率 0.011252 0.003632 0.259340 0.011252 0.003632 0.259340 1 True 3
7 ExtraTreesGini 0.833862 0.82 准确率 0.108191 0.057446 0.540293 0.108191 0.057446 0.540293 1 True 8
8 ExtraTreesEntr 0.833862 0.81 准确率 0.110759 0.057617 0.592023 0.110759 0.057617 0.592023 1 True 9
9 NeuralNetTorch 0.833657 0.83 准确率 0.050072 0.010545 2.418256 0.050072 0.010545 2.418256 1 True 12
10 NeuralNetFastAI 0.828949 0.84 准确率 0.136036 0.009065 2.855326 0.136036 0.009065 2.855326 1 True 10
11 LightGBMLarge 0.817074 0.83 准确率 0.012573 0.003610 0.486884 0.012573 0.003610 0.486884 1 True 13
12 KNeighborsUnif 0.725970 0.73 准确率 0.031042 0.014811 0.035085 0.031042 0.014811 0.035085 1 True 1
13 KNeighborsDist 0.695158 0.65 准确率 0.028719 0.013254 0.008521 0.028719 0.013254 0.008521 1 True 2

加载训练好的预测器

最后,我们可以通过调用 TabularPredictor.load() 并指定磁盘上预测器artifact的位置,在新的会话(或新机器)中加载预测器。

predictor.path  # The path on disk where the predictor is saved
'/home/ci/autogluon/docs/tutorials/tabular/AutogluonModels/ag-20250508_205754'
# Load the predictor by specifying the path it is saved to on disk.
# You can control where it is saved to by setting the `path` parameter during init
predictor = TabularPredictor.load(predictor.path)

警告

TabularPredictor.load() 隐式使用了 pickle 模块,该模块已知不安全。可以构造恶意 pickle 数据,在反序列化期间执行任意代码。切勿加载可能来自不受信任来源或可能已被篡改的数据。仅加载您信任的数据。

现在您可以开始在您自己的表格数据集上试用 AutoGluon 了!只要数据以流行的格式(如 CSV)存储,您只需 2 行代码即可获得强大的预测性能

from autogluon.tabular import TabularPredictor
predictor = TabularPredictor(label=<variable-name>).fit(train_data=<file-name>)

注意:此对 TabularPredictor.fit() 的简单调用适用于您的第一个原型模型。在后续章节中,我们将演示如何通过额外指定 fit()presets 参数和 TabularPredictor()eval_metric 参数来最大化预测性能。

fit() 的说明

在此,我们将讨论 fit() 期间发生了什么。

由于 class 变量只有两个可能的值,这是一个二元分类问题,其合适的性能评估指标是 准确率。AutoGluon 会自动推断出这一点,以及每个特征的类型(即哪些列包含连续数值,哪些列包含离散类别)。AutoGluon 还可以自动处理常见问题,如缺失数据和特征值缩放。

我们没有指定单独的验证数据,因此 AutoGluon 自动选择了数据的随机训练/验证划分。用于验证的数据与训练数据分开,用于确定产生最佳结果的模型和超参数值。AutoGluon 不仅训练单个模型,还会训练多个模型并将它们集成在一起,以获得卓越的预测性能。

默认情况下,AutoGluon 会尝试拟合各种类型的模型,包括神经网络和树集成模型。每种类型的模型都有各种超参数,传统上需要用户指定。AutoGluon 自动化了这个过程。

AutoGluon 会自动并迭代地测试超参数值,以在验证数据上获得最佳性能。这涉及在不同的超参数设置下重复训练模型并评估其性能。此过程可能计算密集,因此 fit() 使用 Ray 在多个线程上并行化此过程。要控制运行时长,您可以在 fit() 中指定各种参数,例如 time_limit,这将在后续的 深入教程 中进行演示。

我们可以查看 AutoGluon 自动推断出的关于我们预测任务的属性

print("AutoGluon infers problem type is: ", predictor.problem_type)
print("AutoGluon identified the following types of features:")
print(predictor.feature_metadata)
AutoGluon infers problem type is:  binary
AutoGluon identified the following types of features:
('category', [])  : 7 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]
('int', [])       : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]
('int', ['bool']) : 1 | ['sex']

AutoGluon 正确地将我们的预测问题识别为二元分类任务,并确定像 age 这样的变量应表示为整数,而像 workclass 这样的变量应表示为类别对象。feature_metadata 属性允许您查看预处理后每个预测变量推断的数据类型(这是其 原始 dtype;通过特征工程生成的一些特征也可能与额外的 特殊 dtype 相关联,例如 datetime/文本列的数值表示)。

要将数据转换为 AutoGluon 的内部表示,我们可以执行以下操作

test_data_transform = predictor.transform_features(test_data)
test_data_transform.head()
年龄 最终权重 教育程度编号 性别 资本收益 资本损失 每周工时 工作类型 教育程度 婚姻状况 职业 关系 种族 原籍国
0 31 169085 7 0 0 0 20 3 1 1 10 5 4 14
1 17 226203 8 1 0 0 45 5 2 3 10 3 4 14
2 47 54260 11 1 0 1887 60 3 7 1 3 0 4 14
3 21 176262 10 0 0 0 30 3 13 3 3 3 4 14
4 17 241185 8 1 0 0 20 3 2 3 8 3 4 14

请注意,预处理后数据如何完全变成数值类型(尽管下游仍会将分类特征视为分类)。

为了更好地理解我们训练好的预测器,我们可以通过 TabularPredictor.feature_importance() 估计每个特征的整体重要性

predictor.feature_importance(test_data)
Computing feature importance via permutation shuffling for 14 features using 5000 rows with 5 shuffle sets...
	1.85s	= Expected runtime (0.37s per shuffle set)
	1.27s	= Actual runtime (Completed 5 of 5 shuffle sets)
重要性 标准差 p值 n p99_high p99_low
婚姻状况 0.05608 0.003399 0.000002 5 0.063078 0.049082
资本收益 0.03204 0.001873 0.000001 5 0.035896 0.028184
教育程度编号 0.02688 0.003294 0.000027 5 0.033663 0.020097
年龄 0.01084 0.002655 0.000399 5 0.016306 0.005374
每周工时 0.00780 0.002302 0.000814 5 0.012540 0.003060
职业 0.00756 0.002934 0.002251 5 0.013601 0.001519
性别 0.00084 0.001081 0.078605 5 0.003065 -0.001385
种族 0.00000 0.000000 0.500000 5 0.000000 0.000000
工作类型 0.00000 0.000000 0.500000 5 0.000000 0.000000
教育程度 0.00000 0.000000 0.500000 5 0.000000 0.000000
资本损失 0.00000 0.000000 0.500000 5 0.000000 0.000000
关系 0.00000 0.000000 0.500000 5 0.000000 0.000000
原籍国 0.00000 0.000000 0.500000 5 0.000000 0.000000
最终权重 -0.00040 0.002753 0.619220 5 0.005269 -0.006069

importance 列是评估指标分数在移除该特征后可能下降量的估计。负的 importance 值意味着如果在移除该特征后重新拟合,结果可能会得到改善。

当我们调用 predict() 时,AutoGluon 会自动使用在验证数据上表现最佳的模型(即加权集成模型)进行预测。

predictor.model_best
'WeightedEnsemble_L2'

我们可以改为这样指定用于预测的模型

predictor.predict(test_data, model='LightGBM')

您可以通过 .leaderboard().model_names() 获取训练模型的列表

predictor.model_names()
['KNeighborsUnif',
 'KNeighborsDist',
 'LightGBMXT',
 'LightGBM',
 'RandomForestGini',
 'RandomForestEntr',
 'CatBoost',
 'ExtraTreesGini',
 'ExtraTreesEntr',
 'NeuralNetFastAI',
 'XGBoost',
 'NeuralNetTorch',
 'LightGBMLarge',
 'WeightedEnsemble_L2']

上面显示的预测性能分数基于默认评估指标(二元分类的准确率)。在某些应用中,性能可能使用不同于 AutoGluon 默认优化的指标来衡量。如果您知道在您的应用中重要的评估指标,您应该通过 eval_metric 参数指定它,如下一节所示。

预设

AutoGluon 提供多种预设,可以在调用 .fit 时通过 presets 参数指定。默认使用 medium_quality 以鼓励初步原型开发,但对于正式使用,应使用其他预设。

预设

模型质量

用例

拟合时间(理想)

推理时间(相对于 medium_quality)

磁盘使用量

best_quality

最先进 (SOTA),远优于 high_quality

当准确率最重要时

16倍+

32倍+

16倍+

high_quality

优于 good_quality

需要功能强大、易于移植且推理速度快的解决方案时:大规模批量推理

16倍+

4倍

2倍

good_quality

强于任何其他 AutoML 框架

需要功能强大、高度可移植且推理速度非常快的解决方案时:十亿规模批量推理、小于 100 毫秒的在线推理、边缘设备

16倍

2倍

0.1倍

medium_quality

与其他顶级 AutoML 框架具有竞争力

初步原型开发,建立性能基准

1倍

1倍

1倍

我们建议用户从 medium_quality 开始,以便了解问题并发现任何数据相关问题。如果 medium_quality 训练时间过长,可以考虑在此原型开发阶段对训练数据进行子采样。
一旦您熟悉了,接下来尝试 best_quality。确保指定的 time_limit 值至少是用于 medium_quality 的 16 倍。完成后,您应该会得到一个非常强大的解决方案,其性能通常优于 medium_quality
请务必考虑保留 AutoGluon 在训练期间从未见过测试数据,以确保模型在性能方面表现如预期。
在评估了 best_qualitymedium_quality 后,检查其中是否有满足您需求的。如果都没有,考虑尝试 high_quality 和/或 good_quality
如果所有预设都无法满足要求,请参阅预测表格中的列 - 深入了解,以获取更高级的 AutoGluon 选项。

最大化预测性能

注意:如果您正在对 AutoGluon-Tabular 进行基准测试或希望最大化其准确性,则不应使用完全默认的参数调用 fit()!要使用 AutoGluon 获得最佳预测准确率,通常应按以下方式使用它

time_limit = 60  # for quick demonstration only, you should set this to longest time you are willing to wait (in seconds)
metric = 'roc_auc'  # specify your evaluation metric here
predictor = TabularPredictor(label, eval_metric=metric).fit(train_data, time_limit=time_limit, presets='best_quality')
隐藏代码单元输出
(_ray_fit pid=7665) [1000]	valid_set's binary_logloss: 0.270008
(_ray_fit pid=7665) [2000]	valid_set's binary_logloss: 0.252973
No path specified. Models will be saved in: "AutogluonModels/ag-20250508_205807"
Verbosity: 2 (Standard Logging)
=================== System Info ===================
AutoGluon Version:  1.3.1b20250508
Python Version:     3.11.9
Operating System:   Linux
Platform Machine:   x86_64
Platform Version:   #1 SMP Wed Mar 12 14:53:59 UTC 2025
CPU Count:          8
Memory Avail:       28.22 GB / 30.95 GB (91.2%)
Disk Space Avail:   211.81 GB / 255.99 GB (82.7%)
===================================================
Presets specified: ['best_quality']
Setting dynamic_stacking from 'auto' to True. Reason: Enable dynamic_stacking when use_bag_holdout is disabled. (use_bag_holdout=False)
Stack configuration (auto_stack=True): num_stack_levels=1, num_bag_folds=8, num_bag_sets=1
DyStack is enabled (dynamic_stacking=True). AutoGluon will try to determine whether the input data is affected by stacked overfitting and enable or disable stacking as a consequence.
	This is used to identify the optimal `num_stack_levels` value. Copies of AutoGluon will be fit on subsets of the data. Then holdout validation data is used to detect stacked overfitting.
	Running DyStack for up to 15s of the 60s of remaining time (25%).
	Running DyStack sub-fit in a ray process to avoid memory leakage. Enabling ray logging (enable_ray_logging=True). Specify `ds_args={'enable_ray_logging': False}` if you experience logging issues.
2025-05-08 20:58:10,922	INFO worker.py:1843 -- Started a local Ray instance. View the dashboard at http://127.0.0.1:8265 
		Context path: "/home/ci/autogluon/docs/tutorials/tabular/AutogluonModels/ag-20250508_205807/ds_sub_fit/sub_fit_ho"
(_dystack pid=7249) Running DyStack sub-fit ...
(_dystack pid=7249) Beginning AutoGluon training ... Time limit = 11s
(_dystack pid=7249) AutoGluon will save models to "/home/ci/autogluon/docs/tutorials/tabular/AutogluonModels/ag-20250508_205807/ds_sub_fit/sub_fit_ho"
(_dystack pid=7249) Train Data Rows:    444
(_dystack pid=7249) Train Data Columns: 14
(_dystack pid=7249) Label Column:       class
(_dystack pid=7249) Problem Type:       binary
(_dystack pid=7249) Preprocessing data ...
(_dystack pid=7249) Selected class <--> label mapping:  class 1 =  >50K, class 0 =  <=50K
(_dystack pid=7249) 	Note: For your binary classification, AutoGluon arbitrarily selected which label-value represents positive ( >50K) vs negative ( <=50K) class.
(_dystack pid=7249) 	To explicitly set the positive_class, either rename classes to 1 and 0, or specify positive_class in Predictor init.
(_dystack pid=7249) Using Feature Generators to preprocess the data ...
(_dystack pid=7249) Fitting AutoMLPipelineFeatureGenerator...
(_dystack pid=7249) 	Available Memory:                    28326.98 MB
(_dystack pid=7249) 	Train Data (Original)  Memory Usage: 0.25 MB (0.0% of available memory)
(_dystack pid=7249) 	Inferring data type of each feature based on column values. Set feature_metadata_in to manually specify special dtypes of the features.
(_dystack pid=7249) 	Stage 1 Generators:
(_dystack pid=7249) 		Fitting AsTypeFeatureGenerator...
(_dystack pid=7249) 			Note: Converting 1 features to boolean dtype as they only contain 2 unique values.
(_dystack pid=7249) 	Stage 2 Generators:
(_dystack pid=7249) 		Fitting FillNaFeatureGenerator...
(_dystack pid=7249) 	Stage 3 Generators:
(_dystack pid=7249) 		Fitting IdentityFeatureGenerator...
(_dystack pid=7249) 		Fitting CategoryFeatureGenerator...
(_dystack pid=7249) 			Fitting CategoryMemoryMinimizeFeatureGenerator...
(_dystack pid=7249) 	Stage 4 Generators:
(_dystack pid=7249) 		Fitting DropUniqueFeatureGenerator...
(_dystack pid=7249) 	Stage 5 Generators:
(_dystack pid=7249) 		Fitting DropDuplicatesFeatureGenerator...
(_dystack pid=7249) 	Types of features in original data (raw dtype, special dtypes):
(_dystack pid=7249) 		('int', [])    : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]
(_dystack pid=7249) 		('object', []) : 8 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]
(_dystack pid=7249) 	Types of features in processed data (raw dtype, special dtypes):
(_dystack pid=7249) 		('category', [])  : 7 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]
(_dystack pid=7249) 		('int', [])       : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]
(_dystack pid=7249) 		('int', ['bool']) : 1 | ['sex']
(_dystack pid=7249) 	0.1s = Fit runtime
(_dystack pid=7249) 	14 features in original data used to generate 14 features in processed data.
(_dystack pid=7249) 	Train Data (Processed) Memory Usage: 0.03 MB (0.0% of available memory)
(_dystack pid=7249) Data preprocessing and feature engineering runtime = 0.07s ...
(_dystack pid=7249) AutoGluon will gauge predictive performance using evaluation metric: 'roc_auc'
(_dystack pid=7249) 	This metric expects predicted probabilities rather than predicted class labels, so you'll need to use predict_proba() instead of predict()
(_dystack pid=7249) 	To change this, specify the eval_metric parameter of Predictor()
(_dystack pid=7249) Large model count detected (112 configs) ... Only displaying the first 3 models of each family. To see all, set `verbosity=3`.
(_dystack pid=7249) User-specified model hyperparameters to be fit:
(_dystack pid=7249) {
(_dystack pid=7249) 	'NN_TORCH': [{}, {'activation': 'elu', 'dropout_prob': 0.10077639529843717, 'hidden_size': 108, 'learning_rate': 0.002735937344002146, 'num_layers': 4, 'use_batchnorm': True, 'weight_decay': 1.356433327634438e-12, 'ag_args': {'name_suffix': '_r79', 'priority': -2}}, {'activation': 'elu', 'dropout_prob': 0.11897478034205347, 'hidden_size': 213, 'learning_rate': 0.0010474382260641949, 'num_layers': 4, 'use_batchnorm': False, 'weight_decay': 5.594471067786272e-10, 'ag_args': {'name_suffix': '_r22', 'priority': -7}}],
(_dystack pid=7249) 	'GBM': [{'extra_trees': True, 'ag_args': {'name_suffix': 'XT'}}, {}, {'learning_rate': 0.03, 'num_leaves': 128, 'feature_fraction': 0.9, 'min_data_in_leaf': 3, 'ag_args': {'name_suffix': 'Large', 'priority': 0, 'hyperparameter_tune_kwargs': None}}],
(_dystack pid=7249) 	'CAT': [{}, {'depth': 6, 'grow_policy': 'SymmetricTree', 'l2_leaf_reg': 2.1542798306067823, 'learning_rate': 0.06864209415792857, 'max_ctr_complexity': 4, 'one_hot_max_size': 10, 'ag_args': {'name_suffix': '_r177', 'priority': -1}}, {'depth': 8, 'grow_policy': 'Depthwise', 'l2_leaf_reg': 2.7997999596449104, 'learning_rate': 0.031375015734637225, 'max_ctr_complexity': 2, 'one_hot_max_size': 3, 'ag_args': {'name_suffix': '_r9', 'priority': -5}}],
(_dystack pid=7249) 	'XGB': [{}, {'colsample_bytree': 0.6917311125174739, 'enable_categorical': False, 'learning_rate': 0.018063876087523967, 'max_depth': 10, 'min_child_weight': 0.6028633586934382, 'ag_args': {'name_suffix': '_r33', 'priority': -8}}, {'colsample_bytree': 0.6628423832084077, 'enable_categorical': False, 'learning_rate': 0.08775715546881824, 'max_depth': 5, 'min_child_weight': 0.6294123374222513, 'ag_args': {'name_suffix': '_r89', 'priority': -16}}],
(_dystack pid=7249) 	'FASTAI': [{}, {'bs': 256, 'emb_drop': 0.5411770367537934, 'epochs': 43, 'layers': [800, 400], 'lr': 0.01519848858318159, 'ps': 0.23782946566604385, 'ag_args': {'name_suffix': '_r191', 'priority': -4}}, {'bs': 2048, 'emb_drop': 0.05070411322605811, 'epochs': 29, 'layers': [200, 100], 'lr': 0.08974235041576624, 'ps': 0.10393466140748028, 'ag_args': {'name_suffix': '_r102', 'priority': -11}}],
(_dystack pid=7249) 	'RF': [{'criterion': 'gini', 'ag_args': {'name_suffix': 'Gini', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'entropy', 'ag_args': {'name_suffix': 'Entr', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'squared_error', 'ag_args': {'name_suffix': 'MSE', 'problem_types': ['regression', 'quantile']}}],
(_dystack pid=7249) 	'XT': [{'criterion': 'gini', 'ag_args': {'name_suffix': 'Gini', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'entropy', 'ag_args': {'name_suffix': 'Entr', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'squared_error', 'ag_args': {'name_suffix': 'MSE', 'problem_types': ['regression', 'quantile']}}],
(_dystack pid=7249) 	'KNN': [{'weights': 'uniform', 'ag_args': {'name_suffix': 'Unif'}}, {'weights': 'distance', 'ag_args': {'name_suffix': 'Dist'}}],
(_dystack pid=7249) }
(_dystack pid=7249) AutoGluon will fit 2 stack levels (L1 to L2) ...
(_dystack pid=7249) Fitting 110 L1 models, fit_strategy="sequential" ...
(_dystack pid=7249) Fitting model: KNeighborsUnif_BAG_L1 ... Training model for up to 7.16s of the 10.74s of remaining time.
(_dystack pid=7249) 	0.5271	 = Validation score   (roc_auc)
(_dystack pid=7249) 	0.0s	 = Training   runtime
(_dystack pid=7249) 	0.02s	 = Validation runtime
(_dystack pid=7249) Fitting model: KNeighborsDist_BAG_L1 ... Training model for up to 7.11s of the 10.69s of remaining time.
(_dystack pid=7249) 	0.5389	 = Validation score   (roc_auc)
(_dystack pid=7249) 	0.0s	 = Training   runtime
(_dystack pid=7249) 	0.01s	 = Validation runtime
(_dystack pid=7249) Fitting model: LightGBMXT_BAG_L1 ... Training model for up to 7.08s of the 10.66s of remaining time.
(_dystack pid=7249) 	Fitting 8 child models (S1F1 - S1F8) | Fitting with ParallelLocalFoldFittingStrategy (8 workers, per: cpus=1, gpus=0, memory=0.02%)
(_dystack pid=7249) 	0.8895	 = Validation score   (roc_auc)
(_dystack pid=7249) 	0.8s	 = Training   runtime
(_dystack pid=7249) 	0.05s	 = Validation runtime
(_dystack pid=7249) Fitting model: LightGBM_BAG_L1 ... Training model for up to 3.25s of the 6.83s of remaining time.
(_dystack pid=7249) 	Fitting 8 child models (S1F1 - S1F8) | Fitting with ParallelLocalFoldFittingStrategy (8 workers, per: cpus=1, gpus=0, memory=0.02%)
(_dystack pid=7249) 	0.8693	 = Validation score   (roc_auc)
(_dystack pid=7249) 	0.65s	 = Training   runtime
(_dystack pid=7249) 	0.04s	 = Validation runtime
(_dystack pid=7249) Fitting model: WeightedEnsemble_L2 ... Training model for up to 10.75s of the 2.68s of remaining time.
(_dystack pid=7249) 	Ensemble Weights: {'LightGBMXT_BAG_L1': 1.0}
(_dystack pid=7249) 	0.8895	 = Validation score   (roc_auc)
(_dystack pid=7249) 	0.02s	 = Training   runtime
(_dystack pid=7249) 	0.0s	 = Validation runtime
(_dystack pid=7249) Fitting 108 L2 models, fit_strategy="sequential" ...
(_dystack pid=7249) Fitting model: LightGBMXT_BAG_L2 ... Training model for up to 2.63s of the 2.61s of remaining time.
(_dystack pid=7249) 	Fitting 8 child models (S1F1 - S1F8) | Fitting with ParallelLocalFoldFittingStrategy (8 workers, per: cpus=1, gpus=0, memory=0.02%)
(_dystack pid=7249) 	0.876	 = Validation score   (roc_auc)
(_dystack pid=7249) 	0.87s	 = Training   runtime
(_dystack pid=7249) 	0.05s	 = Validation runtime
(_dystack pid=7249) Fitting model: WeightedEnsemble_L3 ... Training model for up to 10.75s of the -1.53s of remaining time.
(_dystack pid=7249) 	Ensemble Weights: {'LightGBMXT_BAG_L1': 1.0}
(_dystack pid=7249) 	0.8895	 = Validation score   (roc_auc)
(_dystack pid=7249) 	0.02s	 = Training   runtime
(_dystack pid=7249) 	0.0s	 = Validation runtime
(_dystack pid=7249) AutoGluon training complete, total runtime = 12.42s ... Best model: WeightedEnsemble_L2 | Estimated inference throughput: 1151.9 rows/s (56 batch size)
(_dystack pid=7249) TabularPredictor saved. To load, use: predictor = TabularPredictor.load("/home/ci/autogluon/docs/tutorials/tabular/AutogluonModels/ag-20250508_205807/ds_sub_fit/sub_fit_ho")
(_dystack pid=7249) Deleting DyStack predictor artifacts (clean_up_fits=True) ...
Leaderboard on holdout data (DyStack):
                   model  score_holdout  score_val eval_metric  pred_time_test  pred_time_val  fit_time  pred_time_test_marginal  pred_time_val_marginal  fit_time_marginal  stack_level  can_infer  fit_order
0      LightGBMXT_BAG_L1       0.918699   0.889480     roc_auc        0.423165       0.048561  0.799887                 0.423165                0.048561           0.799887            1       True          3
1    WeightedEnsemble_L2       0.918699   0.889480     roc_auc        0.424993       0.049006  0.815953                 0.001828                0.000445           0.016066            2       True          5
2    WeightedEnsemble_L3       0.918699   0.889480     roc_auc        0.425017       0.049214  0.823127                 0.001852                0.000654           0.023240            3       True          7
3      LightGBMXT_BAG_L2       0.918699   0.876029     roc_auc        0.484090       0.112550  1.675859                 0.045424                0.050777           0.872793            2       True          6
4        LightGBM_BAG_L1       0.897561   0.869264     roc_auc        0.048833       0.043195  0.652257                 0.048833                0.043195           0.652257            1       True          4
5  KNeighborsUnif_BAG_L1       0.573171   0.527070     roc_auc        0.044503       0.016067  0.003655                 0.044503                0.016067           0.003655            1       True          1
6  KNeighborsDist_BAG_L1       0.556098   0.538940     roc_auc        0.015501       0.013213  0.003179                 0.015501                0.013213           0.003179            1       True          2
	1	 = Optimal   num_stack_levels (Stacked Overfitting Occurred: False)
	19s	 = DyStack   runtime |	41s	 = Remaining runtime
Starting main fit with num_stack_levels=1.
	For future fit calls on this dataset, you can skip DyStack to save time: `predictor.fit(..., dynamic_stacking=False, num_stack_levels=1)`
Beginning AutoGluon training ... Time limit = 41s
AutoGluon will save models to "/home/ci/autogluon/docs/tutorials/tabular/AutogluonModels/ag-20250508_205807"
Train Data Rows:    500
Train Data Columns: 14
Label Column:       class
Problem Type:       binary
Preprocessing data ...
Selected class <--> label mapping:  class 1 =  >50K, class 0 =  <=50K
	Note: For your binary classification, AutoGluon arbitrarily selected which label-value represents positive ( >50K) vs negative ( <=50K) class.
	To explicitly set the positive_class, either rename classes to 1 and 0, or specify positive_class in Predictor init.
Using Feature Generators to preprocess the data ...
Fitting AutoMLPipelineFeatureGenerator...
	Available Memory:                    28281.05 MB
	Train Data (Original)  Memory Usage: 0.28 MB (0.0% of available memory)
	Inferring data type of each feature based on column values. Set feature_metadata_in to manually specify special dtypes of the features.
	Stage 1 Generators:
		Fitting AsTypeFeatureGenerator...
			Note: Converting 1 features to boolean dtype as they only contain 2 unique values.
	Stage 2 Generators:
		Fitting FillNaFeatureGenerator...
	Stage 3 Generators:
		Fitting IdentityFeatureGenerator...
		Fitting CategoryFeatureGenerator...
			Fitting CategoryMemoryMinimizeFeatureGenerator...
	Stage 4 Generators:
		Fitting DropUniqueFeatureGenerator...
	Stage 5 Generators:
		Fitting DropDuplicatesFeatureGenerator...
	Types of features in original data (raw dtype, special dtypes):
		('int', [])    : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]
		('object', []) : 8 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]
	Types of features in processed data (raw dtype, special dtypes):
		('category', [])  : 7 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]
		('int', [])       : 6 | ['age', 'fnlwgt', 'education-num', 'capital-gain', 'capital-loss', ...]
		('int', ['bool']) : 1 | ['sex']
	0.1s = Fit runtime
	14 features in original data used to generate 14 features in processed data.
	Train Data (Processed) Memory Usage: 0.03 MB (0.0% of available memory)
Data preprocessing and feature engineering runtime = 0.1s ...
AutoGluon will gauge predictive performance using evaluation metric: 'roc_auc'
	This metric expects predicted probabilities rather than predicted class labels, so you'll need to use predict_proba() instead of predict()
	To change this, specify the eval_metric parameter of Predictor()
Large model count detected (112 configs) ... Only displaying the first 3 models of each family. To see all, set `verbosity=3`.
User-specified model hyperparameters to be fit:
{
	'NN_TORCH': [{}, {'activation': 'elu', 'dropout_prob': 0.10077639529843717, 'hidden_size': 108, 'learning_rate': 0.002735937344002146, 'num_layers': 4, 'use_batchnorm': True, 'weight_decay': 1.356433327634438e-12, 'ag_args': {'name_suffix': '_r79', 'priority': -2}}, {'activation': 'elu', 'dropout_prob': 0.11897478034205347, 'hidden_size': 213, 'learning_rate': 0.0010474382260641949, 'num_layers': 4, 'use_batchnorm': False, 'weight_decay': 5.594471067786272e-10, 'ag_args': {'name_suffix': '_r22', 'priority': -7}}],
	'GBM': [{'extra_trees': True, 'ag_args': {'name_suffix': 'XT'}}, {}, {'learning_rate': 0.03, 'num_leaves': 128, 'feature_fraction': 0.9, 'min_data_in_leaf': 3, 'ag_args': {'name_suffix': 'Large', 'priority': 0, 'hyperparameter_tune_kwargs': None}}],
	'CAT': [{}, {'depth': 6, 'grow_policy': 'SymmetricTree', 'l2_leaf_reg': 2.1542798306067823, 'learning_rate': 0.06864209415792857, 'max_ctr_complexity': 4, 'one_hot_max_size': 10, 'ag_args': {'name_suffix': '_r177', 'priority': -1}}, {'depth': 8, 'grow_policy': 'Depthwise', 'l2_leaf_reg': 2.7997999596449104, 'learning_rate': 0.031375015734637225, 'max_ctr_complexity': 2, 'one_hot_max_size': 3, 'ag_args': {'name_suffix': '_r9', 'priority': -5}}],
	'XGB': [{}, {'colsample_bytree': 0.6917311125174739, 'enable_categorical': False, 'learning_rate': 0.018063876087523967, 'max_depth': 10, 'min_child_weight': 0.6028633586934382, 'ag_args': {'name_suffix': '_r33', 'priority': -8}}, {'colsample_bytree': 0.6628423832084077, 'enable_categorical': False, 'learning_rate': 0.08775715546881824, 'max_depth': 5, 'min_child_weight': 0.6294123374222513, 'ag_args': {'name_suffix': '_r89', 'priority': -16}}],
	'FASTAI': [{}, {'bs': 256, 'emb_drop': 0.5411770367537934, 'epochs': 43, 'layers': [800, 400], 'lr': 0.01519848858318159, 'ps': 0.23782946566604385, 'ag_args': {'name_suffix': '_r191', 'priority': -4}}, {'bs': 2048, 'emb_drop': 0.05070411322605811, 'epochs': 29, 'layers': [200, 100], 'lr': 0.08974235041576624, 'ps': 0.10393466140748028, 'ag_args': {'name_suffix': '_r102', 'priority': -11}}],
	'RF': [{'criterion': 'gini', 'ag_args': {'name_suffix': 'Gini', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'entropy', 'ag_args': {'name_suffix': 'Entr', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'squared_error', 'ag_args': {'name_suffix': 'MSE', 'problem_types': ['regression', 'quantile']}}],
	'XT': [{'criterion': 'gini', 'ag_args': {'name_suffix': 'Gini', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'entropy', 'ag_args': {'name_suffix': 'Entr', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'squared_error', 'ag_args': {'name_suffix': 'MSE', 'problem_types': ['regression', 'quantile']}}],
	'KNN': [{'weights': 'uniform', 'ag_args': {'name_suffix': 'Unif'}}, {'weights': 'distance', 'ag_args': {'name_suffix': 'Dist'}}],
}
AutoGluon will fit 2 stack levels (L1 to L2) ...
Fitting 110 L1 models, fit_strategy="sequential" ...
Fitting model: KNeighborsUnif_BAG_L1 ... Training model for up to 27.27s of the 40.90s of remaining time.
	0.5196	 = Validation score   (roc_auc)
	0.0s	 = Training   runtime
	0.02s	 = Validation runtime
Fitting model: KNeighborsDist_BAG_L1 ... Training model for up to 27.24s of the 40.88s of remaining time.
	0.537	 = Validation score   (roc_auc)
	0.0s	 = Training   runtime
	0.01s	 = Validation runtime
Fitting model: LightGBMXT_BAG_L1 ... Training model for up to 27.22s of the 40.85s of remaining time.
	Fitting 8 child models (S1F1 - S1F8) | Fitting with ParallelLocalFoldFittingStrategy (8 workers, per: cpus=1, gpus=0, memory=0.02%)
	0.8912	 = Validation score   (roc_auc)
	0.75s	 = Training   runtime
	0.04s	 = Validation runtime
Fitting model: LightGBM_BAG_L1 ... Training model for up to 23.63s of the 37.27s of remaining time.
	Fitting 8 child models (S1F1 - S1F8) | Fitting with ParallelLocalFoldFittingStrategy (8 workers, per: cpus=1, gpus=0, memory=0.02%)
	0.8799	 = Validation score   (roc_auc)
	0.97s	 = Training   runtime
	0.04s	 = Validation runtime
Fitting model: RandomForestGini_BAG_L1 ... Training model for up to 19.48s of the 33.11s of remaining time.
/home/ci/opt/venv/lib/python3.11/site-packages/sklearn/base.py:474: FutureWarning: `BaseEstimator._validate_data` is deprecated in 1.6 and will be removed in 1.7. Use `sklearn.utils.validation.validate_data` instead. This function becomes public and is part of the scikit-learn developer API.
  warnings.warn(
	0.8879	 = Validation score   (roc_auc)
	0.96s	 = Training   runtime
	0.11s	 = Validation runtime
Fitting model: RandomForestEntr_BAG_L1 ... Training model for up to 18.39s of the 32.02s of remaining time.
/home/ci/opt/venv/lib/python3.11/site-packages/sklearn/base.py:474: FutureWarning: `BaseEstimator._validate_data` is deprecated in 1.6 and will be removed in 1.7. Use `sklearn.utils.validation.validate_data` instead. This function becomes public and is part of the scikit-learn developer API.
  warnings.warn(
	0.8899	 = Validation score   (roc_auc)
	0.54s	 = Training   runtime
	0.11s	 = Validation runtime
Fitting model: CatBoost_BAG_L1 ... Training model for up to 17.72s of the 31.36s of remaining time.
	Fitting 8 child models (S1F1 - S1F8) | Fitting with ParallelLocalFoldFittingStrategy (8 workers, per: cpus=1, gpus=0, memory=1.39%)
	0.8902	 = Validation score   (roc_auc)
	5.82s	 = Training   runtime
	0.04s	 = Validation runtime
Fitting model: ExtraTreesGini_BAG_L1 ... Training model for up to 9.08s of the 22.72s of remaining time.
/home/ci/opt/venv/lib/python3.11/site-packages/sklearn/base.py:474: FutureWarning: `BaseEstimator._validate_data` is deprecated in 1.6 and will be removed in 1.7. Use `sklearn.utils.validation.validate_data` instead. This function becomes public and is part of the scikit-learn developer API.
  warnings.warn(
	0.8958	 = Validation score   (roc_auc)
	0.57s	 = Training   runtime
	0.11s	 = Validation runtime
Fitting model: ExtraTreesEntr_BAG_L1 ... Training model for up to 8.38s of the 22.02s of remaining time.
/home/ci/opt/venv/lib/python3.11/site-packages/sklearn/base.py:474: FutureWarning: `BaseEstimator._validate_data` is deprecated in 1.6 and will be removed in 1.7. Use `sklearn.utils.validation.validate_data` instead. This function becomes public and is part of the scikit-learn developer API.
  warnings.warn(
	0.8904	 = Validation score   (roc_auc)
	0.52s	 = Training   runtime
	0.11s	 = Validation runtime
Fitting model: NeuralNetFastAI_BAG_L1 ... Training model for up to 7.73s of the 21.37s of remaining time.
	Fitting 8 child models (S1F1 - S1F8) | Fitting with ParallelLocalFoldFittingStrategy (8 workers, per: cpus=1, gpus=0, memory=0.00%)
	0.8701	 = Validation score   (roc_auc)
	4.7s	 = Training   runtime
	0.11s	 = Validation runtime
Fitting model: XGBoost_BAG_L1 ... Training model for up to 0.16s of the 13.79s of remaining time.
	Fitting 8 child models (S1F1 - S1F8) | Fitting with ParallelLocalFoldFittingStrategy (8 workers, per: cpus=1, gpus=0, memory=0.04%)
	0.8496	 = Validation score   (roc_auc)
	1.2s	 = Training   runtime
	0.06s	 = Validation runtime
Fitting model: WeightedEnsemble_L2 ... Training model for up to 40.91s of the 8.76s of remaining time.
	Ensemble Weights: {'ExtraTreesGini_BAG_L1': 0.333, 'XGBoost_BAG_L1': 0.25, 'LightGBMXT_BAG_L1': 0.167, 'CatBoost_BAG_L1': 0.167, 'NeuralNetFastAI_BAG_L1': 0.083}
	0.9042	 = Validation score   (roc_auc)
	0.05s	 = Training   runtime
	0.0s	 = Validation runtime
Fitting 108 L2 models, fit_strategy="sequential" ...
Fitting model: LightGBMXT_BAG_L2 ... Training model for up to 8.70s of the 8.67s of remaining time.
	Fitting 8 child models (S1F1 - S1F8) | Fitting with ParallelLocalFoldFittingStrategy (8 workers, per: cpus=1, gpus=0, memory=0.03%)
	0.8884	 = Validation score   (roc_auc)
	0.79s	 = Training   runtime
	0.04s	 = Validation runtime
Fitting model: LightGBM_BAG_L2 ... Training model for up to 4.88s of the 4.85s of remaining time.
	Fitting 8 child models (S1F1 - S1F8) | Fitting with ParallelLocalFoldFittingStrategy (8 workers, per: cpus=1, gpus=0, memory=0.03%)
	0.8697	 = Validation score   (roc_auc)
	0.84s	 = Training   runtime
	0.04s	 = Validation runtime
Fitting model: RandomForestGini_BAG_L2 ... Training model for up to 0.73s of the 0.70s of remaining time.
/home/ci/opt/venv/lib/python3.11/site-packages/sklearn/base.py:474: FutureWarning: `BaseEstimator._validate_data` is deprecated in 1.6 and will be removed in 1.7. Use `sklearn.utils.validation.validate_data` instead. This function becomes public and is part of the scikit-learn developer API.
  warnings.warn(
	0.8711	 = Validation score   (roc_auc)
	0.79s	 = Training   runtime
	0.11s	 = Validation runtime
Fitting model: WeightedEnsemble_L3 ... Training model for up to 40.91s of the -0.37s of remaining time.
	Ensemble Weights: {'ExtraTreesGini_BAG_L1': 0.333, 'XGBoost_BAG_L1': 0.25, 'LightGBMXT_BAG_L1': 0.167, 'CatBoost_BAG_L1': 0.167, 'NeuralNetFastAI_BAG_L1': 0.083}
	0.9042	 = Validation score   (roc_auc)
	0.05s	 = Training   runtime
	0.0s	 = Validation runtime
AutoGluon training complete, total runtime = 41.45s ... Best model: WeightedEnsemble_L2 | Estimated inference throughput: 241.0 rows/s (63 batch size)
TabularPredictor saved. To load, use: predictor = TabularPredictor.load("/home/ci/autogluon/docs/tutorials/tabular/AutogluonModels/ag-20250508_205807")
predictor.leaderboard(test_data)
模型 score_test score_val 评估指标 pred_time_test pred_time_val 拟合时间 pred_time_test_marginal pred_time_val_marginal fit_time_marginal 堆叠层级 可推理 拟合顺序
0 CatBoost_BAG_L1 0.902618 0.890228 roc_auc 0.050304 0.039344 5.824297 0.050304 0.039344 5.824297 1 True 7
1 LightGBMXT_BAG_L1 0.900085 0.891223 roc_auc 0.246651 0.038467 0.746474 0.246651 0.038467 0.746474 1 True 3
2 LightGBMXT_BAG_L2 0.899803 0.888361 roc_auc 1.956953 0.521406 14.376791 0.108029 0.040050 0.789424 2 True 13
3 WeightedEnsemble_L3 0.899461 0.904231 roc_auc 1.710372 0.360134 13.092940 0.002940 0.000477 0.046427 3 True 16
4 WeightedEnsemble_L2 0.899461 0.904231 roc_auc 1.710430 0.360164 13.093320 0.002998 0.000507 0.046807 2 True 12
5 RandomForestGini_BAG_L2 0.893800 0.871081 roc_auc 1.968632 0.595503 14.374731 0.119709 0.114148 0.787364 2 True 15
6 LightGBM_BAG_L2 0.891542 0.869691 roc_auc 1.933111 0.524429 14.428029 0.084188 0.043073 0.840662 2 True 14
7 LightGBM_BAG_L1 0.889478 0.879878 roc_auc 0.125095 0.039144 0.971785 0.125095 0.039144 0.971785 1 True 4
8 RandomForestEntr_BAG_L1 0.886981 0.889863 roc_auc 0.109420 0.108268 0.537700 0.109420 0.108268 0.537700 1 True 6
9 RandomForestGini_BAG_L1 0.885163 0.887874 roc_auc 0.119115 0.111590 0.959964 0.119115 0.111590 0.959964 1 True 5
10 NeuralNetFastAI_BAG_L1 0.883602 0.870056 roc_auc 1.033716 0.108463 4.701377 1.033716 0.108463 4.701377 1 True 10
11 ExtraTreesEntr_BAG_L1 0.880342 0.890401 roc_auc 0.108395 0.113053 0.521556 0.108395 0.113053 0.521556 1 True 9
12 ExtraTreesGini_BAG_L1 0.879143 0.895789 roc_auc 0.100756 0.112490 0.571164 0.100756 0.112490 0.571164 1 True 8
13 XGBoost_BAG_L1 0.863921 0.849609 roc_auc 0.276004 0.060892 1.203202 0.276004 0.060892 1.203202 1 True 11
14 KNeighborsDist_BAG_L1 0.525998 0.536956 roc_auc 0.032071 0.013430 0.003154 0.032071 0.013430 0.003154 1 True 2
15 KNeighborsUnif_BAG_L1 0.514970 0.519604 roc_auc 0.026831 0.015071 0.003830 0.026831 0.015071 0.003830 1 True 1

此命令实现了以下最大化准确率的策略

  • 指定参数 presets='best_quality',这允许 AutoGluon 基于堆叠/装袋(stacking/bagging)自动构建强大的模型集成,如果在有足够的训练时间的情况下,会显著提高预测结果。 presets 的默认值为 'medium_quality',它产生的模型准确率较低,但有助于更快速地进行原型开发。通过 presets,您可以灵活地优先考虑预测准确率或训练/推理速度。例如,如果您不太关心预测性能,而希望快速部署一个基本模型,可以考虑使用: presets=['good_quality', 'optimize_for_deployment']

  • 如果您知道将在您的应用中用于评估预测的指标,请向 TabularPredictor() 提供参数 eval_metric。您可能使用的一些其他非默认指标包括:'f1'(用于二元分类)、'roc_auc'(用于二元分类)、'log_loss'(用于分类)、'mean_absolute_error'(用于回归)、'median_absolute_error'(用于回归)。您还可以定义自己的自定义指标函数。更多信息请参阅向 AutoGluon 添加自定义指标

  • 将所有数据包含在 train_data 中,并且不要提供 tuning_data(AutoGluon 将更智能地分割数据以满足其需求)。

  • 不要指定 hyperparameter_tune_kwargs 参数(与直觉相反,超参数调整并不是利用有限训练时间预算的最佳方式,因为模型集成通常更优)。我们建议您仅在目标是部署单个模型而非集成模型时使用 hyperparameter_tune_kwargs

  • 不要指定 hyperparameters 参数(允许 AutoGluon 自适应地选择要使用的模型/超参数)。

  • time_limit 设置为您愿意等待的最长时间(以秒为单位)。AutoGluon 的预测性能会随着 fit() 运行时间的增加而提高。

回归(预测表格中的数值列):

为了演示 fit() 也能自动处理回归任务,我们现在尝试根据其他特征预测同一表格中的数值变量 age

age_column = 'age'
train_data[age_column].head()
6118     51
23204    58
29590    40
18116    37
33964    62
Name: age, dtype: int64

我们再次调用 fit(),这次设置了时间限制(以秒为单位),并演示了一种在测试数据(包含标签)上评估结果模型的简写方法

predictor_age = TabularPredictor(label=age_column, path="agModels-predictAge").fit(train_data, time_limit=60)
隐藏代码单元输出
Verbosity: 2 (Standard Logging)
=================== System Info ===================
AutoGluon Version:  1.3.1b20250508
Python Version:     3.11.9
Operating System:   Linux
Platform Machine:   x86_64
Platform Version:   #1 SMP Wed Mar 12 14:53:59 UTC 2025
CPU Count:          8
Memory Avail:       27.67 GB / 30.95 GB (89.4%)
Disk Space Avail:   211.77 GB / 255.99 GB (82.7%)
===================================================
No presets specified! To achieve strong results with AutoGluon, it is recommended to use the available presets. Defaulting to `'medium'`...
	Recommended Presets (For more details refer to https://autogluon.cn/stable/tutorials/tabular/tabular-essentials.html#presets):
	presets='experimental' : New in v1.2: Pre-trained foundation model + parallel fits. The absolute best accuracy without consideration for inference speed. Does not support GPU.
	presets='best'         : Maximize accuracy. Recommended for most users. Use in competitions and benchmarks.
	presets='high'         : Strong accuracy with fast inference speed.
	presets='good'         : Good accuracy with very fast inference speed.
	presets='medium'       : Fast training time, ideal for initial prototyping.
Beginning AutoGluon training ... Time limit = 60s
AutoGluon will save models to "/home/ci/autogluon/docs/tutorials/tabular/agModels-predictAge"
Train Data Rows:    500
Train Data Columns: 14
Label Column:       age
AutoGluon infers your prediction problem is: 'regression' (because dtype of label-column == int and many unique label-values observed).
	Label info (max, min, mean, stddev): (85, 17, 39.652, 13.52393)
	If 'regression' is not the correct problem_type, please manually specify the problem_type parameter during Predictor init (You may specify problem_type as one of: ['binary', 'multiclass', 'regression', 'quantile'])
Problem Type:       regression
Preprocessing data ...
Using Feature Generators to preprocess the data ...
Fitting AutoMLPipelineFeatureGenerator...
	Available Memory:                    28330.66 MB
	Train Data (Original)  Memory Usage: 0.31 MB (0.0% of available memory)
	Inferring data type of each feature based on column values. Set feature_metadata_in to manually specify special dtypes of the features.
	Stage 1 Generators:
		Fitting AsTypeFeatureGenerator...
			Note: Converting 2 features to boolean dtype as they only contain 2 unique values.
	Stage 2 Generators:
		Fitting FillNaFeatureGenerator...
	Stage 3 Generators:
		Fitting IdentityFeatureGenerator...
		Fitting CategoryFeatureGenerator...
			Fitting CategoryMemoryMinimizeFeatureGenerator...
	Stage 4 Generators:
		Fitting DropUniqueFeatureGenerator...
	Stage 5 Generators:
		Fitting DropDuplicatesFeatureGenerator...
	Types of features in original data (raw dtype, special dtypes):
		('int', [])    : 5 | ['fnlwgt', 'education-num', 'capital-gain', 'capital-loss', 'hours-per-week']
		('object', []) : 9 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]
	Types of features in processed data (raw dtype, special dtypes):
		('category', [])  : 7 | ['workclass', 'education', 'marital-status', 'occupation', 'relationship', ...]
		('int', [])       : 5 | ['fnlwgt', 'education-num', 'capital-gain', 'capital-loss', 'hours-per-week']
		('int', ['bool']) : 2 | ['sex', 'class']
	0.1s = Fit runtime
	14 features in original data used to generate 14 features in processed data.
	Train Data (Processed) Memory Usage: 0.03 MB (0.0% of available memory)
Data preprocessing and feature engineering runtime = 0.08s ...
AutoGluon will gauge predictive performance using evaluation metric: 'root_mean_squared_error'
	This metric's sign has been flipped to adhere to being higher_is_better. The metric score can be multiplied by -1 to get the metric value.
	To change this, specify the eval_metric parameter of Predictor()
Automatically generating train/validation split with holdout_frac=0.2, Train Rows: 400, Val Rows: 100
User-specified model hyperparameters to be fit:
{
	'NN_TORCH': [{}],
	'GBM': [{'extra_trees': True, 'ag_args': {'name_suffix': 'XT'}}, {}, {'learning_rate': 0.03, 'num_leaves': 128, 'feature_fraction': 0.9, 'min_data_in_leaf': 3, 'ag_args': {'name_suffix': 'Large', 'priority': 0, 'hyperparameter_tune_kwargs': None}}],
	'CAT': [{}],
	'XGB': [{}],
	'FASTAI': [{}],
	'RF': [{'criterion': 'gini', 'ag_args': {'name_suffix': 'Gini', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'entropy', 'ag_args': {'name_suffix': 'Entr', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'squared_error', 'ag_args': {'name_suffix': 'MSE', 'problem_types': ['regression', 'quantile']}}],
	'XT': [{'criterion': 'gini', 'ag_args': {'name_suffix': 'Gini', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'entropy', 'ag_args': {'name_suffix': 'Entr', 'problem_types': ['binary', 'multiclass']}}, {'criterion': 'squared_error', 'ag_args': {'name_suffix': 'MSE', 'problem_types': ['regression', 'quantile']}}],
	'KNN': [{'weights': 'uniform', 'ag_args': {'name_suffix': 'Unif'}}, {'weights': 'distance', 'ag_args': {'name_suffix': 'Dist'}}],
}
Fitting 11 L1 models, fit_strategy="sequential" ...
Fitting model: KNeighborsUnif ... Training model for up to 59.92s of the 59.92s of remaining time.
	-15.6869	 = Validation score   (-root_mean_squared_error)
	0.0s	 = Training   runtime
	0.01s	 = Validation runtime
Fitting model: KNeighborsDist ... Training model for up to 59.90s of the 59.90s of remaining time.
	-15.1801	 = Validation score   (-root_mean_squared_error)
	0.0s	 = Training   runtime
	0.01s	 = Validation runtime
Fitting model: LightGBMXT ... Training model for up to 59.88s of the 59.88s of remaining time.
	-11.7092	 = Validation score   (-root_mean_squared_error)
	0.33s	 = Training   runtime
	0.0s	 = Validation runtime
Fitting model: LightGBM ... Training model for up to 59.54s of the 59.54s of remaining time.
	-11.9295	 = Validation score   (-root_mean_squared_error)
	0.29s	 = Training   runtime
	0.0s	 = Validation runtime
Fitting model: RandomForestMSE ... Training model for up to 59.24s of the 59.24s of remaining time.
	-11.6624	 = Validation score   (-root_mean_squared_error)
	0.45s	 = Training   runtime
	0.06s	 = Validation runtime
Fitting model: CatBoost ... Training model for up to 58.71s of the 58.71s of remaining time.
	-11.7993	 = Validation score   (-root_mean_squared_error)
	0.63s	 = Training   runtime
	0.0s	 = Validation runtime
Fitting model: ExtraTreesMSE ... Training model for up to 58.07s of the 58.07s of remaining time.
	-11.3627	 = Validation score   (-root_mean_squared_error)
	0.41s	 = Training   runtime
	0.06s	 = Validation runtime
Fitting model: NeuralNetFastAI ... Training model for up to 57.58s of the 57.58s of remaining time.
	-11.9445	 = Validation score   (-root_mean_squared_error)
	0.56s	 = Training   runtime
	0.01s	 = Validation runtime
Fitting model: XGBoost ... Training model for up to 57.00s of the 57.00s of remaining time.
	-11.5274	 = Validation score   (-root_mean_squared_error)
	0.24s	 = Training   runtime
	0.01s	 = Validation runtime
Fitting model: NeuralNetTorch ... Training model for up to 56.75s of the 56.75s of remaining time.
	-12.0028	 = Validation score   (-root_mean_squared_error)
	2.4s	 = Training   runtime
	0.01s	 = Validation runtime
Fitting model: LightGBMLarge ... Training model for up to 54.34s of the 54.33s of remaining time.
	-12.6926	 = Validation score   (-root_mean_squared_error)
	0.55s	 = Training   runtime
	0.0s	 = Validation runtime
Fitting model: WeightedEnsemble_L2 ... Training model for up to 59.92s of the 53.77s of remaining time.
	Ensemble Weights: {'ExtraTreesMSE': 0.48, 'XGBoost': 0.2, 'NeuralNetFastAI': 0.16, 'NeuralNetTorch': 0.16}
	-11.1703	 = Validation score   (-root_mean_squared_error)
	0.01s	 = Training   runtime
	0.0s	 = Validation runtime
AutoGluon training complete, total runtime = 6.26s ... Best model: WeightedEnsemble_L2 | Estimated inference throughput: 1209.6 rows/s (100 batch size)
TabularPredictor saved. To load, use: predictor = TabularPredictor.load("/home/ci/autogluon/docs/tutorials/tabular/agModels-predictAge")
predictor_age.evaluate(test_data)
{'root_mean_squared_error': np.float64(-10.473113418101228),
 'mean_squared_error': -109.68611145019531,
 'mean_absolute_error': -8.2290620803833,
 'r2': 0.4137064218521118,
 'pearsonr': 0.6458247054882367,
 'median_absolute_error': np.float64(-6.8800048828125)}

请注意,我们无需告诉 AutoGluon 这是一个回归问题,它会根据数据自动推断并报告适当的性能评估指标(默认为 RMSE)。要指定除默认值以外的特定评估指标,请设置 TabularPredictor()eval_metric 参数,AutoGluon 将根据您的指标定制模型进行优化(例如 eval_metric = 'mean_absolute_error')。对于值越高越差的评估指标(如 RMSE),AutoGluon 在训练期间会将其符号翻转并打印为负值(因为它内部假定值越高越好)。您甚至可以按照自定义指标教程指定自定义指标。

我们可以调用 leaderboard 查看每个模型的性能

predictor_age.leaderboard(test_data)
模型 score_test score_val 评估指标 pred_time_test pred_time_val 拟合时间 pred_time_test_marginal pred_time_val_marginal fit_time_marginal 堆叠层级 可推理 拟合顺序
0 WeightedEnsemble_L2 -10.473113 -11.170347 root_mean_squared_error 0.355627 0.082671 3.622110 0.002383 0.000302 0.010410 2 True 12
1 ExtraTreesMSE -10.655482 -11.362738 root_mean_squared_error 0.124923 0.057184 0.412735 0.124923 0.057184 0.412735 1 True 7
2 RandomForestMSE -10.746175 -11.662354 root_mean_squared_error 0.140525 0.059639 0.454365 0.140525 0.059639 0.454365 1 True 5
3 CatBoost -10.780312 -11.799279 root_mean_squared_error 0.010753 0.004292 0.633243 0.010753 0.004292 0.633243 1 True 6
4 LightGBMXT -10.837373 -11.709228 root_mean_squared_error 0.059728 0.003600 0.330500 0.059728 0.003600 0.330500 1 True 3
5 XGBoost -10.903558 -11.527441 root_mean_squared_error 0.045800 0.005569 0.240767 0.045800 0.005569 0.240767 1 True 9
6 LightGBM -10.972156 -11.929546 root_mean_squared_error 0.019616 0.002632 0.289428 0.019616 0.002632 0.289428 1 True 4
7 NeuralNetTorch -11.193955 -12.002765 root_mean_squared_error 0.046560 0.010272 2.398271 0.046560 0.010272 2.398271 1 True 10
8 NeuralNetFastAI -11.343937 -11.944539 root_mean_squared_error 0.135961 0.009345 0.559929 0.135961 0.009345 0.559929 1 True 8
9 LightGBMLarge -11.832441 -12.692643 root_mean_squared_error 0.033194 0.003251 0.549246 0.033194 0.003251 0.549246 1 True 11
10 KNeighborsUnif -14.902058 -15.686937 root_mean_squared_error 0.030776 0.013438 0.003672 0.030776 0.013438 0.003672 1 True 1
11 KNeighborsDist -15.771259 -15.180149 root_mean_squared_error 0.028360 0.013096 0.003516 0.028360 0.013096 0.003516 1 True 2

数据格式: AutoGluon 目前可以处理已加载到 Python 中作为 pandas DataFrame 的数据表,或存储在 CSV 格式Parquet 格式文件中的数据表。如果您的数据位于多个表中,您需要先将它们合并为一个表格,其中每行对应一个统计独立的观测值(数据点),每列对应不同的特征(也称为变量/协变量)。

请参阅TabularPredictor 文档以查看所有可用方法/选项。

高级用法

有关 AutoGluon 的更高级用法示例,请参阅深入教程

如果您对部署优化感兴趣,请参阅部署优化教程

要向 AutoGluon 添加自定义模型,请参阅自定义模型自定义模型(高级)教程。