{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " lot_size_sqm floors rooms crime_rate school_rating price_thousands\n", "0 182 1 7 5.9 7 292.46\n", "1 515 3 6 7.8 2 334.81\n", "2 350 2 7 9.2 6 322.05\n", "3 186 3 7 9.0 7 377.70\n", "4 151 3 8 1.8 10 515.72\n", "\n", "Price range: $72.2k – $618.6k\n", "\n", "R² score : 0.8868\n", "RMSE : $35.93k\n", "\n", "Sample (200m² lot, 2 floors, 5 rooms, crime=3, school=8): $370.7k\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "from sklearn.ensemble import GradientBoostingRegressor\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import mean_squared_error, r2_score\n", "\n", "np.random.seed(42)\n", "n = 400\n", "\n", "lot_size_sqm = np.random.randint(80, 600, n)\n", "floors = np.random.randint(1, 4, n)\n", "rooms = np.random.randint(2, 9, n)\n", "crime_rate = np.round(np.random.uniform(0.5, 9.5, n), 1) # index 0-10, lower is safer\n", "school_rating = np.random.randint(1, 11, n) # 1-10\n", "\n", "price_thousands = (\n", " 0.35 * lot_size_sqm\n", " + 30 * floors\n", " + 20 * rooms\n", " - 15 * crime_rate\n", " + 18 * school_rating\n", " + np.random.normal(0, 20, n)\n", " + 50\n", ").round(2)\n", "price_thousands = np.clip(price_thousands, 50.0, None)\n", "\n", "df = pd.DataFrame({\n", " 'lot_size_sqm': lot_size_sqm,\n", " 'floors': floors,\n", " 'rooms': rooms,\n", " 'crime_rate': crime_rate,\n", " 'school_rating': school_rating,\n", " 'price_thousands': price_thousands\n", "})\n", "\n", "print(df.head())\n", "print(f'\\nPrice range: ${df.price_thousands.min():.1f}k \\u2013 ${df.price_thousands.max():.1f}k')\n", "\n", "X = df[['lot_size_sqm', 'floors', 'rooms', 'crime_rate', 'school_rating']]\n", "y = df['price_thousands']\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n", "\n", "model = GradientBoostingRegressor(n_estimators=200, learning_rate=0.1, max_depth=4, random_state=42)\n", "model.fit(X_train, y_train)\n", "\n", "y_pred = model.predict(X_test)\n", "r2 = r2_score(y_test, y_pred)\n", "rmse = np.sqrt(mean_squared_error(y_test, y_pred))\n", "\n", "print(f'\\nR\\u00b2 score : {r2:.4f}')\n", "print(f'RMSE : ${rmse:.2f}k')\n", "\n", "sample = pd.DataFrame([[200, 2, 5, 3.0, 8]], columns=['lot_size_sqm', 'floors', 'rooms', 'crime_rate', 'school_rating'])\n", "pred = model.predict(sample)[0]\n", "print(f'\\nSample (200m\\u00b2 lot, 2 floors, 5 rooms, crime=3, school=8): ${pred:.1f}k')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.12/dist-packages/huggingface_hub/utils/_auth.py:122: UserWarning: \n", "Error while fetching `HF_TOKEN` secret value from your vault: 'Requesting secret HF_TOKEN timed out. Secrets can only be fetched when running from the Colab UI.'.\n", "You are not authenticated with the Hugging Face Hub in this notebook.\n", "If the error persists, please let us know by opening an issue on GitHub (https://github.com/huggingface/huggingface_hub/issues/new).\n", " warnings.warn(\n" ] } ], "source": [ "from huggingface_hub import notebook_login\n", "\n", "notebook_login()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model saved locally: /content/ML_GradientBoosting_HousePricePredictor.joblib\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ba48ab6b85c34c90a902aa4545924c0f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Processing Files (0 / 0) : | | 0.00B / 0.00B " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4bc019c7879e43049cca1caa952b0c87", "version_major": 2, "version_minor": 0 }, "text/plain": [ "New Data Upload : | | 0.00B / 0.00B " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a4c5c7f729ba4b2db31b6473d890d6bd", "version_major": 2, "version_minor": 0 }, "text/plain": [ " ...ousePricePredictor.joblib: 100%|##########| 461kB / 461kB " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Uploaded model to https://huggingface.co/looh2/model\n" ] } ], "source": [ "import joblib\n", "from pathlib import Path\n", "from huggingface_hub import HfApi\n", "\n", "repo_id = \"looh2/model\"\n", "model_path = Path(\"ML_GradientBoosting_HousePricePredictor.joblib\")\n", "\n", "joblib.dump(model, model_path)\n", "print(f\"Model saved locally: {model_path.resolve()}\")\n", "\n", "api = HfApi()\n", "api.create_repo(repo_id=repo_id, repo_type=\"model\", exist_ok=True)\n", "api.upload_file(\n", " path_or_fileobj=str(model_path),\n", " path_in_repo=model_path.name,\n", " repo_id=repo_id,\n", " repo_type=\"model\",\n", ")\n", "print(f\"Uploaded model to https://huggingface.co/{repo_id}\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c1a443dc4ed64a82b6f8596b3c585130", "version_major": 2, "version_minor": 0 }, "text/plain": [ "ML_GradientBoosting_HousePricePredictor.(…): 0%| | 0.00/461k [00:00