
- CatBoost 教程
- CatBoost - 首頁
- CatBoost - 概述
- CatBoost - 架構
- CatBoost - 安裝
- CatBoost - 特性
- CatBoost - 決策樹
- CatBoost - 提升過程
- CatBoost - 核心引數
- CatBoost - 資料預處理
- CatBoost - 處理類別特徵
- CatBoost - 處理缺失值
- CatBoost 分類器
- CatBoost - 迴歸器
- CatBoost - 排序器
- CatBoost - 模型訓練
- CatBoost - 模型評估指標
- CatBoost - 分類指標
- CatBoost - 過擬合檢測
- CatBoost 與其他提升演算法的比較
- CatBoost 有用資源
- CatBoost - 有用資源
- CatBoost - 討論
CatBoost 分類器
CatBoost 分類器是一個處理分類問題的有用工具,尤其是在處理包含類別變數的資料時。梯度提升是其核心方法,它將多個弱模型組合成一個強大的模型。CatBoost 的主要特性之一是它能夠處理類別資料,而無需將其轉換為數值。
現在我們將學習如何使用 CatBoost 分類器。我們將以一個例子為例,在這個例子中,我們想要使用某些資料來預測某些東西。
使用 CatBoostClassifier 的步驟
現在讓我們一起學習使用 CatBoost 分類器的步驟:
1. 準備資料
資料:為了訓練模型,必須獲得一系列資料。假設您有一份人員名單,以及他們的年齡、性別和薪水。您想預測每個人是否會購買產品。這裡:
特徵 (X):具體資訊,例如年齡、性別和價格。
標籤 (y):結果,包括他們是否購買了該商品。
訓練資料:模型將從中學習的資料。
測試資料:用於檢查模型是否有效的資料。
示例
以下是如何準備資料的示例:
X_train, X_test, y_train, y_test = train_test_split (data, labels, test_size=0.2)
2. 構建 CatBoost 分類器
接下來,生成 CatBoost 模型。您可以更改許多引數,例如:
迭代次數:它將生成的子模型或樹的數量。每棵樹都從前一棵樹的錯誤中學習。
學習率:模型的學習率。如果太快,模型可能會錯過重要的細節。如果太慢,則學習時間會很長。
深度:樹的整體深度。透過增加深度,模型可以捕獲更復雜的特徵,但是過擬合的可能性也會增加。
示例
以下是如何構建 CatBoost 分類器的示例:
model = CatBoostClassifier (iterations=1000, learning_rate=0.1, depth=6)
3. 訓練模型
一旦資料和模型可用,就需要訓練模型。這意味著模型將瀏覽訓練資料並嘗試查詢任何模式。這裡的 fit 命令告訴模型從訓練資料 (X_train 和 y_train) 中學習。而 verbose=100 表示模型將在每 100 次迭代後顯示其進度,以便您可以檢視其學習情況。
model.fit (X_train, y_train, verbose=100)
4. 進行預測
模型訓練完成後,您可以使用它進行預測。例如,當您提供新資料 (X_test) 時,它將預測結果 (y_test)。
preds = model.predict(X_test)
5. 評估模型
進行預測後,您必須評估模型的效能。一種方法是檢查模型預測的準確性。
accuracy = accuracy_score(y_test, preds) print(f"Accuracy is: {accuracy}")
使用 CatBoostClassifier 的示例
我們將使用 Housing.csv 資料集來演示如何建立 CatBoost 分類器模型。為了建立模型,我們將遵循上述步驟:
import pandas as pd from catboost import CatBoostClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, classification_report # 1. Load the dataset data = pd.read_csv('/Python/Housing.csv') # Step 3: Preprocess the Data # Convert categorical variables to numeric categorical_features = ['mainroad', 'guestroom', 'basement', 'hotwaterheating', 'airconditioning', 'prefarea', 'furnishingstatus'] for col in categorical_features: data[col] = data[col].map({'yes': 1, 'no': 0}) # Target variable X = data.drop('price', axis=1) y = data['price'] # Step 4: Split the Data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Step 5: Train the Model model = CatBoostClassifier(iterations=100, depth=6, learning_rate=0.1, random_seed=42, verbose=0) model.fit(X_train, y_train) # Step 6: Evaluate the Model y_pred = model.predict(X_test) accuracy = accuracy_score(y_test, y_pred) report = classification_report(y_test, y_pred) print(f"Accuracy: {accuracy:.2f}") print("Classification Report:") print(report)
輸出
以下是結果:
Accuracy: 0.00 Classification Report: precision recall f1-score support 1750000 0.00 0.00 0.00 1.0 1820000 0.00 0.00 0.00 1.0 1890000 0.00 0.00 0.00 2.0 2100000 0.00 0.00 0.00 1.0 2233000 0.00 0.00 0.00 1.0 . . . 9800000 0.00 0.00 0.00 2.0 10150000 0.00 0.00 0.00 1.0 12250000 0.00 0.00 0.00 1.0 13300000 0.00 0.00 0.00 1.0 accuracy 0.00 109.0 macro avg 0.00 0.00 0.00 109.0 weighted avg 0.00 0.00 0.00 109.0