首頁 > web前端 > js教程 > 在 Node.js 中使用 XGBoost 預測房價

在 Node.js 中使用 XGBoost 預測房價

Patricia Arquette
發布: 2024-11-15 14:51:03
原創
1095 人瀏覽過

Predicting House Prices with XGBoost in Node.js

什麼是 XGBoost?

XGBoost 是一種流行的機器學習演算法,經常在 Kaggle 和其他資料科學競賽中名列前茅。 XGBoost 的與眾不同之處在於它能夠將多個弱模型(在本例中為決策樹)組合成一個強模型。這是透過一種稱為梯度增強的技術來完成的,該技術有助於使演算法穩健且對於各種預測任務都非常有效。

XGBoost 如何運作?

XGBoost 使用梯度提升,這意味著它按順序建立樹,其中每棵樹都嘗試糾正先前樹的錯誤。這是該過程的簡化視圖:

  1. 進行初步預測(可以是所有目標值的平均值)
  2. 計算這個預測有多錯誤(錯誤)
  3. 建立決策樹來預測此錯誤
  4. 將此樹的預測添加到我們的運行預測總數中(但按比例縮小以防止過度自信)
  5. 重複步驟2-4多次

例如,如果我們預測房價:

  • 第一棵樹可能預測 200,000 美元
  • 如果實際價格為 $250,000,則錯誤為 $50,000
  • 下一棵樹專注於預測這個 50,000 美元的錯誤
  • 最終預測結合了所有樹的預測

這個過程與一些巧妙的數學和優化相結合,使得 XGBoost 既準確又快速。

為什麼在 Node.js 中使用 XGBoost?

雖然 XGBoost 最初是作為 C 庫實現的,但有適用於 Python 和 R 等語言的綁定,使得通常專門從事資料和機器學習的廣泛開發人員可以使用它。

我最近有一個專案對 Node.js 有嚴格的要求,所以我看到了一個透過為 Node.js 編寫綁定來彌補差距的機會。我希望這有助於為 JavaScript 開發人員打開更多 ML 的大門。

在本文中,我們將仔細研究如何在 Node.js 應用程式中使用 XGBoost。

先決條件

開始之前,請確保您已經:

  • Linux 作業系統(xgboost_node 的目前要求)
  • Node.js 版本 18.0.0 或更高版本
  • 對機器學習概念的基本了解

安裝

使用 npm 安裝 XGBoost Node.js 綁定:

npm install xgboost_node
登入後複製

了解數據

在進入程式碼之前,讓我們先了解我們的特徵在房價預測範例中代表什麼:

// Each feature array represents:
[square_feet, property_age, total_rooms, has_parking, neighborhood_type, is_furnished]

// Example:
[1200,       8,            10,           0,           1,                1        ]
登入後複製

以下是每個功能的意思:

  • square_feet: The size of the property (e.g., 1200 sq ft)
  • property_age: Age of the property in years (e.g., 8 years)
  • total_rooms: Total number of rooms (e.g., 10 rooms)
  • has_parking: Binary (0 = no parking, 1 = has parking)
  • neighborhood_type: Category (1 = residential, 2 = commercial area)
  • is_furnished: Binary (0 = unfurnished, 1 = furnished)

And the corresponding labels array contains house prices in thousands (e.g., 250 means $250,000).

Transforming Your Data

If you have raw data in a different format, here's how to transform it for XGBoost:

// Let's say you have data in this format:
const rawHouses = [
    {
        address: "123 Main St",
        sqft: 1200,
        yearBuilt: 2015,
        rooms: 10,
        parking: "Yes",
        neighborhood: "Residential",
        furnished: true,
        price: 250000
    },
    // ... more houses
];

// Transform it to XGBoost format:
const features = rawHouses.map(house => [
    house.sqft,
    new Date().getFullYear() - house.yearBuilt,  // Convert year built to age
    house.rooms,
    house.parking === "Yes" ? 1 : 0,             // Convert Yes/No to 1/0
    house.neighborhood === "Residential" ? 1 : 2, // Convert category to number
    house.furnished ? 1 : 0                       // Convert boolean to 1/0
]);

const labels = rawHouses.map(house => house.price / 1000); // Convert price to thousands
登入後複製

Training Your First Model

Here's a complete example that shows how to train a model and make predictions:

import xgboost from 'xgboost_node';

async function test() {
    const features = [
        [1200, 8, 10, 0, 1, 1],
        [800, 14, 15, 1, 2, 0],
        [1200, 8, 10, 0, 1, 1],
        [1200, 8, 10, 0, 1, 1],
        [1200, 8, 10, 0, 1, 1],
        [800, 14, 15, 1, 2, 0],
        [1200, 8, 10, 0, 1, 1],
        [1200, 8, 10, 0, 1, 1],
    ];
    const labels = [250, 180, 250, 180, 250, 180, 250, 180];

    const params = {
        max_depth: 3,
        eta: 0.3,
        objective: 'reg:squarederror',
        eval_metric: 'rmse',
        nthread: 4,
        num_round: 100,
        min_child_weight: 1,
        subsample: 0.8,
        colsample_bytree: 0.8,
    };

    try {
        await xgboost.train(features, labels, params);
        const predictions = await xgboost.predict([[1000, 0, 1, 0, 1, 1], [800, 0, 1, 0, 1, 1]]);
        console.log('Predicted value:', predictions[0]);
    } catch (error) {
        console.error('Error:', error);
    }
}

test();
登入後複製

The example above shows how to:

  1. Set up training data with features and labels
  2. Configure XGBoost parameters for training
  3. Train the model
  4. Make predictions on new data

Model Management

XGBoost provides straightforward methods for saving and loading models:

// Save model after training
await xgboost.saveModel('model.xgb');

// Load model for predictions
await xgboost.loadModel('model.xgb');
登入後複製

Further Considerations

You may have noticed there are parameters for this model. I would advise looking into XGBoost documentation to understand how to tune and choose your parameters. Here's what some of these parameters are trying to achieve:

const params = {
    max_depth: 3,              // Controls how deep each tree can grow
    eta: 0.3,                 // Learning rate - how much we adjust for each tree
    objective: 'reg:squarederror',  // For regression problems
    eval_metric: 'rmse',      // How we measure prediction errors
    nthread: 4,               // Number of parallel processing threads
    num_round: 100,           // Number of trees to build
    min_child_weight: 1,      // Minimum amount of data in a leaf
    subsample: 0.8,           // Fraction of data to use in each tree
    colsample_bytree: 0.8,    // Fraction of features to consider for each tree
};
登入後複製

These parameters significantly impact your model's performance and behavior. For example:

  • Lower max_depth helps prevent overfitting but might underfit if too low
  • Lower eta means slower learning but can lead to better generalization
  • Higher num_round means more trees, which can improve accuracy but increases training time

Conclusion

This guide provides a starting point for using XGBoost in Node.js. For production use, I recommend:

  1. Understanding and tuning the XGBoost parameters for your specific use case
  2. Implementing proper cross-validation to evaluate your model
  3. Testing with different data scenarios to ensure robustness
  4. Monitoring model performance in production

Jonathan Farrow

@farrow_jonny

以上是在 Node.js 中使用 XGBoost 預測房價的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板