{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5ac7ee77491dddc3",
   "metadata": {},
   "source": [
    "# 🙋🏻 模型保存&加载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-09-01T08:33:05.975505Z"
    },
    "jupyter": {
     "is_executing": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[iter 0] loss=1.5650 val_loss=0.0000 scale=1.0000 norm=1.1076\n",
      "[iter 100] loss=1.1282 val_loss=0.0000 scale=1.0000 norm=0.7727\n",
      "[iter 200] loss=0.9069 val_loss=0.0000 scale=1.0000 norm=0.7003\n",
      "[iter 300] loss=0.7586 val_loss=0.0000 scale=2.0000 norm=1.3529\n",
      "[iter 400] loss=0.6875 val_loss=0.0000 scale=1.0000 norm=0.6776\n"
     ]
    }
   ],
   "source": [
    "from ngboost import NGBRegressor\n",
    "\n",
    "from sklearn.datasets import fetch_california_housing\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X, Y = fetch_california_housing(return_X_y=True, as_frame=True)\n",
    "X_reg_train, X_reg_test, Y_reg_train, Y_reg_test = train_test_split(X, Y, test_size=0.2)\n",
    "\n",
    "ngb = NGBRegressor().fit(X_reg_train, Y_reg_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d2a5dca356dedc72",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "from pathlib import Path\n",
    "\n",
    "# file_path = Path.home()/'Desktop'/'ngbtest.p'\n",
    "file_path = Path.cwd()/'ngtest.p'\n",
    "\n",
    "with file_path.open(\"wb\") as f:\n",
    "    pickle.dump(ngb, f)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "637f3c0607102a46",
   "metadata": {},
   "source": [
    "## 贰丨加载模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b73ce3e09133f1f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "with file_path.open(\"rb\") as f:\n",
    "    ngb_unpickled = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "43991711-9e57-43f3-93e6-626b1806674b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'loc': array([2.51734662, 2.67314501, 1.27700306, 1.95835727, 2.35964955]),\n",
       " 'scale': array([0.592938  , 0.61716588, 0.49899312, 0.39302516, 0.63571354])}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y_preds = ngb_unpickled.predict(X_reg_test)\n",
    "Y_dists = ngb_unpickled.pred_dist(X_reg_test)\n",
    "\n",
    "Y_dists[0:5].params"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
