{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Demo for using xgboost with sklearn\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import multiprocessing\nfrom urllib.error import HTTPError\n\nfrom sklearn.datasets import fetch_california_housing, make_regression\nfrom sklearn.model_selection import GridSearchCV\n\nimport xgboost as xgb\n\nif __name__ == \"__main__\":\n    print(\"Parallel Parameter optimization\")\n    try:\n        X, y = fetch_california_housing(return_X_y=True)\n    except HTTPError:\n        # Use a synthetic dataset instead if we couldn't\n        X, y = make_regression(n_samples=20640, n_features=8, random_state=1234)\n    # Make sure the number of threads is balanced.\n    xgb_model = xgb.XGBRegressor(\n        n_jobs=multiprocessing.cpu_count() // 2, tree_method=\"hist\"\n    )\n    clf = GridSearchCV(\n        xgb_model,\n        {\"max_depth\": [2, 4, 6], \"n_estimators\": [50, 100, 200]},\n        verbose=1,\n        n_jobs=2,\n    )\n    clf.fit(X, y)\n    print(clf.best_score_)\n    print(clf.best_params_)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.17"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}