> ## Documentation Index
> Fetch the complete documentation index at: https://docs.monostate.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 表格数据

> 在结构化表格数据上训练模型

# 表格数据

在 CSV 和数据库等结构化数据上训练模型。

## 快速开始

```bash theme={null}
aitraining tabular --train \
  --model xgboost \
  --data-path ./data.csv \
  --target-columns target \
  --valid-split validation \
  --project-name tabular-model
```

<Warning>
  表格训练需要 `--valid-split` 来指定数据中的验证集。
</Warning>

## 任务类型

### 分类

预测分类标签：

```bash theme={null}
aitraining tabular --train \
  --model xgboost \
  --data-path ./customers.csv \
  --target-columns churn \
  --valid-split validation \
  --task classification \
  --project-name churn-predictor
```

### 回归

预测连续值：

```bash theme={null}
aitraining tabular --train \
  --model xgboost \
  --data-path ./houses.csv \
  --target-columns price \
  --valid-split validation \
  --task regression \
  --project-name price-predictor
```

## Parameters

| Parameter               | Description                     | Default          |
| ----------------------- | ------------------------------- | ---------------- |
| `--model`               | Model type                      | `xgboost`        |
| `--data-path`           | Path to CSV/data                | None (required)  |
| `--project-name`        | Output directory                | `project-name`   |
| `--target-columns`      | Target variable(s)              | `["target"]`     |
| `--task`                | classification/regression       | `classification` |
| `--train-split`         | Training data split             | `train`          |
| `--valid-split`         | Validation data split           | None (required)  |
| `--id-column`           | ID column to exclude            | `id`             |
| `--categorical-columns` | Categorical features            | `None`           |
| `--numerical-columns`   | Numerical features              | `None`           |
| `--num-trials`          | Number of hyperparameter trials | `10`             |
| `--time-limit`          | Time limit in seconds           | `600`            |
| `--seed`                | Random seed                     | `42`             |

## Available Models

| Model                 | Classification | Regression |
| --------------------- | -------------- | ---------- |
| `xgboost`             | Yes            | Yes        |
| `random_forest`       | Yes            | Yes        |
| `extra_trees`         | Yes            | Yes        |
| `gradient_boosting`   | Yes            | Yes        |
| `adaboost`            | Yes            | Yes        |
| `decision_tree`       | Yes            | Yes        |
| `logistic_regression` | Yes            | Yes        |
| `ridge`               | Yes            | Yes        |
| `svm`                 | Yes            | Yes        |
| `knn`                 | Yes            | Yes        |
| `naive_bayes`         | Yes            | No         |
| `lasso`               | No             | Yes        |
| `linear_regression`   | No             | Yes        |

## 数据格式

### CSV 格式

```csv theme={null}
feature1,feature2,feature3,target
1.5,category_a,100,1
2.3,category_b,200,0
```

### 处理特征

指定分类列：

```bash theme={null}
aitraining tabular --train \
  --model gradient_boosting \
  --data-path ./data.csv \
  --target-columns target \
  --valid-split validation \
  --categorical-columns "color,size,region" \
  --project-name model
```

排除 ID 列：

```bash theme={null}
aitraining tabular --train \
  --model xgboost \
  --data-path ./data.csv \
  --target-columns target \
  --valid-split validation \
  --id-column customer_id \
  --project-name model
```

## Examples

### 客户流失

```bash theme={null}
aitraining tabular --train \
  --model xgboost \
  --data-path ./customers.csv \
  --target-columns churned \
  --valid-split validation \
  --id-column customer_id \
  --task classification \
  --project-name churn-model
```

### 房价预测

```bash theme={null}
aitraining tabular --train \
  --model gradient_boosting \
  --data-path ./houses.csv \
  --target-columns sale_price \
  --valid-split validation \
  --id-column house_id \
  --task regression \
  --project-name house-prices
```

### 多类分类

```bash theme={null}
aitraining tabular --train \
  --model extra_trees \
  --data-path ./products.csv \
  --target-columns category \
  --valid-split validation \
  --categorical-columns "brand,color,material" \
  --task classification \
  --project-name product-classifier
```

## 模型比较

在您的数据上比较不同模型：

```bash theme={null}
# Train XGBoost
aitraining tabular --train \
  --model xgboost \
  --data-path ./data.csv \
  --target-columns target \
  --valid-split validation \
  --project-name model-xgb

# Train Random Forest
aitraining tabular --train \
  --model random_forest \
  --data-path ./data.csv \
  --target-columns target \
  --valid-split validation \
  --project-name model-rf

# Train Gradient Boosting
aitraining tabular --train \
  --model gradient_boosting \
  --data-path ./data.csv \
  --target-columns target \
  --valid-split validation \
  --project-name model-gb
```

## Output

After training, you'll find:

```
project-name/
├── model.joblib        # Trained model
├── metrics.json        # Evaluation metrics
├── feature_importance.json
└── config.yaml         # Training config
```

## 加载模型

```python theme={null}
import joblib

model = joblib.load("project-name/model.joblib")
predictions = model.predict(new_data)
```

## 下一步

<CardGroup cols={2}>
  <Card title="文本任务" href="/cli/text-tasks">
    文本分类
  </Card>

  <Card title="视觉任务" href="/cli/vision-tasks">
    图像分类
  </Card>
</CardGroup>
