2024-10-18 19:14:48 +04:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Лабораторная 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Информация о б экономике стран"
]
},
{
"cell_type": "code",
2024-10-18 20:20:52 +04:00
"execution_count": 35,
2024-10-18 19:14:48 +04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Index(['stock index', 'country', 'year', 'index price', 'log_indexprice',\n",
" 'inflationrate', 'oil prices', 'exchange_rate', 'gdppercent',\n",
" 'percapitaincome', 'unemploymentrate', 'manufacturingoutput',\n",
" 'tradebalance', 'USTreasury'],\n",
" dtype='object')\n"
]
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"df = pd.read_csv(\".//static//scv//Economic Data - 9 Countries (1980-2020).csv\")\n",
"print(df.columns)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Столбцы на русском:\n",
"'stock index' - индекс акций\n",
"'country' - страна\n",
"'year'- год\n",
"'index price' - индекс стоимости\n",
"'log_indexprice' - индексная цена журнала\n",
"'inflationrate' - ставка инфляции\n",
"'oil prices' - цена на нефть\n",
"'exchange_rate' - ставка обмена\n",
"'gdppercent' - процент ВВП\n",
"'percapitaincome' - доход на душу населения\n",
"'unemploymentrate' - уровень безработицы\n",
"'manufacturingoutput' - объем производства\n",
"'tradebalance' - торговый баланс\n",
"'USTreasury' - UST казначейство"
]
},
{
"cell_type": "code",
2024-10-18 20:20:52 +04:00
"execution_count": 36,
2024-10-18 19:14:48 +04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 369 entries, 0 to 368\n",
"Data columns (total 14 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 stock index 369 non-null object \n",
" 1 country 369 non-null object \n",
" 2 year 369 non-null float64\n",
" 3 index price 317 non-null float64\n",
" 4 log_indexprice 369 non-null float64\n",
" 5 inflationrate 326 non-null float64\n",
" 6 oil prices 369 non-null float64\n",
" 7 exchange_rate 367 non-null float64\n",
" 8 gdppercent 350 non-null float64\n",
" 9 percapitaincome 368 non-null float64\n",
" 10 unemploymentrate 348 non-null float64\n",
" 11 manufacturingoutput 278 non-null float64\n",
" 12 tradebalance 365 non-null float64\n",
" 13 USTreasury 369 non-null float64\n",
"dtypes: float64(12), object(2)\n",
"memory usage: 40.5+ KB\n"
]
},
{
"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>stock index</th>\n",
" <th>country</th>\n",
" <th>year</th>\n",
" <th>index price</th>\n",
" <th>log_indexprice</th>\n",
" <th>inflationrate</th>\n",
" <th>oil prices</th>\n",
" <th>exchange_rate</th>\n",
" <th>gdppercent</th>\n",
" <th>percapitaincome</th>\n",
" <th>unemploymentrate</th>\n",
" <th>manufacturingoutput</th>\n",
" <th>tradebalance</th>\n",
" <th>USTreasury</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>NASDAQ</td>\n",
" <td>United States of America</td>\n",
" <td>1980.0</td>\n",
" <td>168.61</td>\n",
" <td>2.23</td>\n",
" <td>0.14</td>\n",
" <td>21.59</td>\n",
" <td>1.0</td>\n",
" <td>0.09</td>\n",
" <td>12575.0</td>\n",
" <td>0.07</td>\n",
" <td>NaN</td>\n",
" <td>-13.06</td>\n",
" <td>0.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>NASDAQ</td>\n",
" <td>United States of America</td>\n",
" <td>1981.0</td>\n",
" <td>203.15</td>\n",
" <td>2.31</td>\n",
" <td>0.10</td>\n",
" <td>31.77</td>\n",
" <td>1.0</td>\n",
" <td>0.12</td>\n",
" <td>13976.0</td>\n",
" <td>0.08</td>\n",
" <td>NaN</td>\n",
" <td>-12.52</td>\n",
" <td>0.14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>NASDAQ</td>\n",
" <td>United States of America</td>\n",
" <td>1982.0</td>\n",
" <td>188.98</td>\n",
" <td>2.28</td>\n",
" <td>0.06</td>\n",
" <td>28.52</td>\n",
" <td>1.0</td>\n",
" <td>0.04</td>\n",
" <td>14434.0</td>\n",
" <td>0.10</td>\n",
" <td>NaN</td>\n",
" <td>-19.97</td>\n",
" <td>0.13</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>NASDAQ</td>\n",
" <td>United States of America</td>\n",
" <td>1983.0</td>\n",
" <td>285.43</td>\n",
" <td>2.46</td>\n",
" <td>0.03</td>\n",
" <td>26.19</td>\n",
" <td>1.0</td>\n",
" <td>0.09</td>\n",
" <td>15544.0</td>\n",
" <td>0.10</td>\n",
" <td>NaN</td>\n",
" <td>-51.64</td>\n",
" <td>0.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>NASDAQ</td>\n",
" <td>United States of America</td>\n",
" <td>1984.0</td>\n",
" <td>248.89</td>\n",
" <td>2.40</td>\n",
" <td>0.04</td>\n",
" <td>25.88</td>\n",
" <td>1.0</td>\n",
" <td>0.11</td>\n",
" <td>17121.0</td>\n",
" <td>0.08</td>\n",
" <td>NaN</td>\n",
" <td>-102.73</td>\n",
" <td>0.12</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" stock index country year index price log_indexprice \\\n",
"0 NASDAQ United States of America 1980.0 168.61 2.23 \n",
"1 NASDAQ United States of America 1981.0 203.15 2.31 \n",
"2 NASDAQ United States of America 1982.0 188.98 2.28 \n",
"3 NASDAQ United States of America 1983.0 285.43 2.46 \n",
"4 NASDAQ United States of America 1984.0 248.89 2.40 \n",
"\n",
" inflationrate oil prices exchange_rate gdppercent percapitaincome \\\n",
"0 0.14 21.59 1.0 0.09 12575.0 \n",
"1 0.10 31.77 1.0 0.12 13976.0 \n",
"2 0.06 28.52 1.0 0.04 14434.0 \n",
"3 0.03 26.19 1.0 0.09 15544.0 \n",
"4 0.04 25.88 1.0 0.11 17121.0 \n",
"\n",
" unemploymentrate manufacturingoutput tradebalance USTreasury \n",
"0 0.07 NaN -13.06 0.11 \n",
"1 0.08 NaN -12.52 0.14 \n",
"2 0.10 NaN -19.97 0.13 \n",
"3 0.10 NaN -51.64 0.11 \n",
"4 0.08 NaN -102.73 0.12 "
]
},
2024-10-18 20:20:52 +04:00
"execution_count": 36,
2024-10-18 19:14:48 +04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.info()\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Объект наблюдения - экономика\n",
"Атрибуты - содержит набор информации о б обучении, такие как:\n",
"Фондовый рынок, ВВП, страна, год, стоимость топлива, уровень инфлции,уровень безработицы и так далее"
]
},
{
"cell_type": "code",
2024-10-18 20:20:52 +04:00
"execution_count": 80,
2024-10-18 19:14:48 +04:00
"metadata": {},
"outputs": [
{
"data": {
2024-10-18 20:20:52 +04:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA2kAAAIjCAYAAACZPFMYAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdd5xdVb3//9fe+/Qzc6aX1EmDVEIgKAmhRFpALCAWEDWUyxUuqBB/Fu7Xi4JXvXpFUAGxAEERFbhYKAIxEAQJLRAICemTOpnJ9DPltF1+f0wyMGRS5pxJZiZ5P32MZvZes9dnn52J53PWWp9leJ7nISIiIiIiIoOCOdABiIiIiIiIyLuUpImIiIiIiAwiStJEREREREQGESVpIiIiIiIig4iSNBERERERkUFESZqIiIiIiMggoiRNRERERERkEFGSJiIiIiIiMogoSRMRERERERlElKSJiIiIiIgMIkrSRERkQDz88MMYhtHr17Rp0wY6vCNWe3s73/72tznnnHMoLi7GMAwWLlw40GGJiBxRfAMdgIiIHNn+8z//k8mTJ3d//73vfW8Ao5GGhgZuvvlmRo8ezbHHHsuSJUsGOiQRkSOOkjQRERlQZ511FnPnzu3+/je/+Q0NDQ0DF9ARbtiwYezYsYPKykpee+01PvCBDwx0SCIiRxxNdxQRkQGRTqcBMM39/1/RwoULMQyDTZs2dR9zXZfp06fvMR3vrbfe4tJLL2XcuHGEQiEqKyu5/PLLaWxs7HHN73znO71OtfT53v38cu7cuUybNo1ly5Zx0kknEQ6HGTt2LHfdddce93LjjTcyc+ZMCgoKiEajnHLKKTz77LM92m3atKm7n7/85S89ziWTSYqKijAMgx//+Md7xFleXk4mk+nxM3/4wx+6r/fexPavf/0r5513HsOHDycYDDJ+/Hi++93v4jjOfl/rYDBIZWXlftuJiMjBo5E0EREZELuTtGAwmNXP/+53v2PFihV7HF+0aBEbN27ksssuo7KykpUrV/KrX/2KlStX8tJLL2EYRo/2v/jFL8jLy+v+/v1JY3NzMx/+8If59Kc/zcUXX8yDDz7I1VdfTSAQ4PLLLwcgHo/zm9/8hosvvpgrr7yStrY27r77bubNm8crr7zCjBkzelwzFApx7733cv7553cfe+SRR0gmk3u937a2Nh577DEuuOCC7mP33nsvoVBoj59buHAheXl5LFiwgLy8PJ555hluvPFG4vE4//u//7vXPkREZHBQkiYiIgOitbUVgHA43OefTaVS3HjjjZx77rn8/e9/73HuP/7jP/jqV7/a49isWbO4+OKLeeGFFzjllFN6nPvkJz9JaWnpXvuqqanhlltuYcGCBQB88Ytf5MQTT+SGG27g85//PH6/n6KiIjZt2kQgEOj+uSuvvJJJkybx85//nLvvvrvHNS+44AIeeugh6urqqKioAOCee+7hE5/4BA888ECvcVxwwQXcc8893Unali1bWLx4MZ/5zGf4wx/+0KPtAw880ON1veqqq7jqqqu48847+e///u+sE2MRETk0NN1RREQGxO7ph2VlZX3+2TvuuIPGxka+/e1v73HuvclJMpmkoaGBWbNmAfD666/3uS+fz8cXv/jF7u8DgQBf/OIX2blzJ8uWLQPAsqzuBM11XZqamrBtmxNOOKHXPo8//nimTp3K7373OwA2b97Ms88+y6WXXrrXOC6//HKefPJJamtrAbjvvvuYPXs2Rx999B5t3/satLW10dDQwCmnnEJnZyerV6/u82sgIiKHlpI0EREZEJs3b8bn8/U5SWttbeX73/8+CxYs6B6Feq+mpia+8pWvUFFRQTgcpqysjLFjx3b/bF8NHz6caDTa49juxOi9a+Tuu+8+pk+fTigUoqSkhLKyMh5//PG99nnZZZdx7733Al3TE0866SSOOuqovcYxY8YMpk2bxm9/+1s8z2PhwoVcdtllvbZduXIlF1xwAQUFBcRiMcrKyvjc5z4HZPcaiIjIoaUkTUREBsSaNWsYN25cj0IdB+KHP/whpmnyta99rdfzn/70p/n1r3/NVVddxSOPPMLTTz/Nk08+CXSNch0M999/P5deeinjx4/n7rvv5sknn2TRokWcfvrpe+3zc5/7HOvXr+ell17ivvvu22vC9V6XX3459957L8899xy1tbV8+tOf3qNNS0sLp512Gm+++SY333wzjz76KIsWLeKHP/whcPBeAxER6T9akyYiIodcKpVi+fLlPQpnHIiamhp++tOf8oMf/ID8/Pw9KjY2NzezePFibrrpJm688cbu4+vWrcs61pqaGjo6OnqMpq1duxaAMWPGAF0bc48bN45HHnmkR2GS3qZj7lZSUsLHPvax7qmTn/70p/e79cAll1zC1772Nb7yla/wyU9+kvz8/D3aLFmyhMbGRh555BFOPfXU7uPV1dUHdL8iIjLwNJImIiKH3AMPPEAqleKMM87o08/ddNNNVFRUcNVVV/V63rIsADzP63H8tttuyypOANu2+eUvf9n9fTqd5pe//CVlZWXMnDlzr/2+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ggoiRNRERERERkEFGSJiIiIiIiMogoSRMRERERERlElKSJiIiIiIgMIkrSREREREREBhElaSIiIiIiIoOIkjQREREREZFBREmaiIiIiIjIIKIkTUREREREZBBRkiYiB8zzEnhuM56XGehQRERERA5bvoEOQEQGP8/egpdaDOnXwLPBjEHwZAiegWHmDXR4IiIiIocVJWkisk9eZi1e+x3g1oJRBEYI3Ea8zj9A5m3I+xKGGRvoMEVEREQOG5ruKCJ75Xk2Xuf94O4EawJYZV2jaNYIsEZD5i285FMDHaaIiIjIYUVJmojsnb0anE1dSZnxvn8ujCAYMUj9C8/tHJDwRERERA5HStJEZO+cevAyYIR7P2/kg9cGXvOhjUtERETkMKYkTUT2zgh0/a/n7KV
2024-10-18 19:14:48 +04:00
"text/plain": [
"<Figure size 1000x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize=(10, 6))\n",
"\n",
"plt.scatter(df['inflationrate'], df['percapitaincome'], c=df['percapitaincome'], alpha=0.6)\n",
"\n",
2024-10-18 20:20:52 +04:00
"plt.title(\"Диаграмма 1\")\n",
2024-10-18 19:14:48 +04:00
"plt.ylabel(\"Доход на душу населения\")\n",
"plt.xlabel(\"Уровень инфляции\")\n",
"plt.grid(visible='true')\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "code",
2024-10-18 20:20:52 +04:00
"execution_count": 79,
2024-10-18 19:14:48 +04:00
"metadata": {},
"outputs": [
{
"data": {
2024-10-18 20:20:52 +04:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA/IAAAIjCAYAAACgdyAGAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAACpxElEQVR4nOzdd3xTdfcH8M/N7l7pgFJoyxDK3lNARUEFBFTQx4H4/HAiKMgjOBAnoMCDAgou9HEiCgoqCDJUliDIKHu00N2me4/k/v5Ib0qhQFOS3uTm8369+pKmNzenoNiTc77nCKIoiiAiIiIiIiIit6CSOwAiIiIiIiIiqj8m8kRERERERERuhIk8ERERERERkRthIk9ERERERETkRpjIExEREREREbkRJvJEREREREREboSJPBEREREREZEbYSJPRERERERE5EaYyBMRERERERG5ESbyRERERERERG6EiTwREZFMvvvuOwiCUOdHhw4d5A7PY+3duxeTJk1C+/bt4ePjg+bNm2Ps2LE4efKk3KEREREBADRyB0BEROTpnn/+ebRr1872+RtvvCFjNDRv3jzs2LEDd999Nzp16oT09HQsWbIE3bp1w+7du/kmCxERyU4QRVGUOwgiIiJP9N133+Huu+/G1q1bMXjwYNvjgwcPhslkQnx8vHzBebCdO3eiR48e0Ol0tsdOnTqFjh074q677sIXX3whY3RERERsrSciIpJNRUUFAECluvr/jj/99FMIgoDExETbYxaLBZ06dYIgCPj0009tjx86dAgPPfQQYmNjYTAYEBERgYcffhjZ2dm17jl79uw62/o1mpqGvcGDB6NDhw7Yt28f+vXrBy8vL8TExGDZsmWXfC+zZs1C9+7dERAQAB8fH1x//fXYunVrresSExNtr/PDDz/U+lpZWRmCgoIgCALmz59/SZxhYWGorKys9Zyvv/7adj+TyWR7/Mcff8Ttt9+Opk2bQq/Xo2XLlnjttddgNpuv+nvdr1+/Wkk8ALRu3Rrt27fHsWPHrvp8IiIiZ2NrPRERkUykRF6v1zfo+Z9//jkOHz58yeObNm3C2bNnMWHCBERERODIkSP44IMPcOTIEezevRuCINS6/v3334evr6/t84vfWMjNzcVtt92GsWPH4t5778W3336Lxx9/HDqdDg8//DAAoKCgAB999BHuvfdeTJw4EYWFhfj4448xdOhQ7NmzB126dKl1T4PBgBUrVmDUqFG2x1avXo2ysrLLfr+FhYX46aefMHr0aNtjK1asgMFguOR5n376KXx9fTF16lT4+vpiy5YtmDVrFgoKCvD2229f9jUuRxRFZGRkoH379nY/l4iIyNGYyBMREckkPz8fAODl5WX3c8vLyzFr1izceuutWL9+fa2vPfHEE5g2bVqtx/r06YN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FEUURhYSGaNm0KlerKNXcm8nVITU1FVFSU3GEQERERERGRh0lKSkKzZs2ueA0T+Tr4+fkBsP4G+vv7yxwNERERERERKV1BQQGioqJs+eiVMJGvg9RO7+/vz0SeiIiIiIiIGk19jndz2B0RERERERGRG2EiT0RERERERORGmMgTERERERERuREm8kRERERERERuhIk8ERERERERkRthIk9ERERERETkRpjIExEREREREbkRJvJEREREREREboSJPBEREREREZEbYSJPRERERERE5EaYyBMRERERERG5ESbyRERERERERG6EiTwRERERERGRG2EiT0RERERERORGXCKRX7p0KaKjo2EwGNC7d2/s2bPnstceOXIEd955J6KjoyEIAhYtWlTndSkpKbj//vsREhICLy8vdOzYEX///beTvgMiIiIiIiKixiF7Ir9
2024-10-18 19:14:48 +04:00
"text/plain": [
"<Figure size 1200x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"year_condition = df.groupby('gdppercent')['unemploymentrate'].mean().reset_index()\n",
"\n",
"plt.figure(figsize=(12, 6))\n",
"\n",
"plt.plot(year_condition['gdppercent'], year_condition['unemploymentrate'], marker='.')\n",
"\n",
"plt.title(\"Диаграмма 2\")\n",
2024-10-18 20:20:52 +04:00
"plt.xlabel(\"Уровень ВВП\")\n",
"plt.ylabel(\"Уровень безработицы\")\n",
2024-10-18 19:14:48 +04:00
"\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Присутствует связь между атрибутами, уровень инфляции влияет и зависит от многих атрибутов.\n",
"Для примера на графике приведена связь между инфляцией и доходом на душу населения. Н а втором графике показана связь уровня ВВП и безработицы\n",
"Примеры бизнес целей\n",
"\n",
" 1.Прогнозирование уровня инфляции на основе уровня ВВП.\n",
" 2.Наблюдение за изменениями уровня безработицы с уровнем ВВП.\n",
" \n",
"Эффект для бизнеса: влияние на инвестиции индекса акций и цен на нефть, исследование влияния фондового индекса на инвестиции, исследования инфляции и покупательской способности.\n",
"Цели технического проекта\n",
"\n",
"Для первой цели:\n",
"\n",
"Вход: Доход на душу населения\n",
"Целевой признак: Уровень инфляции.\n",
"\n",
"Для второй цели:\n",
"\n",
"Вход: Уровень безработицы\n",
"Целевой признак: Уровень ВВП"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Проверка на выбросы"
]
},
{
"cell_type": "code",
2024-10-18 20:20:52 +04:00
"execution_count": 39,
2024-10-18 19:14:48 +04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Пропущенные значения по столбцам:\n",
"stock index 0\n",
"country 0\n",
"year 0\n",
"index price 52\n",
"log_indexprice 0\n",
"inflationrate 43\n",
"oil prices 0\n",
"exchange_rate 2\n",
"gdppercent 19\n",
"percapitaincome 1\n",
"unemploymentrate 21\n",
"manufacturingoutput 91\n",
"tradebalance 4\n",
"USTreasury 0\n",
"dtype: int64\n",
"\n",
"Статистический обзор данных:\n",
" year index price log_indexprice inflationrate oil prices \\\n",
"count 369.000000 317.000000 369.000000 326.000000 369.000000 \n",
"mean 2000.000000 7898.648297 3.610542 0.041748 39.743171 \n",
"std 11.848225 7811.336862 0.482481 0.039579 25.452654 \n",
"min 1980.000000 168.610000 2.230000 -0.040000 11.350000 \n",
"25% 1990.000000 2407.100000 3.320000 0.020000 19.410000 \n",
"50% 2000.000000 5160.100000 3.600000 0.030000 28.520000 \n",
"75% 2010.000000 10279.500000 3.980000 0.057500 57.880000 \n",
"max 2020.000000 47751.330000 4.680000 0.240000 98.560000 \n",
"\n",
" exchange_rate gdppercent percapitaincome unemploymentrate \\\n",
"count 367.000000 350.000000 368.000000 348.000000 \n",
"mean 27.897548 0.037114 20719.964674 0.068908 \n",
"std 49.620521 0.037850 17435.037783 0.043207 \n",
"min 0.900000 -0.110000 27.000000 0.020000 \n",
"25% 1.330000 0.020000 2090.250000 0.040000 \n",
"50% 5.440000 0.030000 19969.500000 0.060000 \n",
"75% 15.055000 0.060000 36384.000000 0.090000 \n",
"max 249.050000 0.150000 65280.000000 0.260000 \n",
"\n",
" manufacturingoutput tradebalance USTreasury \n",
"count 278.000000 365.000000 369.000000 \n",
"mean 328.084820 -15.996384 0.059024 \n",
"std 622.395923 154.557170 0.033086 \n",
"min 0.590000 -770.930000 0.010000 \n",
"25% 80.380000 -25.370000 0.030000 \n",
"50% 188.160000 -0.140000 0.050000 \n",
"75% 271.977500 19.080000 0.080000 \n",
"max 3868.460000 366.140000 0.140000 \n"
]
}
],
"source": [
"null_values = df.isnull().sum()\n",
"print(\"Пропущенные значения по столбцам:\")\n",
"print(null_values)\n",
"\n",
"stat_summary = df.describe()\n",
"print(\"\\nС та тис тиче с кий обзор данных:\")\n",
"print(stat_summary)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Н а основе данных выше можно выделить большое количество столбцов с пропущенными значениями\n",
"Также проверим данные на выбросы и дубликаты:"
]
},
{
"cell_type": "code",
2024-10-18 20:20:52 +04:00
"execution_count": 40,
2024-10-18 19:14:48 +04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Коэффициент асимметрии для столбца 'year': 0.0\n",
"\n",
"Коэффициент асимметрии для столбца 'index price': 1.7605604508668822\n",
"\n",
"Коэффициент асимметрии для столбца 'log_indexprice': -0.23716751168770417\n",
"\n",
"Коэффициент асимметрии для столбца 'inflationrate': 1.5616085380027898\n",
"\n",
"Коэффициент асимметрии для столбца 'oil prices': 0.9915046764713877\n",
"\n",
"Коэффициент асимметрии для столбца 'exchange_rate': 2.1575952097650455\n",
"\n",
"Коэффициент асимметрии для столбца 'gdppercent': -0.038272329611460466\n",
"\n",
"Коэффициент асимметрии для столбца 'percapitaincome': 0.3051430219264069\n",
"\n",
"Коэффициент асимметрии для столбца 'unemploymentrate': 1.8092896369785585\n",
"\n",
"Коэффициент асимметрии для столбца 'manufacturingoutput': 4.195480293406057\n",
"\n",
"Коэффициент асимметрии для столбца 'tradebalance': -2.266183907194849\n",
"\n",
"Коэффициент асимметрии для столбца 'USTreasury': 0.6687596580836408\n",
"\n",
"Количество дубликатов: 0\n"
]
}
],
"source": [
"for column in df.select_dtypes(include=[np.number]).columns:\n",
" skewness = df[column].skew()\n",
" print(f\"\\nК о эффицие нт асимметрии для столбца '{column}': {skewness}\")\n",
"\n",
"duplicates = df.duplicated().sum()\n",
"print(f\"\\nК о личе с тво дубликатов: {duplicates}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Н а основе данных выше можно сказать, что для столбца объем производства присутствует выброс.\n",
"Удаляем все найденные пустые значения."
]
},
{
"cell_type": "code",
2024-10-18 20:20:52 +04:00
"execution_count": 41,
2024-10-18 19:14:48 +04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"В наборе данных 'Economic' было удалено 150 строк с пустыми значениями.\n"
]
}
],
"source": [
"def drop_missing_values(dataframe, name):\n",
" before_shape = dataframe.shape \n",
" cleaned_dataframe = dataframe.dropna() \n",
" after_shape = cleaned_dataframe.shape \n",
" print(f\"В наборе данных '{name}' было удалено {before_shape[0] - after_shape[0]} строк с пустыми значениями.\")\n",
" return cleaned_dataframe\n",
"\n",
"cleaned_df = drop_missing_values(df, \"Economic\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Очистка данных от шумов:"
]
},
{
"cell_type": "code",
2024-10-18 20:20:52 +04:00
"execution_count": 42,
2024-10-18 19:14:48 +04:00
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA2gAAAIjCAYAAAB2/jgmAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAByTElEQVR4nO3deXxU1f3/8fdMQhKWZJIAyQQaICyKkc2AQFCECpaIgtTWhYIiWlBcqnUFW42xtWipuxaU/gQV91b9itooRVGxwSiLGgJUMAGUhEACSViSQOb8/kgzZcgkmaxzJ3k9H495wNx77rmfc+bO5H7m3jnHZowxAgAAAAD4nd3fAQAAAAAAqpCgAQAAAIBFkKABAAAAgEWQoAEAAACARZCgAQAAAIBFkKABAAAAgEWQoAEAAACARZCgAQAAAIBFkKABAAAAgEWQoAEAALRhP/zwg5YvX+5+npubq5deesl/AQGoEwkaAEv7+9//LpvN5vUxaNAgf4cHAJZns9l0ww036IMPPlBubq7uvPNOffbZZ/4OC0Atgv0dAAD44u6779Zpp53mfv7AAw/4MRoACBw9e/bUnDlzlJKSIkmKi4vTmjVr/BsUgFrZjDHG30EAQG3+/ve/65JLLtHHH3+s8ePHu5ePHz9e+/fvV1ZWlv+CA4AAsmPHDu3fv1+DBg1S586d/R0OgFpwiyMAS6uoqJAk2e31f1wtX75cNptNubm57mUul0tDhgyRzWbz+A3GN998o6uuukp9+/ZVWFiYnE6nrr76ahUWFnrUed9993m9vTI4+H83IIwfP16DBg3S+vXrNWbMGHXs2FEJCQlasmRJjbbce++9Gj58uBwOhzp37qyxY8fq448/9iiXm5vr3s/bb7/tsa6srExRUVGy2Wz6y1/+UiPOmJgYHTt2zGObV155xV3f/v373cv/7//+TxdccIF69Oih0NBQ9evXT3/4wx9UWVlZb19X72/r1q269NJLFRERoa5du+rmm29WWVmZR9lly5bp3HPPVUxMjEJDQ5WYmKjFixd7rfef//ynxo0bp/DwcEVEROjMM8/Uyy+/7FHmiy++0OTJkxUVFaXOnTtryJAhevzxxz3KbN26Vb/85S8VHR2tsLAwjRgxQu+8845HmYYcL1dddZXH6x8VFaXx48fXuE3M1z6tPmZO9pe//KVGTH369NFVV13lUe6NN96QzWZTnz59PJYXFBTommuuUa9evRQUFOSOt0uXLjX2dbI+ffrUejuxzWarUX7FihUaPny4OnbsqOjoaF1++eXavXu313bW996QpPLycqWmpqp///4KDQ1VfHy87rzzTpWXl9cou2bNGp/jPFn1seut/Sf2c0OOD0nu90L37t3VsWNHnXrqqfrd737nsc+6HtVXtMaPH+/xZZRUdceA3W6v8V5444033K9Bt27dNHPmTP34448eZa666ir3cdKvXz+NGjVKRUVF6tixY432AbAGbnEEYGnVCVpoaGijtn/xxRf17bff1li+atUqff/995o9e7acTqc2b96sZ599Vps3b9a6detqnMAtXrzY4yT35ITxwIEDmjx5si699FJNnz5dr7/+uubNm6eQkBBdffXVkqSSkhL97W9/0/Tp0zVnzhyVlpbq//2//6dJkyYpMzNTw4YN86gzLCxMy5Yt07Rp09zL3nzzzRoJ0IlKS0v17rvv6uc//7l72bJlyxQWFlZju+XLl6tLly669dZb1aVLF3300Ue69957VVJSokWLFtW6jxNdeuml6tOnjxYuXKh169bpiSee0IEDB/TCCy949N3pp5+uqVOnKjg4WCtXrtT1118vl8ulG264wSOeq6++WqeffroWLFigyMhIbdy4Uenp6frVr34lqep1u/DCCxUXF6ebb75ZTqdTW7Zs0bvvvqubb75ZkrR582adddZZ6tmzp+bPn6/OnTvr9ddf17Rp0/SPf/zDo29OVtvxIkndunXTo48+Kqlq0IXHH39ckydP1u7duxUZGdlsfVqf48ePu0/8TzZr1iz961//0k033aShQ4cqKChIzz77rDZs2OBT3cOGDdNtt93mseyFF17QqlWrPJY98MADuueee3TppZfq17/+tfbt26cnn3xS55xzjjZu3OjuD8m394bL5dLUqVO1du1azZ07V6eddpq+/fZbPfroo/rPf/5T44uKar/5zW905pln1hpnc6vt+Pjmm280duxYdejQQXPnzlWfPn20Y8cOrVy5Ug888IAuvvhi9e/f313+t7/9rU477TTNnTvXvezEW7hPtGzZMv3+97/Xww8/7H4fSFXH2uzZs3XmmWdq4cKF2rt3rx5//HF9/vnnNV6Dk9177711fo4A8DMDABb22GOPGUnm66+/9lg+btw4c/rpp3ssW7ZsmZFkcnJyjDHGlJWVmV69epnzzz/fSDLLli1zlz1y5EiNfb3yyitGkvn000/dy1JTU40ks2/fvlpjHDdunJFkHn74Yfey8vJyM2zYMBMTE2MqKiqMMcYcP37clJeXe2x74MABExsba66++mr3spycHCPJTJ8+3QQHB5v8/Hz3ugkTJphf/epXRpJZtGhRjTinT59uLrzwQvfynTt3GrvdbqZPn16jHd764NprrzWdOnUyZWVltbb3xP1NnTrVY/n1119f4/Xytp9JkyaZvn37up8fPHjQhIeHm1GjRpmjR496lHW5XMaYqv5LSEgwvXv3NgcOHPBaxpiqPho8eLBHG1wulxkzZowZMGCAe1lDjpdZs2aZ3r17e+zz2WefNZJMZmZmnW311qfejl9jjFm0aJFHTMYY07t3bzNr1iz387/+9a8mNDTU/PSnP/WI6ejRo8Zut5trr73Wo85Zs2aZzp0719jXyXr37m0uuOCCGstvuOEGc+LpQm5urgkKCjIPPPCAR7lvv/3WBAcHeyz39b3x4osvGrvdbj777DOPOpcsWWIkmc8//9xj+Ycffmgkmb///e+1xlmbtLQ0I8njmKlu/4n93JDj45xzzjHh4eFm586dHnWevI/a9nWicePGmXHjxhljjHnvvfdMcHCwue222zzKVFRUmJiYGDNo0CCP98u7775rJJl7773XvezkYzcrK8vY7XZ3O0481gBYA7c4ArC06lsOu3fv3uBtn376aRUWFio1NbXGuo4dO7r/X1ZWpv3792v06NGS5PPVhhMFBwfr2muvdT8PCQnRtddeq4KCAq1fv16SFBQUpJCQEElVVwyKiop0/PhxjRgxwus+k5KSdPrpp+vFF1+UJO3cuVMff/xxjdvdTnT11VcrPT1d+fn5kqTnn39eycnJOuWUU2qUPbEPSktLtX//fo0dO1ZHjhzR1q1bfWr3iVfAJOmmm26SJL3//vte91NcXKz9+/dr3Lhx+v7771VcXCyp6spYaWmp5s+fr7CwMI86q69mbty4UTk5ObrllltqXB2oLlNUVKSPPvpIl156qbtN+/fvV2FhoSZNmqTvvvuuxi1g1eo6XqSq16y6vk2bNumFF15QXFycx5WPhvRpZWWlu77qx5EjR7zuu9qRI0d0//3368Ybb1SvXr081h0+fFgul0tdu3ats46mevPNN+VyuXTppZd6xO50OjVgwIAat+z68t544403dNppp2ngwIEedZ577rmSVKPO6qs/Jx8rvoiJiZFUdRW0IWo7Pvbt26dPP/1UV199dY3XxJdbLmuTmZmpSy+9VL/4xS9qXH396quvVFBQoOuvv96jDy644AINHDhQ7733Xq31LliwQElJSbrkkksaHRuAlsUtjgAsbefOnQoODm5wglZcXKw//elPuvXWWxUbG1tjfVFRkdLS0vTqq6+qoKCgxrYN1aNHjxo/uq9OinJzc93J3/PPP6+HH35YW7du9fitWEJCgtd6Z8+erWeffVa33367li9frjFjxmjAgAG1xjFs2DANGjRIL7zwgu644w4tX75cd999d43fBklVtwL+/ve/10cffaSSkhKPdb72wcmx9OvXT3a73eN3LZ9//rlSU1OVkZFRIwEpLi6Ww+HQjh07JKnOqRN8KbN9+3YZY3TPPffonnvu8VqmoKBAPXv
"text/plain": [
"<Figure size 1000x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Выбросы в датасете:\n",
" stock index country year index price log_indexprice inflationrate \\\n",
"229 SZCOMP China 2004.0 1467.57 3.17 0.04 \n",
"230 SZCOMP China 2005.0 1144.54 3.06 0.02 \n",
"231 SZCOMP China 2006.0 1687.14 3.23 0.02 \n",
"232 SZCOMP China 2007.0 4329.44 3.64 0.05 \n",
"233 SZCOMP China 2008.0 2912.90 3.46 0.06 \n",
"234 SZCOMP China 2009.0 2737.01 3.44 -0.01 \n",
"235 SZCOMP China 2010.0 2795.88 3.45 0.03 \n",
"236 SZCOMP China 2011.0 2639.19 3.42 0.06 \n",
"237 SZCOMP China 2012.0 2211.11 3.34 0.03 \n",
"238 SZCOMP China 2013.0 2182.52 3.34 0.03 \n",
"239 SZCOMP China 2014.0 2279.75 3.36 0.02 \n",
"240 SZCOMP China 2015.0 3657.40 3.56 0.01 \n",
"241 SZCOMP China 2016.0 2978.14 3.47 0.02 \n",
"242 SZCOMP China 2017.0 3257.35 3.51 0.02 \n",
"243 SZCOMP China 2018.0 2920.18 3.47 0.02 \n",
"244 SZCOMP China 2019.0 2928.94 3.47 0.03 \n",
"245 SZCOMP China 2020.0 3109.78 3.49 0.02 \n",
"271 DAX 30 Germany 2005.0 5408.25 3.73 0.02 \n",
"272 DAX 30 Germany 2006.0 6596.91 3.82 0.02 \n",
"273 DAX 30 Germany 2007.0 8067.31 3.91 0.02 \n",
"274 DAX 30 Germany 2008.0 4810.20 3.68 0.03 \n",
"276 DAX 30 Germany 2010.0 6914.19 3.84 0.01 \n",
"277 DAX 30 Germany 2011.0 5898.35 3.77 0.02 \n",
"280 DAX 30 Germany 2014.0 9805.55 3.99 0.01 \n",
"281 DAX 30 Germany 2015.0 10743.01 4.03 0.01 \n",
"283 DAX 30 Germany 2017.0 12917.64 4.11 0.02 \n",
"284 DAX 30 Germany 2018.0 10558.96 4.02 0.02 \n",
"285 DAX 30 Germany 2019.0 13249.01 4.12 0.01 \n",
"286 DAX 30 Germany 2020.0 13718.78 4.14 0.01 \n",
"\n",
" oil prices exchange_rate gdppercent percapitaincome unemploymentrate \\\n",
"229 43.15 8.28 0.10 1509.0 0.04 \n",
"230 59.41 8.19 0.11 1753.0 0.04 \n",
"231 61.96 7.97 0.13 2099.0 0.04 \n",
"232 91.69 7.61 0.14 2694.0 0.04 \n",
"233 41.12 6.95 0.10 3468.0 0.04 \n",
"234 74.47 6.83 0.09 3832.0 0.04 \n",
"235 89.15 6.77 0.11 4550.0 0.04 \n",
"236 98.56 6.46 0.10 5618.0 0.04 \n",
"237 87.86 6.31 0.08 6317.0 0.04 \n",
"238 97.63 6.15 0.08 7051.0 0.05 \n",
"239 59.29 6.16 0.07 7679.0 0.05 \n",
"240 37.19 6.28 0.07 8067.0 0.05 \n",
"241 51.97 6.64 0.07 8148.0 0.05 \n",
"242 57.88 6.76 0.07 8879.0 0.04 \n",
"243 49.52 6.61 0.07 9977.0 0.04 \n",
"244 59.88 6.91 0.06 10217.0 0.05 \n",
"245 47.02 6.90 0.02 10500.0 0.05 \n",
"271 59.41 1.24 0.01 34520.0 0.12 \n",
"272 61.96 1.26 0.04 36354.0 0.11 \n",
"273 91.69 1.37 0.03 41640.0 0.09 \n",
"274 41.12 1.47 0.01 45613.0 0.08 \n",
"276 89.15 1.33 0.04 41572.0 0.08 \n",
"277 98.56 1.39 0.04 46706.0 0.07 \n",
"280 59.29 1.33 0.02 48024.0 0.07 \n",
"281 37.19 1.11 0.01 41103.0 0.06 \n",
"283 57.88 1.13 0.03 44553.0 0.06 \n",
"284 49.52 1.18 0.01 47811.0 0.05 \n",
"285 59.88 1.12 0.01 46468.0 0.05 \n",
"286 47.02 1.14 -0.05 45724.0 0.06 \n",
"\n",
" manufacturingoutput tradebalance USTreasury \n",
"229 625.22 51.17 0.04 \n",
"230 733.66 124.63 0.04 \n",
"231 893.13 208.92 0.05 \n",
"232 1149.72 308.04 0.05 \n",
"233 1475.66 348.83 0.04 \n",
"234 1611.95 220.13 0.03 \n",
"235 1924.32 222.40 0.03 \n",
"236 2421.37 180.89 0.03 \n",
"237 2690.09 231.87 0.02 \n",
"238 2935.34 234.87 0.02 \n",
"239 3184.24 221.55 0.03 \n",
"240 3202.50 358.84 0.02 \n",
"241 3153.12 255.48 0.02 \n",
"242 3460.33 215.70 0.02 \n",
"243 3868.46 106.71 0.03 \n",
"244 3823.41 164.99 0.02 \n",
"245 3853.81 366.14 0.01 \n",
"271 571.36 148.05 0.04 \n",
"272 618.70 162.20 0.05 \n",
"273 714.38 231.95 0.05 \n",
"274 750.91 227.47 0.04 \n",
"276 669.57 178.90 0.03 \n",
"277 758.60 184.02 0.03 \n",
"280 786.55 257.40 0.03 \n",
"281 683.20 255.02 0.02 \n",
"283 752.02 257.66 0.02 \n",
"284 795.96 243.72 0.03 \n",
"285 737.94 223.82 0.02 \n",
"286 678.29 221.53 0.01 \n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA2IAAAIjCAYAAABh3KjvAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAB0Y0lEQVR4nO3deXxU1f3/8fdMQhICWYGQYDEJoNWIgKBAV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tUPQPhxRwxA1HImYomJiQHt/9JLL+nLL79stX3JkiX67rvvdMUVVyg7O1sbNmzQc889pw0bNmjlypWtPnDNnj3b7cNsy8Rw165dOvPMM3XRRRdp4sSJeuONNzRt2jQlJCToyiuvlCTV1tbq+eef18SJEzVlyhTV1dXp//2//6exY8eqpKREQ4YMcTtmUlKS5s2bpwkTJri2/f3vf2+V6DRXV1end955R+edd55r27x585SUlNRqv/nz56t79+66+eab1b17d33wwQe66667VFtbq4cfftjrOZq76KKLlJeXp1mzZmnlypV68skntWvXLr344otubXfUUUfpnHPOUXx8vBYvXqxrrrlGDodD1157rVs8V155pY466ijNmDFD6enpWrt2rYqLi/XrX/9aUlO/nX322crJydENN9yg7OxsffXVV3rnnXd0ww03SJI2bNigE044QYcccoimT5+ubt266Y033tCECRP0t7/9za1tWvI2XiSpZ8+eeuyxxyRJP/zwg5544gmdeeaZ2rp1q9LT04PWpu05ePCg/vCHP3h8bfLkyXr//fd1/fXXa/DgwYqLi9Nzzz2nNWvW+HTsIUOG6JZbbnHb9uKLL2rJkiVu237zm99owYIFuuCCC3TLLbdo1apVmjVrlr766iu9+eabrnK+9GlzV199tUaNGiWpaaw3P5YkTZ06VfPnz9cVV1yh3/3udyorK9PTTz+ttWvXavny5erSpYtP9fTX7bff7nH7zJkzdffdd+v444/XPffco4SEBK1atUoffPCBTj/9dD3++OPavXu3JOmrr77S/fff7/aYtbcE+cCBAzr//PP1/fffa/ny5crJyfEa2zfffKO5c+d2sIYAQsYAQJR6/PHHjSTz+eefu20fPXq0Oeqoo9y2zZs3z0gyZWVlxhhj6uvrzaGHHmrOOOMMI8nMmzfPVXbv3r2tzvXqq68aSebjjz92bSsqKjKSzE8//eQ1xtGjRxtJ5s9//rNr2/79+82QIUNMVlaWaWhoMMYYc/DgQbN//363fXft2mV69+5trrzySte2srIyI8lMnDjRxMfHm4qKCtdrp556qvn1r39tJJmHH364VZwTJ040Z599tmv7li1bjN1uNxMnTmxVD09tMHXqVJOcnGzq6+u91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StLVu2KD4+3u9ErKamRvfff79uvvlm9e7du9XrVVVVuuGGG9S7d2917dpVvXr1Un5+vmtff/Xp00fdunVz2+ZMfpr/vc+CBQs0aNAgJSUlqUePHurVq5f+8Y9/eD3nFVdcoXnz5klqeszr+OOP12GHHeY1jiFDhmjgwIF68cUXZYxxPcblyYYNG3TeeecpLS1Nqamp6tWrlyZNmiTJ9zZoGUv//v1lt9vd6rx8+XKNGTNG3bp1U3p6unr16qU77rjD7TzffvutJLW5JIEvZb755hsZY3TnnXeqV69ebl/OBKuysrLVfu2NF0naunWr61jHHHOMvv32W/3tb39ze7zMnzbduHGj1xi9efTRR1VfX+9qv+Z69Oihww47TM8//7zee+89VVZWaseOHR7/bqsjtmzZIrvdrgEDBrhtz87OVnp6urZs2SLJt/5yqq6uluT9UT1J2rRpk2pqapSVldW
"text/plain": [
"<Figure size 1000x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize=(10, 6))\n",
"plt.scatter(cleaned_df['manufacturingoutput'], cleaned_df['gdppercent'])\n",
"plt.xlabel('Объем производства')\n",
"plt.ylabel('ВВП')\n",
"plt.title('Диаграмма рассеивания перед чисткой')\n",
"plt.show()\n",
"\n",
"Q1 = cleaned_df[\"manufacturingoutput\"].quantile(0.25)\n",
"Q3 = cleaned_df[\"manufacturingoutput\"].quantile(0.75)\n",
"\n",
"IQR = Q3 - Q1\n",
"\n",
"threshold = 1.5 * IQR\n",
"lower_bound = Q1 - threshold\n",
"upper_bound = Q3 + threshold\n",
"\n",
"outliers = (cleaned_df[\"manufacturingoutput\"] < lower_bound) | (cleaned_df[\"manufacturingoutput\"] > upper_bound)\n",
"\n",
"# Вывод выбросов\n",
"print(\"Выбросы в датасете:\")\n",
"print(cleaned_df[outliers])\n",
"\n",
"# Заменяем выбросы на медианные значения\n",
"median_score = cleaned_df[\"manufacturingoutput\"].median()\n",
"cleaned_df.loc[outliers, \"manufacturingoutput\"] = median_score\n",
"\n",
"# Визуализация данных после обработки\n",
"plt.figure(figsize=(10, 6))\n",
"plt.scatter(cleaned_df['manufacturingoutput'], cleaned_df['gdppercent'])\n",
"plt.xlabel('Объем производства')\n",
"plt.ylabel('ВВП')\n",
"plt.title('Диаграмма рассеивания после чистки')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Разбиение набора данных на обучающую, контрольную и тестовую выборки"
]
},
{
"cell_type": "code",
2024-10-18 20:20:52 +04:00
"execution_count": 43,
2024-10-18 19:14:48 +04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Размер обучающей выборки: 131\n",
"Размер контрольной выборки: 44\n",
"Размер тестовой выборки: 44\n"
]
}
],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"train_df, test_df = train_test_split(cleaned_df, test_size=0.2, random_state=42)\n",
"\n",
"train_df, val_df = train_test_split(train_df, test_size=0.25, random_state=42)\n",
"\n",
"print(\"Размер обучающей выборки:\", len(train_df))\n",
"print(\"Размер контрольной выборки:\", len(val_df))\n",
"print(\"Размер тестовой выборки:\", len(test_df))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Видим недостаток баланса"
]
},
{
"cell_type": "code",
2024-10-18 20:20:52 +04:00
"execution_count": 44,
2024-10-18 19:14:48 +04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Распределение ВВП в обучающей выборке:\n",
"gdppercent\n",
" 0.02 30\n",
" 0.04 25\n",
" 0.03 21\n",
" 0.01 13\n",
" 0.07 8\n",
" 0.08 8\n",
" 0.05 7\n",
"-0.01 5\n",
" 0.11 2\n",
" 0.09 2\n",
"-0.02 2\n",
" 0.10 2\n",
"-0.03 1\n",
" 0.14 1\n",
"-0.10 1\n",
" 0.06 1\n",
"-0.05 1\n",
"-0.04 1\n",
"Name: count, dtype: int64\n",
"\n",
"Распределение ВВП в контрольной выборке:\n",
"gdppercent\n",
" 0.02 9\n",
" 0.03 7\n",
" 0.01 6\n",
" 0.07 4\n",
" 0.04 4\n",
" 0.05 4\n",
" 0.08 3\n",
" 0.06 3\n",
"-0.01 2\n",
" 0.10 1\n",
"-0.08 1\n",
"Name: count, dtype: int64\n",
"\n",
"Распределение ВВП в тестовой выборке:\n",
"gdppercent\n",
" 0.02 12\n",
" 0.03 8\n",
" 0.01 7\n",
" 0.05 5\n",
" 0.04 3\n",
" 0.08 3\n",
"-0.01 2\n",
"-0.05 1\n",
" 0.06 1\n",
" 0.13 1\n",
" 0.07 1\n",
"Name: count, dtype: int64\n",
"\n"
]
}
],
"source": [
"def check_balance(df, name):\n",
" counts = df['gdppercent'].value_counts()\n",
" print(f\"Распределение ВВП в {name}:\")\n",
" print(counts)\n",
" print()\n",
"\n",
"check_balance(train_df, \"обучающей выборке\")\n",
"check_balance(val_df, \"контрольной выборке\")\n",
"check_balance(test_df, \"тестовой выборке\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"также используем oversampling и undersampling"
]
},
{
"cell_type": "code",
2024-10-18 20:20:52 +04:00
"execution_count": 45,
2024-10-18 19:14:48 +04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Оверсэмплинг:\n",
"Распределение ВВП в обучающей выборке:\n",
"gdppercent\n",
2024-10-18 20:20:52 +04:00
" 0.04 36\n",
2024-10-18 19:14:48 +04:00
" 0.02 30\n",
" 0.03 21\n",
" 0.01 13\n",
2024-10-18 20:20:52 +04:00
" 0.07 10\n",
" 0.08 10\n",
2024-10-18 19:14:48 +04:00
" 0.05 7\n",
"-0.01 5\n",
2024-10-18 20:20:52 +04:00
" 0.09 4\n",
" 0.11 3\n",
" 0.14 2\n",
" 0.10 2\n",
2024-10-18 19:14:48 +04:00
"-0.02 2\n",
2024-10-18 20:20:52 +04:00
"-0.03 1\n",
"-0.10 1\n",
2024-10-18 19:14:48 +04:00
" 0.06 1\n",
2024-10-18 20:20:52 +04:00
"-0.05 1\n",
"-0.04 1\n",
2024-10-18 19:14:48 +04:00
"Name: count, dtype: int64\n",
"\n",
"Распределение ВВП в контрольной выборке:\n",
"gdppercent\n",
2024-10-18 20:20:52 +04:00
" 0.02 9\n",
" 0.07 7\n",
" 0.03 7\n",
" 0.01 6\n",
" 0.05 5\n",
" 0.06 5\n",
" 0.04 4\n",
" 0.08 3\n",
"-0.01 2\n",
" 0.10 1\n",
"-0.08 1\n",
2024-10-18 19:14:48 +04:00
"Name: count, dtype: int64\n",
"\n",
"Распределение ВВП в тестовой выборке:\n",
"gdppercent\n",
" 0.02 12\n",
" 0.03 8\n",
" 0.01 7\n",
" 0.05 5\n",
" 0.04 3\n",
2024-10-18 20:20:52 +04:00
" 0.08 3\n",
"-0.01 2\n",
"-0.05 1\n",
2024-10-18 19:14:48 +04:00
" 0.06 1\n",
2024-10-18 20:20:52 +04:00
" 0.13 1\n",
" 0.07 1\n",
2024-10-18 19:14:48 +04:00
"Name: count, dtype: int64\n",
"\n",
"Андерсэмплинг:\n",
"Распределение ВВП в обучающей выборке:\n",
"gdppercent\n",
2024-10-18 20:20:52 +04:00
" 0.02 25\n",
" 0.04 25\n",
" 0.03 17\n",
" 0.07 8\n",
" 0.08 8\n",
" 0.01 7\n",
" 0.05 7\n",
"-0.01 3\n",
" 0.11 2\n",
" 0.09 2\n",
" 0.10 2\n",
"-0.04 1\n",
"-0.10 1\n",
"-0.02 1\n",
"-0.03 1\n",
" 0.14 1\n",
" 0.06 1\n",
2024-10-18 19:14:48 +04:00
"Name: count, dtype: int64\n",
"\n",
"Распределение ВВП в контрольной выборке:\n",
"gdppercent\n",
2024-10-18 20:20:52 +04:00
" 0.02 7\n",
" 0.03 6\n",
" 0.01 4\n",
" 0.07 4\n",
" 0.04 4\n",
" 0.05 4\n",
" 0.08 3\n",
" 0.06 3\n",
2024-10-18 19:14:48 +04:00
"-0.08 1\n",
2024-10-18 20:20:52 +04:00
"-0.01 1\n",
" 0.10 1\n",
2024-10-18 19:14:48 +04:00
"Name: count, dtype: int64\n",
"\n",
"Распределение ВВП в тестовой выборке:\n",
"gdppercent\n",
2024-10-18 20:20:52 +04:00
" 0.02 12\n",
" 0.03 8\n",
" 0.01 7\n",
" 0.05 5\n",
" 0.08 3\n",
" 0.04 3\n",
"-0.01 2\n",
" 0.06 1\n",
"-0.05 1\n",
" 0.13 1\n",
" 0.07 1\n",
2024-10-18 19:14:48 +04:00
"Name: count, dtype: int64\n",
"\n"
]
}
],
"source": [
"from imblearn.over_sampling import RandomOverSampler\n",
"from imblearn.under_sampling import RandomUnderSampler\n",
"\n",
"def binning(target, bins):\n",
2024-10-18 20:20:52 +04:00
" return pd.qcut(target, q=bins, labels=False)\n",
2024-10-18 19:14:48 +04:00
"\n",
2024-10-18 20:20:52 +04:00
"train_df['gdppercent_binned'] = binning(train_df['gdppercent'], bins=2)\n",
"val_df['gdppercent_binned'] = binning(val_df['gdppercent'], bins=2)\n",
"test_df['gdppercent_binned'] = binning(test_df['gdppercent'], bins=2)\n",
2024-10-18 19:14:48 +04:00
"\n",
"def oversample(df, target_column):\n",
" X = df.drop(target_column, axis=1)\n",
" y = df[target_column]\n",
" \n",
" oversampler = RandomOverSampler(random_state=42)\n",
" x_resampled, y_resampled = oversampler.fit_resample(X, y) # type: ignore\n",
" \n",
" resampled_df = pd.concat([x_resampled, y_resampled], axis=1) \n",
" return resampled_df\n",
"\n",
"def undersample(df, target_column):\n",
" X = df.drop(target_column, axis=1)\n",
" y = df[target_column]\n",
" \n",
" undersampler = RandomUnderSampler(random_state=42)\n",
" x_resampled, y_resampled = undersampler.fit_resample(X, y) # type: ignore\n",
" \n",
" resampled_df = pd.concat([x_resampled, y_resampled], axis=1)\n",
" return resampled_df\n",
"\n",
"train_df_oversampled = oversample(train_df, 'gdppercent_binned')\n",
"val_df_oversampled = oversample(val_df, 'gdppercent_binned')\n",
"test_df_oversampled = oversample(test_df, 'gdppercent_binned')\n",
"\n",
"train_df_undersampled = undersample(train_df, 'gdppercent_binned')\n",
"val_df_undersampled = undersample(val_df, 'gdppercent_binned')\n",
"test_df_undersampled = undersample(test_df, 'gdppercent_binned')\n",
"\n",
"print(\"Оверсэмплинг:\")\n",
2024-10-18 20:20:52 +04:00
"check_balance(train_df_oversampled, \"обучающей выборке\")\n",
"check_balance(val_df_oversampled, \"контрольной выборке\")\n",
"check_balance(test_df_oversampled, \"тестовой выборке\")\n",
"\n",
"print(\"Андерсэмплинг:\")\n",
"check_balance(train_df_undersampled, \"обучающей выборке\")\n",
"check_balance(val_df_undersampled, \"контрольной выборке\")\n",
"check_balance(test_df_undersampled, \"тестовой выборке\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://www.kaggle.com/datasets/surajjha101/stores-area-and-sales-data - данный датасет содержит информацию о магазинах"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Index(['Store ID ', 'Store_Area', 'Items_Available', 'Daily_Customer_Count',\n",
" 'Store_Sales'],\n",
" dtype='object')\n"
]
}
],
"source": [
"df_stores = pd.read_csv(\".//static//scv//Stores.csv\")\n",
"print(df_stores.columns)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Столбцы на русском:\n",
"Store ID - Айди магазина\n",
"Store_Area - Доступное пространство магазина\n",
"Items_Available - Доступные предметы\n",
"Daily_Customer_Count - Ежедневное количество клиентов\n",
"Stores_Sales - Продажи в магазинах"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 896 entries, 0 to 895\n",
"Data columns (total 5 columns):\n",
" # Column Non-Null Count Dtype\n",
"--- ------ -------------- -----\n",
" 0 Store ID 896 non-null int64\n",
" 1 Store_Area 896 non-null int64\n",
" 2 Items_Available 896 non-null int64\n",
" 3 Daily_Customer_Count 896 non-null int64\n",
" 4 Store_Sales 896 non-null int64\n",
"dtypes: int64(5)\n",
"memory usage: 35.1 KB\n"
]
},
{
"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>Store ID</th>\n",
" <th>Store_Area</th>\n",
" <th>Items_Available</th>\n",
" <th>Daily_Customer_Count</th>\n",
" <th>Store_Sales</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>1659</td>\n",
" <td>1961</td>\n",
" <td>530</td>\n",
" <td>66490</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>1461</td>\n",
" <td>1752</td>\n",
" <td>210</td>\n",
" <td>39820</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>1340</td>\n",
" <td>1609</td>\n",
" <td>720</td>\n",
" <td>54010</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4</td>\n",
" <td>1451</td>\n",
" <td>1748</td>\n",
" <td>620</td>\n",
" <td>53730</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5</td>\n",
" <td>1770</td>\n",
" <td>2111</td>\n",
" <td>450</td>\n",
" <td>46620</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Store ID Store_Area Items_Available Daily_Customer_Count Store_Sales\n",
"0 1 1659 1961 530 66490\n",
"1 2 1461 1752 210 39820\n",
"2 3 1340 1609 720 54010\n",
"3 4 1451 1748 620 53730\n",
"4 5 1770 2111 450 46620"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_stores.info()\n",
"df_stores.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Объект наблюдения - экономика\n",
"Атрибуты - содержит набор информации о магазин, такие как:\n",
"Айди, размер, количество доступных предметов, количество покупателей и выручка"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA20AAAIjCAYAAACQ1/NiAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9d5xdZ3Xo/392Ob3Omd6bRr0XS3I37qbZgA2mYy6EJBByyY/cH3kRAiT3ciEkN7SEjmkmQGIM2MZIuBdZXVav03s5M6fXvff3j5FGHs/I6nNG0nrzAl5z9j5nr/Noyl7neZ61FMuyLIQQQgghhBBCzEpqoQMQQgghhBBCCHFqkrQJIYQQQgghxCwmSZsQQgghhBBCzGKStAkhhBBCCCHELCZJmxBCCCGEEELMYpK0CSGEEEIIIcQsJkmbEEIIIYQQQsxikrQJIYQQQgghxCwmSZsQQgghhBBCzGKStAkhhBBCCCHELCZJmxBCiFnrv/7rv1AUZdr/Ll68uNDhXbHi8Tj/8A//wB133EEoFEJRFB588MFChyWEEJctvdABCCGEEKfzd3/3dyxYsGDi6//9v/93AaMRw8PDfPGLX6Suro5ly5bxzDPPFDokIYS4rEnSJoQQYta79dZbufHGGye+/v73v8/w8HDhArrCVVZW0tfXR0VFBdu2bWPNmjWFDkkIIS5rsjxSCCHErJXNZgFQ1dP/uXrwwQdRFIX29vaJx0zTZOnSpVOW7+3evZsPfvCDNDU14XQ6qaio4IEHHmBkZGTSa37+85+fdmmmrp/8zPPGG29k8eLFbN++nauvvhqXy0VjYyPf/va3p7yXz33uc6xatYpAIIDH4+G6667j6aefnnRee3v7xHUeeeSRScfS6TRFRUUoisJXv/rVKXGWlZWRy+Um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lvPe97wXObQyEEEJcPJK0CSGEmLUOHTpEU1PTpMIfZ+LLX/4yqqry6U9/etrj9913H9/73vf42Mc+xsMPP8yGDRt44okngPFZsIvhZz/7GR/84Adpbm7mBz/4AU888QQbN27kDW94wymv+d73vpejR4/y8ssv8+Mf//iUCdirPfDAA/zoRz/i2Wefpb+/n/vuu2/KOWNjY9xwww288sorfPGLX+T3v/89Gzdu5Mtf/jJw8cZACCHEuZE9bUIIIWalTCbDrl27JhXiOBO9vb187Wtf40tf+hI+n29KRcjR0VGefPJJvvCFL/C5z31u4vEjR46cc6y9vb0kEolJs22HDx8GoKGhARhvFN7U1MTDDz88qdDJdMs3TyguLuYtb3nLxFLL++6777StDt7znvfw6U9/mk9+8pO84x3vwOfzTTnnmWeeYWRkhIcffpjrr79+4vG2trYzer9CCCFmlsy0CSGEmJUeeughMpkMN99881k97wtf+ALl5eV87GMfm/a4pmkAWJY16fF/+7d/O6c4AfL5PN/5zncmvs5ms3znO9+htLSUVatWnfK6mzdvZtOmTa/72g888AC7d+/m3nvvnVTB8lRCoRBvfetb2b1790TlyteaLpZsNsu///u/n/b1hRBCzDyZaRNCCDGrJBIJvvGNb/DFL34RTdOwLIuf/exnk84ZGBggHo/zs5/9jFtvvXXSvrUNGzbw85//fFKVxlfz+/1cf/31fOUrXyGXy1FdXc2GDRvOa5apqqqKL3/5y7S3tzN37lx++ctfsmvXLr773e9is9kAeNOb3sTDDz/MPffcwxvf+Eba2tr49re/zcKFC4nH46d87TvuuIOhoaEzSthOePDBB/nWt751yoqXV199NUVFRXzgAx/gr/7qr1AUhZ/+9KdTEtnX881vfpOxsTF6e3sB+P3vf093dzcAn/jEJwgEAmf8WkIIIV6fJG1CCCFmlaGhIT7zmc9MfP3qqoyv9b73vY+nn356UtK2fPly7r///te9xkMPPcQnPvEJvvWtb2FZFrfddht/+MMfqKqqOqeYi4qK+PGPf8wnPvEJvve971FeXs43v/lNPvKRj0yc88EPfpD+/n6+853v8Mc//pGFCxfys5/9jF//+tc888wzp3xtRVFet93AdFwu1+u2SSguLubRRx/lb/7mb/jsZz9LUVER733ve7n55pu5/fbbz+gaX/3qV+no6Jj4+uGHH+bhhx8GxvfiSdImhBAXjmKdzcdqQgghxEXW3t5OY2MjTz/9NDfeeON5n3ex3XjjjQwPD7N3796CxSCEEOLyJnvahBBCCCGEEGIWk6RNCCHErOL1ennPe94zbX+1czlPCCGEuNTJ8kghhBDiPMjySCGEEBebJG1CCCGEEEIIMYvJ8kghhBBCCCGEmMUkaRNCCCGEEEKIWUz6tM0g0zTp7e3F5/OhKEqhwxFCCCGEEEIUiGVZxGIxqqqqUNXXn0uTpG0G9fb2UltbW+gwhBBCCCGEELNEV1cXNTU1r3uOJG0zyOfzAeP/MH6/f0avncvl2LBhA7fddhs2m21Gr30lkXGeOTLWM0fGeubIWM8cGeuZI2M9c2SsZ86FGOtoNEptbe1EjvB6Cpq0Pffcc/zzP/8z27dvp6+vj9/85jfcfffdwPhAfPazn+Xxxx+ntbWVQCDALbfcwv/9v/+XqqqqidcIh8N84hOf4Pe//z2qqvL2t7+dr33ta3i93olzdu/ezV/+5V+ydetWSktL+cQnPsHf/u3fTorl17/+NX//939Pe3s7LS0tfPnLX+auu+6aOG5ZFv/wD//A9773PcbGxrjmmmv4j//4D1paWs74/Z5YEun3+wuStLndbvx+v/wQX0QyzjNHxnrmyFjPHBnrmSNjPXNkrGeOjPXMuZBjfSbbpgpaiCSRSLBs2TK+9a1vTTmWTCbZsWMHf//3f8+OHTt4+OGHOXToEG95y1smnfee97yHffv2sXHjRh599FGee+45PvrRj04cj0aj3HbbbdTX17N9+3b++Z//mc9//vN897vfnTj
"text/plain": [
"<Figure size 1000x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize=(10, 6))\n",
"\n",
"plt.scatter(df_stores['Daily_Customer_Count'], df_stores['Store_Sales'], c=df_stores['Daily_Customer_Count'], alpha=0.6)\n",
"\n",
"plt.title(\"Диаграмма 1\")\n",
"plt.ylabel(\"Доход магазина\")\n",
"plt.xlabel(\"Количество посетителей\")\n",
"plt.grid(visible='true')\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Присутствует связь между атрибутами, прибыль магазина зависит от количества товаров и количества покупателей. Для примера на графике приведена связь между прибылью и количеством покупателей. \n",
"Примеры бизнес целей\n",
"\n",
" 1.Прогнозирование уровня продаж на основе количества покупателей.\n",
" 2.Улучшение маркетинговых стратегий.\n",
" \n",
"Эффект для бизнеса: увеличение объема продаж и снижение затрат на хранение непродаваемых товаров, увеличение клиентской базы и повышение лояльности клиентов.\n",
"Цели технического проекта\n",
"\n",
"Для первой цели:\n",
"\n",
"Вход: площадь магазина\n",
"Целевой признак: сумма продаж.\n",
"\n",
"Для второй цели:\n",
"\n",
"Вход: количество клиентов за день\n",
"Целевой признак: сумма продаж"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Проверка на выбросы"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Пропущенные значения по столбцам:\n",
"Store ID 0\n",
"Store_Area 0\n",
"Items_Available 0\n",
"Daily_Customer_Count 0\n",
"Store_Sales 0\n",
"dtype: int64\n",
"\n",
"Статистический обзор данных:\n",
" Store ID Store_Area Items_Available Daily_Customer_Count \\\n",
"count 896.000000 896.000000 896.000000 896.000000 \n",
"mean 448.500000 1485.409598 1782.035714 786.350446 \n",
"std 258.797218 250.237011 299.872053 265.389281 \n",
"min 1.000000 775.000000 932.000000 10.000000 \n",
"25% 224.750000 1316.750000 1575.500000 600.000000 \n",
"50% 448.500000 1477.000000 1773.500000 780.000000 \n",
"75% 672.250000 1653.500000 1982.750000 970.000000 \n",
"max 896.000000 2229.000000 2667.000000 1560.000000 \n",
"\n",
" Store_Sales \n",
"count 896.000000 \n",
"mean 59351.305804 \n",
"std 17190.741895 \n",
"min 14920.000000 \n",
"25% 46530.000000 \n",
"50% 58605.000000 \n",
"75% 71872.500000 \n",
"max 116320.000000 \n"
]
}
],
"source": [
"null_values = df_stores.isnull().sum()\n",
"print(\"Пропущенные значения по столбцам:\")\n",
"print(null_values)\n",
"\n",
"stat_summary = df_stores.describe()\n",
"print(\"\\nС та тис тиче с кий обзор данных:\")\n",
"print(stat_summary)\n"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Коэффициент асимметрии для столбца 'Store ID ': 0.0\n",
"\n",
"Коэффициент асимметрии для столбца 'Store_Area': 0.030366862979232103\n",
"\n",
"Коэффициент асимметрии для столбца 'Items_Available': 0.03443939172681748\n",
"\n",
"Коэффициент асимметрии для столбца 'Daily_Customer_Count': 0.07463278790524851\n",
"\n",
"Коэффициент асимметрии для столбца 'Store_Sales': 0.14879364291055253\n",
"\n",
"Количество дубликатов: 0\n"
]
}
],
"source": [
"for column in df_stores.select_dtypes(include=[np.number]).columns:\n",
" skewness = df_stores[column].skew()\n",
" print(f\"\\nК о эффицие нт асимметрии для столбца '{column}': {skewness}\")\n",
"\n",
"duplicates = df_stores.duplicated().sum()\n",
"print(f\"\\nК о личе с тво дубликатов: {duplicates}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Н а основе данных выше можно сказать, что пустых значений и дубликатов нет, выбросы минимальны\n",
"Очистиим данные от шумов"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA20AAAIjCAYAAACQ1/NiAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOydeXQUVfbHv90hO2QjkA4ISdgJQSKoENkUgyAoyzg6ICoiAy7gIG6IgoDIIDoKODggqKAguIwLq4wg+EMwgA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7ghRdewMSJE9GyZUuX7yoqKmC329G4cWPFY3jKp59+Crvdjrvuusul7zabDW3btq3j7svybHz88cfo2LEjOnTo4HLMfv36AUCdY4pWIve5wkLTpk0B1FpLeZCbH7///jt27NiBBx54oM49YXHXlGPv3r246667cMcdd9Sx0n7//ff47bff8Mgjj7iMweDBg9GhQwds3LhR9rhTp05F165dceedd2ruG0EQ+kDukQRBBAynTp1CgwYNuJW2srIy/POf/8Tjjz+OhISEOt+XlJRg1qxZ+OCDD/Dbb7/V+S0vzZo1qxPYLypKBQUFDoXw3Xffxauvvopjx465xJ6lpKRIHnfMmDFYunQpnnzySaxYsQI33HAD2rZtK9uP9PR0pKWl4b333sNTTz2FFStW4Nlnn60TawTUuhFOmzYN27ZtQ3l5uct3rGPg3pfWrVvDarW6xMns2rULM2bMQFZWVh2lpKy
"text/plain": [
"<Figure size 1000x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Выбросы в датасете:\n",
" Store ID Store_Area Items_Available Daily_Customer_Count Store_Sales\n",
"39 40 1270 1516 10 45480\n",
"349 350 1886 2228 1530 40350\n",
"848 849 919 1099 1560 73810\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA2wAAAIjCAYAAAB/FZhcAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAD6pElEQVR4nOydeXQUVfbHvx3IDtkIZFFIwqKySQQXIpswQRAQcf2BG6MMbuAgboiCoqgIOiKKgqCjCIKOMy6ImBEEh8WACAYMAYUQokICZiGBhCyk6/dHqKaXWt6rpbu6+37O4RzS/brqVdWr9+59d7MJgiCAIAiCIAiCIAiCsBwhvu4AQRAEQRAEQRAEIQ0pbARBEARBEARBEBaFFDaCIAiCIAiCIAiLQgobQRAEQRAEQRCERS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Ny4cfjXv/6F+++/H2FhYbj77rsBANXV1XjnnXcwbtw4TJw4ESdPnsS7776LYcOG4YcffkBmZqbLMSMiIvDee+9hzJgxjs8+/fRTD4XImZMnT2LNmjW4/vrrHZ+99957iIiI8Pjd+++/j1atWuHhhx9Gq1atsGHDBjz99NOorq7Gyy+/LHsOZ2655Rakp6djzpw52LZtG15//XVUVlbigw8+cLl33bt3x+jRo9GyZUt8+eWXeOCBB2C32zFp0iSX/tx9993o3r07pk+fjri4OPz000/IycnBrbfeCqD5uY0aNQopKSmYMmUKkpOTsW/fPqxZswZTpkwBAOzduxf9+vXDeeedhyeeeALR0dH417/+hTFjxuA///mPy71xR268AEBiYiLmz58PAPjjjz+wYMECjBgxAr///jvi4uIMu6dqnDlzBk899ZTkd+PHj8f69evx4IMPolevXmjRogWWLFmCXbt2MR07MzMTjzzyiMtnH3zwAdatW+fy2d/+9jcsW7YMN910Ex555BFs374dc+bMwb59+/DZZ5852rE8U2fuueceDBgwAEDzWHc+FgDce++9eP/993HXXXfh73//O4qKirBw4UL89NNP2Lp1K0JDQ5muk5dp06ZJfv7ss89i1qxZuPLKK/Hcc88hLCwM27dvx4YNG3D11Vfjtddew6lTpwAA+/btw4svvuji3i2nSDc2NuLGG2/Eb7/9hq1btyIlJUW2bwcPHsTSpUt1XiFBEKYhEARBBDivvfaaAEDYvXu3y+eDBg0Sunfv7vLZe++9JwAQioqKBEEQhLq6OqFDhw7CNddcIwAQ3nvvPUfb2tpaj3OtWrVKACBs2rTJ8dkzzzwjABD+/PNP2T4OGjRIACD84x//cHxWX18vZGZmCu3atRMaGhoEQRCEM2fOCPX19S6/raysFJKSkoS7777b8VlRUZEAQBg3bpzQsmVLobS01PHdX/7yF+HWW28VAAgvv/yyRz/HjRsnjBo1yvF5cXGxEBISIowbN87jOqTuwb333itERUUJdXV1stfrfL7Ro0e7fP7AAw94PC+p8wwbNkzo2LGj4+8TJ04IrVu3Fq644grh9OnTLm3tdrsgCM33LyMjQ0hLSxMqKysl2whC8z3q2bOnyzXY7XbhyiuvFLp06eL4jGe8jB8/XkhLS3M555IlSwQAwg8//KB4rVL3VGr8CoIgvPzyyy59EgRBSEtLE8aPH+/4+6233hLCw8OFwYMHu/Tp9OnTQkhIiHDvvfe6HHP8+PFCdHS0x7ncSUtLE0aOHOnx+aRJkwRnkSMvL08AIPztb39zaffoo48KAIQNGzYIgsD2TEUOHDggABCWLVvm+EwcYyKbN28WAAgffvihy29zcnIkP3cnIyNDuPPOO10+27hxowBA2Lhxo+OzQYMGCYMGDXL8vXbtWgGAMHz4cJf+HDhwQAgJCRGuv/56oampSfH65M4lIr7z7733nmC324XbbrtNiIqKErZv3y7bTuSWW24RevToIbRv395lnBAEYQ3IJZIgiIBHdFFs27Yt92/ffPNNlJeX45lnnvH4LjIy0vH/uro6lJWVoW/fvgDAbI1wpmXLlrj33nsdf4eFheHee+/F8ePHsXPnTgBAixYtEBYWBqDZtauiogJnzpzBpZdeKnnO3r17o3v37li+fDkAoLi4GBs3blR0e7r77ruRk5OD0tJSAMCyZcuQlZWFCy64wKOt8z04efIkysrKMGDAANTW1mL//v1M1+1sIQOABx98EACwdu1ayfNUVVWhrKwMgwYNwqFDh1BVVQWg2XJ28uRJPPHEE4iIiHA5pmjt/Omnn1BUVISHHnrIYdFyb1NRUYENGzbglltucVxTWVkZysvLMWzYMBw4cABHjhyRvBal8QI0PzPxeHl5efjggw+QkpLikgyH5542NTU5jif+q62tlTy3SG1tLZ577jlMnjwZHTp0cPmupqYGdrsdbdq0UTyGXsRn+/DDD7t8LlrmvvrqKwBsz1SExZL+ySefIDY2FkOHDnW5Z3369EGrVq08XIvdadeuHf744w+GKzyHIAiYPn06brzxRlxxxRUu333++eew2+14+umnPSzuUq6TrDz22GP48MMP8a9//QuXX365YtudO3fik08+wZw5c5jcxgmC8D70ZhIEEfAUFxejZcuW3ApbVVUVXnzxRTz88MNISkry+L6iogJTpkxBUlISIiMj0bZtW2RkZDh+y0tqaiqio6NdPhOVJOd4pGXLluHiiy9GREQE2rRpg7Zt2+Krr76SPeddd92F9957D0Cze9mVV16JLl26yPYjMzMTPXr0wAcffABBEBzuY1Ls3bsX119/PWJjYxETE4O2bdvi9ttvB8B+D9z70qlTJ4SEhLhc89atW5GdnY3o6GjExcWhbdu2ePLJJ13OU1hYCACKpRpY2hw8eBCCIGDmzJlo27atyz9RETt+/LjH79TGCwD8/vvvjmNdcsklKCwsxH/+8x8Xtzaee7p//37ZPsrx6quvoq6uznH/nGnTpg26dOmCd955B9988w2OHz+OsrIyybgyPRQ
"text/plain": [
"<Figure size 1000x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize=(10, 6))\n",
"plt.scatter(df_stores['Daily_Customer_Count'], df_stores['Store_Sales'])\n",
"plt.xlabel('Количество клиентов за день')\n",
"plt.ylabel('Сумма прибыли')\n",
"plt.title('Диаграмма рассеивания перед чисткой')\n",
"plt.show()\n",
"\n",
"Q1 = df_stores[\"Daily_Customer_Count\"].quantile(0.25)\n",
"Q3 = df_stores[\"Daily_Customer_Count\"].quantile(0.75)\n",
"\n",
"IQR = Q3 - Q1\n",
"\n",
"threshold = 1.5 * IQR\n",
"lower_bound = Q1 - threshold\n",
"upper_bound = Q3 + threshold\n",
"\n",
"outliers = (df_stores[\"Daily_Customer_Count\"] < lower_bound) | (df_stores[\"Daily_Customer_Count\"] > upper_bound)\n",
"\n",
"# Вывод выбросов\n",
"print(\"Выбросы в датасете:\")\n",
"print(df_stores[outliers])\n",
"\n",
"# Заменяем выбросы на медианные значения\n",
"median_score = df_stores[\"Daily_Customer_Count\"].median()\n",
"df_stores.loc[outliers, \"Daily_Customer_Count\"] = median_score\n",
"\n",
"# Визуализация данных после обработки\n",
"plt.figure(figsize=(10, 6))\n",
"plt.scatter(df_stores['Daily_Customer_Count'], df_stores['Store_Sales'])\n",
"plt.xlabel('количество клиентов за день')\n",
"plt.ylabel('сумма прибыли')\n",
"plt.title('Диаграмма рассеивания после чистки')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Разбиение набора данных на обучающую, контрольную и тестовую выборки"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Размер обучающей выборки: 537\n",
"Размер контрольной выборки: 179\n",
"Размер тестовой выборки: 180\n"
]
}
],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"train_df, test_df = train_test_split(df_stores, test_size=0.2, random_state=42)\n",
"\n",
"train_df, val_df = train_test_split(train_df, test_size=0.25, random_state=42)\n",
"\n",
"print(\"Размер обучающей выборки:\", len(train_df))\n",
"print(\"Размер контрольной выборки:\", len(val_df))\n",
"print(\"Размер тестовой выборки:\", len(test_df))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Видим недостаток баланса"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Распределение суммы продаж в обучающей выборке:\n",
"Store_Sales\n",
"63540 3\n",
"69940 2\n",
"66050 2\n",
"41490 2\n",
"85670 2\n",
" ..\n",
"43720 1\n",
"17670 1\n",
"66490 1\n",
"58670 1\n",
"62470 1\n",
"Name: count, Length: 508, dtype: int64\n",
"\n",
"Распределение суммы продаж в контрольной выборке:\n",
"Store_Sales\n",
"54590 3\n",
"60060 2\n",
"74080 2\n",
"62380 2\n",
"39140 1\n",
" ..\n",
"67640 1\n",
"72350 1\n",
"21750 1\n",
"55170 1\n",
"69130 1\n",
"Name: count, Length: 174, dtype: int64\n",
"\n",
"Распределение суммы продаж в тестовой выборке:\n",
"Store_Sales\n",
"66000 2\n",
"59190 2\n",
"51480 2\n",
"76300 1\n",
"44890 1\n",
" ..\n",
"65970 1\n",
"70050 1\n",
"90180 1\n",
"71280 1\n",
"81470 1\n",
"Name: count, Length: 177, dtype: int64\n",
"\n"
]
}
],
"source": [
"def check_balance(df_stores, name):\n",
" counts = df_stores['Store_Sales'].value_counts()\n",
" print(f\"Распределение суммы продаж в {name}:\")\n",
" print(counts)\n",
" print()\n",
"\n",
"check_balance(train_df, \"обучающей выборке\")\n",
"check_balance(val_df, \"контрольной выборке\")\n",
"check_balance(test_df, \"тестовой выборке\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Применим андерсемплинг и оверсемплинг"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Оверсэмплинг:\n",
"Распределение суммы продаж в обучающей выборке:\n",
"Store_Sales\n",
"44660 3\n",
"70810 3\n",
"62470 3\n",
"72320 3\n",
"66050 3\n",
" ..\n",
"73710 3\n",
"67290 3\n",
"36290 3\n",
"49330 3\n",
"33010 3\n",
"Name: count, Length: 508, dtype: int64\n",
"\n",
"Распределение суммы продаж в контрольной выборке:\n",
"Store_Sales\n",
"41080 3\n",
"39140 3\n",
"54590 3\n",
"39820 3\n",
"54820 3\n",
" ..\n",
"67640 3\n",
"72350 3\n",
"21750 3\n",
"55170 3\n",
"69130 3\n",
"Name: count, Length: 174, dtype: int64\n",
"\n",
"Распределение суммы продаж в тестовой выборке:\n",
"Store_Sales\n",
"61080 2\n",
"76300 2\n",
"44890 2\n",
"36280 2\n",
"45480 2\n",
" ..\n",
"65970 2\n",
"70050 2\n",
"90180 2\n",
"71280 2\n",
"81470 2\n",
"Name: count, Length: 177, dtype: int64\n",
"\n",
"Андерсэмплинг:\n",
"Распределение суммы продаж в обучающей выборке:\n",
"Store_Sales\n",
"105150 1\n",
"16370 1\n",
"17670 1\n",
"21300 1\n",
"21470 1\n",
" ..\n",
"30610 1\n",
"30020 1\n",
"28330 1\n",
"27970 1\n",
"27840 1\n",
"Name: count, Length: 508, dtype: int64\n",
"\n",
"Распределение суммы продаж в контрольной выборке:\n",
"Store_Sales\n",
"20270 1\n",
"21750 1\n",
"23740 1\n",
"24410 1\n",
"25820 1\n",
" ..\n",
"87000 1\n",
"87330 1\n",
"89540 1\n",
"94690 1\n",
"102920 1\n",
"Name: count, Length: 174, dtype: int64\n",
"\n",
"Распределение суммы продаж в тестовой выборке:\n",
"Store_Sales\n",
"14920 1\n",
"22310 1\n",
"26770 1\n",
"32260 1\n",
"33730 1\n",
" ..\n",
"93530 1\n",
"93950 1\n",
"97260 1\n",
"102310 1\n",
"116320 1\n",
"Name: count, Length: 177, dtype: int64\n",
"\n"
]
}
],
"source": [
"def oversample(df, target_column):\n",
" X = df.drop(target_column, axis=1)\n",
" y = df[target_column]\n",
" \n",
" oversampler = RandomOverSampler(random_state=42)\n",
" x_resampled, y_resampled = oversampler.fit_resample(X, y) # type: ignore\n",
" \n",
" resampled_df = pd.concat([x_resampled, y_resampled], axis=1) \n",
" return resampled_df\n",
"\n",
"def undersample(df, target_column):\n",
" X = df.drop(target_column, axis=1)\n",
" y = df[target_column]\n",
" \n",
" undersampler = RandomUnderSampler(random_state=42)\n",
" x_resampled, y_resampled = undersampler.fit_resample(X, y) # type: ignore\n",
" \n",
" resampled_df = pd.concat([x_resampled, y_resampled], axis=1)\n",
" return resampled_df\n",
"\n",
"train_df_oversampled = oversample(train_df, 'Store_Sales')\n",
"val_df_oversampled = oversample(val_df, 'Store_Sales')\n",
"test_df_oversampled = oversample(test_df, 'Store_Sales')\n",
"\n",
"train_df_undersampled = undersample(train_df, 'Store_Sales')\n",
"val_df_undersampled = undersample(val_df, 'Store_Sales')\n",
"test_df_undersampled = undersample(test_df, 'Store_Sales')\n",
"\n",
"print(\"Оверсэмплинг:\")\n",
2024-10-18 19:14:48 +04:00
"check_balance(train_df_oversampled, \"обучающей выборке\")\n",
"check_balance(val_df_oversampled, \"контрольной выборке\")\n",
"check_balance(test_df_oversampled, \"тестовой выборке\")\n",
"\n",
"print(\"Андерсэмплинг:\")\n",
"check_balance(train_df_undersampled, \"обучающей выборке\")\n",
"check_balance(val_df_undersampled, \"контрольной выборке\")\n",
"check_balance(test_df_undersampled, \"тестовой выборке\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}