{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " tv_budget radio_budget social_budget sales_k\n", "0 102.0 33.0 89.0 12.40\n", "1 270.0 30.0 45.0 20.14\n", "2 106.0 9.0 33.0 12.19\n", "3 71.0 18.0 48.0 7.97\n", "4 188.0 31.0 77.0 19.15\n", "\n", "Sales range: $2.0k – $23.2k\n", "\n", "Coefficients (after L1): TV=3.6672, Radio=0.0426, Social=0.5363\n", "R² score : 0.8566\n", "RMSE : $1.73k\n", "\n", "Sample (TV=$150k, Radio=$20k, Social=$60k): $13.4k sales\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "from sklearn.linear_model import Lasso\n", "from sklearn.preprocessing import StandardScaler\n", "from sklearn.pipeline import Pipeline\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", "tv_budget = np.random.randint(0, 300, n).astype(float) # $k spent on TV ads\n", "radio_budget = np.random.randint(0, 50, n).astype(float) # $k spent on radio\n", "social_budget = np.random.randint(0, 100, n).astype(float) # $k spent on social media\n", "\n", "# TV and social drive sales; radio has minimal effect (Lasso will shrink it)\n", "sales_k = (\n", " 0.045 * tv_budget\n", " + 0.004 * radio_budget\n", " + 0.025 * social_budget\n", " + np.random.normal(0, 1.5, n)\n", " + 5.0\n", ").round(2)\n", "\n", "df = pd.DataFrame({\n", " 'tv_budget': tv_budget,\n", " 'radio_budget': radio_budget,\n", " 'social_budget': social_budget,\n", " 'sales_k': sales_k\n", "})\n", "\n", "print(df.head())\n", "print(f'\\nSales range: ${df.sales_k.min():.1f}k \\u2013 ${df.sales_k.max():.1f}k')\n", "\n", "X = df[['tv_budget', 'radio_budget', 'social_budget']]\n", "y = df['sales_k']\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n", "\n", "pipeline = Pipeline([\n", " ('scaler', StandardScaler()),\n", " ('lasso', Lasso(alpha=0.1))\n", "])\n", "pipeline.fit(X_train, y_train)\n", "\n", "y_pred = pipeline.predict(X_test)\n", "r2 = r2_score(y_test, y_pred)\n", "rmse = np.sqrt(mean_squared_error(y_test, y_pred))\n", "\n", "coefs = pipeline.named_steps['lasso'].coef_\n", "print(f'\\nCoefficients (after L1): TV={coefs[0]:.4f}, Radio={coefs[1]:.4f}, Social={coefs[2]:.4f}')\n", "print(f'R\\u00b2 score : {r2:.4f}')\n", "print(f'RMSE : ${rmse:.2f}k')\n", "\n", "sample = pd.DataFrame([[150, 20, 60]], columns=['tv_budget', 'radio_budget', 'social_budget'])\n", "pred = pipeline.predict(sample)[0]\n", "print(f'\\nSample (TV=$150k, Radio=$20k, Social=$60k): ${pred:.1f}k sales')" ] }, { "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_LassoRegression_SalesPredictor.joblib\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e185633c1dc6461cbc712459e56b0615", "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": "ef2629a70be14cd4b80a0606cf05e0d3", "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": "46f7c2f0e80845a999440dfb222ace44", "version_major": 2, "version_minor": 0 }, "text/plain": [ " ...ion_SalesPredictor.joblib: 100%|##########| 1.48kB / 1.48kB " ] }, "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_LassoRegression_SalesPredictor.joblib\")\n", "\n", "joblib.dump(pipeline, 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": "e0624f5ead574de89d675af44671b57a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "ML_LassoRegression_SalesPredictor.joblib: 0%| | 0.00/1.48k [00:00