285 lines
7.1 KiB
Plaintext

{
"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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Sales</th>\n",
" <th>Temperature</th>\n",
" <th>Advertising</th>\n",
" <th>Discount</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>17235</td>\n",
" <td>33</td>\n",
" <td>15</td>\n",
" <td>5.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>19854</td>\n",
" <td>42</td>\n",
" <td>25</td>\n",
" <td>5.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>45786</td>\n",
" <td>58</td>\n",
" <td>40</td>\n",
" <td>10.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>49745</td>\n",
" <td>67</td>\n",
" <td>70</td>\n",
" <td>20.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>65894</td>\n",
" <td>73</td>\n",
" <td>75</td>\n",
" <td>20.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<statsmodels.regression.linear_model.RegressionResultsWrapper at 0x12e499550>"
]
},
"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
}