{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "id": "ZbwpTMgRjUMS", "outputId": "7fca63af-b277-4dad-bc59-44ad128cb10a" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SalesTemperatureAdvertisingDiscount
01723533155.0
11985442255.0
245786584010.0
349745677020.0
465894737520.0
\n", "
" ], "text/plain": [ " Sales Temperature Advertising Discount\n", "0 17235 33 15 5.0\n", "1 19854 42 25 5.0\n", "2 45786 58 40 10.0\n", "3 49745 67 70 20.0\n", "4 65894 73 75 20.0" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "from mlModelSaver import MlModelSaver\n", "from helpers import add_constant_column\n", "\n", "mowersDf = pd.read_excel('https://www.dropbox.com/scl/fi/y2rktyoqb8rrshrnlpvw1/Mowers.xlsx?rlkey=e5bi1d8sx5hml4ylfkjv7cryh&dl=1')\n", "mowersDf.head()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "SPmxr6rde0Od" }, "outputs": [], "source": [ "# https://www.statsmodels.org/stable/index.html\n", "import statsmodels.api as sm\n", "# Your answer" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4jjcJF3SfX-h", "outputId": "dac6566b-1320-48ca-db70-712d4a7ff82b" }, "outputs": [], "source": [ "modelPredictSaleByTemperatureAdvertisingDiscount = sm.OLS(\n", " mowersDf[\"Sales\"],\n", " add_constant_column(mowersDf[[\"Temperature\", \"Advertising\", \"Discount\"]])\n", ")\n", "modelPredictSaleByTemperatureAdvertisingDiscountFit = modelPredictSaleByTemperatureAdvertisingDiscount.fit()\n", "# print(modelPredictSaleByTemperatureAdvertisingDiscountFit.summary())" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mlModelSaver import MlModelSaver\n", "mlModelSaverInstance = MlModelSaver({\n", " \"baseRelativePath\": \"..\",\n", " \"modelsFolder\": \"~~tmp/testModels\"\n", "})\n", "\n", "loadedModel = mlModelSaverInstance.exportModel(\n", " modelPredictSaleByTemperatureAdvertisingDiscountFit,\n", " {\n", " \"modelName\": \"modelPredictSaleByTemperatureAdvertisingDiscountFit\",\n", " \"description\": \"modelPredictSaleByTemperatureAdvertisingDiscountFit\",\n", " \"modelType\": \"sm.OLS\",\n", " \"inputs\": [\n", " {\n", " \"name\": \"Temperature\",\n", " \"type\": \"float\",\n", " },\n", " {\n", " \"name\": \"Advertising\",\n", " \"type\": \"float\"\n", " },\n", " {\n", " \"name\": \"Discount\",\n", " \"type\": \"float\"\n", " }\n", " ],\n", " \"transformer\": add_constant_column,\n", " \"outputs\": [\n", " {\n", " \"name\": \"Sales\",\n", " \"type\": \"float\"\n", " }\n", " ]\n", " }\n", ")\n", "loadedModel" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "\n", "testData = [{\n", " 'Temperature': 42,\n", " 'Advertising': 15,\n", " 'Discount': 5\n", "}]\n", "\n", "# Create a DataFrame from the dictionary\n", "testDf = pd.DataFrame(testData)\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 19590.46727\n", "dtype: float64" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "modelPredictSaleByTemperatureAdvertisingDiscountFit.predict( add_constant_column(testDf))\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'Sales': 19590.467270313893}]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loadedModel.mlModelSavePredict(testDf)" ] }, { "cell_type": "markdown", "metadata": { "id": "4YoK17TkeGCw" }, "source": [] } ], "metadata": { "colab": { "provenance": [] }, "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.12.3" } }, "nbformat": 4, "nbformat_minor": 4 }