AIM-PIbd-31-Kozyrev-S-S/lab_2/lab_2.ipynb

685 lines
141 KiB
Plaintext
Raw Permalink Normal View History

2024-10-07 21:06:58 +04:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Выбранные темы: цены на кофе, магазины, оценки студентов\n",
"Далее идут выбранные таблицы"
]
},
{
"cell_type": "code",
2024-10-07 21:07:16 +04:00
"execution_count": 64,
2024-10-07 21:06:58 +04:00
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.preprocessing import LabelEncoder\n",
"from imblearn.over_sampling import RandomOverSampler\n",
"from imblearn.under_sampling import RandomUnderSampler\n",
"\n",
"label_encoder = LabelEncoder()\n",
"\n",
"# Функция для применения oversampling\n",
"def apply_oversampling(X, y):\n",
" oversampler = RandomOverSampler(random_state=42)\n",
" X_resampled, y_resampled = oversampler.fit_resample(X, y)\n",
" return X_resampled, y_resampled\n",
"\n",
"# Функция для применения undersampling\n",
"def apply_undersampling(X, y):\n",
" undersampler = RandomUnderSampler(random_state=42)\n",
" X_resampled, y_resampled = undersampler.fit_resample(X, y)\n",
" return X_resampled, y_resampled\n",
"\n",
"def split_stratified_into_train_val_test(\n",
" df_input,\n",
" stratify_colname=\"y\",\n",
" frac_train=0.6,\n",
" frac_val=0.15,\n",
" frac_test=0.25,\n",
" random_state=None,\n",
"):\n",
" \"\"\"\n",
" Splits a Pandas dataframe into three subsets (train, val, and test)\n",
" following fractional ratios provided by the user, where each subset is\n",
" stratified by the values in a specific column (that is, each subset has\n",
" the same relative frequency of the values in the column). It performs this\n",
" splitting by running train_test_split() twice.\n",
"\n",
" Parameters\n",
" ----------\n",
" df_input : Pandas dataframe\n",
" Input dataframe to be split.\n",
" stratify_colname : str\n",
" The name of the column that will be used for stratification. Usually\n",
" this column would be for the label.\n",
" frac_train : float\n",
" frac_val : float\n",
" frac_test : float\n",
" The ratios with which the dataframe will be split into train, val, and\n",
" test data. The values should be expressed as float fractions and should\n",
" sum to 1.0.\n",
" random_state : int, None, or RandomStateInstance\n",
" Value to be passed to train_test_split().\n",
"\n",
" Returns\n",
" -------\n",
" df_train, df_val, df_test :\n",
" Dataframes containing the three splits.\n",
" \"\"\"\n",
"\n",
" if frac_train + frac_val + frac_test != 1.0:\n",
" raise ValueError(\n",
" \"fractions %f, %f, %f do not add up to 1.0\"\n",
" % (frac_train, frac_val, frac_test)\n",
" )\n",
"\n",
" if stratify_colname not in df_input.columns:\n",
" raise ValueError(\"%s is not a column in the dataframe\" % (stratify_colname))\n",
"\n",
" X = df_input # Contains all columns.\n",
" y = df_input[\n",
" [stratify_colname]\n",
" ] # Dataframe of just the column on which to stratify.\n",
"\n",
" # Split original dataframe into train and temp dataframes.\n",
" df_train, df_temp, y_train, y_temp = train_test_split(\n",
" X, y, stratify=y, test_size=(1.0 - frac_train), random_state=random_state\n",
" )\n",
"\n",
" # Split the temp dataframe into val and test dataframes.\n",
" relative_frac_test = frac_test / (frac_val + frac_test)\n",
" df_val, df_test, y_val, y_test = train_test_split(\n",
" df_temp,\n",
" y_temp,\n",
" stratify=y_temp,\n",
" test_size=relative_frac_test,\n",
" random_state=random_state,\n",
" )\n",
"\n",
" assert len(df_input) == len(df_train) + len(df_val) + len(df_test)\n",
"\n",
" return df_train, df_val, df_test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Отслеживание цен на акции Старбакс. Объекты связаны между собой датой, т.е. каждая следующая строка это новый день. Можно узнать как, относительно изменения цен на акции, идут продажи акций. Поможет для трейдинговых компаний. Целевым признаком является количество покупающих."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 8036 entries, 0 to 8035\n",
"Data columns (total 7 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 Date 8036 non-null object \n",
" 1 Open 8036 non-null float64\n",
" 2 High 8036 non-null float64\n",
" 3 Low 8036 non-null float64\n",
" 4 Close 8036 non-null float64\n",
" 5 Adj Close 8036 non-null float64\n",
" 6 Volume 8036 non-null int64 \n",
"dtypes: float64(5), int64(1), object(1)\n",
"memory usage: 439.6+ KB\n",
"Date 0\n",
"Open 0\n",
"High 0\n",
"Low 0\n",
"Close 0\n",
"Adj Close 0\n",
"Volume 0\n",
"dtype: int64\n",
" Open High Low Close Adj Close \\\n",
"count 8036.000000 8036.000000 8036.000000 8036.000000 8036.000000 \n",
"mean 30.054280 30.351487 29.751322 30.058857 26.674025 \n",
"std 33.615577 33.906613 33.314569 33.615911 31.728090 \n",
"min 0.328125 0.347656 0.320313 0.335938 0.260703 \n",
"25% 4.392031 4.531250 4.304922 4.399610 3.414300 \n",
"50% 13.325000 13.493750 13.150000 13.330000 10.352452 \n",
"75% 55.250000 55.722501 54.852499 55.267499 47.464829 \n",
"max 126.080002 126.320000 124.809998 126.059998 118.010414 \n",
"\n",
" Volume \n",
"count 8.036000e+03 \n",
"mean 1.470459e+07 \n",
"std 1.340021e+07 \n",
"min 1.504000e+06 \n",
"25% 7.817750e+06 \n",
"50% 1.169815e+07 \n",
"75% 1.778795e+07 \n",
"max 5.855088e+08 \n",
"\n",
"[1]\n",
"\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjkAAAGdCAYAAADwjmIIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAABMFUlEQVR4nO3deXhTVcIG8Dfd0r2lhaYttFCQVbayWjYFKgiIovgpDg6OC6gDKjIzCiMwyiBVnFEGRXEblFHcBQUUZVGQxUIpIPva0tLSllK6kzZN8v3R5jY3uWmS9iZpkvf3PH3m5tyTm9POjHk9q0Kv1+tBRERE5GF8XN0AIiIiIkdgyCEiIiKP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hBZbl3Ybn1xuSmooq3O7EFEvkcbWDXncFEMOERGRTKT6VfonRNr8fnvPrmpqVEsqAL2764LoPbUMOURERGSLuhburtdwCLnNc3JMw1BK52jh2lIvj/E72JNDRERENulu4/ENlghzcmwMOab1+nSIEK4trbwSD1dx4rHIrl27MHnyZMTHx0OhUGDDhg2i+3q9HosXL0ZcXByCgoKQmpqKs2fPiuqUlJRg+vTpCA8PR2RkJB555BFUVla26BchIiJytdReqha938fHvrOrTENOZU2d0T3z+qk9VSarq9iTI1JVVYV+/fph1apVkveXL1+OlStXYvXq1UhPT0dISAjGjx8PtVot1Jk+fTqOHz+OrVu3YtOmTdi1axdmzZrV/N+CiIioFbB1Lo0lvnb25JhmlN8vlQrXJVW1ZvVV4UoYD1jZurOyu7L7WIcJEyZ
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df1 = pd.read_csv(\"../data/coffee.csv\")\n",
"df1.info()\n",
"print(df1.isnull().sum())\n",
"print(df1.describe())\n",
"print()\n",
"print(df1[\"Date\"].value_counts().unique())\n",
"print()\n",
"plt.plot(df1[\"Date\"], df1[\"High\"])\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Данные по всем параметрам являются правильными, без шумов, без выбросов, актуальными."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Магазины. Каждая строка представляет собой магазин, его площадь, количество продуктов, количество покупателей и объем продаж. Позволяет увидеть изменения количества продаж относительно размеров магазина и количества покупателей. Ключевой признак - количество продаж"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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",
"Store ID 0\n",
"Store_Area 0\n",
"Items_Available 0\n",
"Daily_Customer_Count 0\n",
"Store_Sales 0\n",
"dtype: int64\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",
"\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkUAAAGdCAYAAAAc+wceAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAACnkklEQVR4nO2deXxU1d3/PzPZE8gkIZJJFEiEIISgbAIxQAsGQVDc+rQsUoo8UJFYQKtIK4qlCmp/BRUUpQpWBapPRdlMmwCKYFgMRghB1gAWMqGQDRKykLm/P4Y7znKXc7dZku/79eKlmTlz77nnnHvvOZ/zXUwcx3EgCIIgCIJo45j9XQGCIAiCIIhAgCZFBEEQBEEQoEkRQRAEQRAEAJoUEQRBEARBAKBJEUEQBEEQBACaFBEEQRAEQQCgSRFBEARBEAQAmhQRBEEQBEEAAEL9XQGjsNvtOH/+PNq3bw+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V2ux2TJ0/GU089hV69enl9X1hYiLi4OOeECABycnJgNpuxd+9eZ5lhw4YhPDzcWWbUqFE4evQoqqqqBM/b2NiI2tpat38EoRR/uEr74py+UqOMpDUoXa1N5SKI1obu3mcvv/wyQkND8bvf/U7we5vNho4dO7pXIjQUCQkJsNlszjJpaWluZZKSkpzfxcfHex138eLFeOGFF/S4BKINI5esk7fv0dNV2hfnDPZgga1B6SKViyACH12VoqKiIrz22mtYs2YNTCbfPpjmz5+Pmpoa578ff/zRp+cnWgdSwRqNcpX2xTmDPVhgsCt
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df2 = pd.read_csv(\"../data/store.csv\")\n",
"df2.info()\n",
"print(df2.isnull().sum())\n",
"print(df2.describe())\n",
"print()\n",
"\n",
"\n",
"\n",
"plt.scatter(df2[\"Store_Sales\"], df2[\"Daily_Customer_Count\"])\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Данные имеют некоторое количество выбросов, что видно на графике."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Оценки студентов. Показывает оценки конкретного студента. Аналитика относительно гендера, расы, уровня образования родителей. Поможет для онлайн-школ для опредения контенгента покупателей курсов. Ключевыми значениями являются оценки по предметам."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 1000 entries, 0 to 999\n",
"Data columns (total 8 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 gender 1000 non-null object\n",
" 1 race/ethnicity 1000 non-null object\n",
" 2 parental level of education 1000 non-null object\n",
" 3 lunch 1000 non-null object\n",
" 4 test preparation course 1000 non-null object\n",
" 5 math score 1000 non-null int64 \n",
" 6 reading score 1000 non-null int64 \n",
" 7 writing score 1000 non-null int64 \n",
"dtypes: int64(3), object(5)\n",
"memory usage: 62.6+ KB\n",
" gender race/ethnicity parental level of education lunch \\\n",
"0 female group B bachelor's degree standard \n",
"1 female group C some college standard \n",
"2 female group B master's degree standard \n",
"3 male group A associate's degree free/reduced \n",
"4 male group C some college standard \n",
"\n",
" test preparation course math score reading score writing score score \n",
"0 none 72 72 74 72.666667 \n",
"1 completed 69 90 88 82.333333 \n",
"2 none 90 95 93 92.666667 \n",
"3 none 47 57 44 49.333333 \n",
"4 none 76 78 75 76.333333 \n",
"gender 0\n",
"race/ethnicity 0\n",
"parental level of education 0\n",
"lunch 0\n",
"test preparation course 0\n",
"math score 0\n",
"reading score 0\n",
"writing score 0\n",
"score 0\n",
"dtype: int64\n",
" math score reading score writing score score\n",
"count 1000.00000 1000.000000 1000.000000 1000.000000\n",
"mean 66.08900 69.169000 68.054000 67.770667\n",
"std 15.16308 14.600192 15.195657 14.257326\n",
"min 0.00000 17.000000 10.000000 9.000000\n",
"25% 57.00000 59.000000 57.750000 58.333333\n",
"50% 66.00000 70.000000 69.000000 68.333333\n",
"75% 77.00000 79.000000 79.000000 77.666667\n",
"max 100.00000 100.000000 100.000000 100.000000\n",
"\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAo8AAAGdCAYAAABzZpl2AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAABn4UlEQVR4nO3deXQUVdrH8W93tk5nT4AkQAhBMRABMQKKKOgABp3BBUdnFBVUUGdEYBQVXIAIGFFgFB1B4wyL4r7wogPIIgyIyCqbiQEhENTEICH7nu73j0x6aLJVoLMAv885fQ7punXvc+tW3X6orq4y2e12OyIiIiIiBpibOwAREREROXsoeRQRERERw5Q8ioiIiIhhSh5FRERExDAljyIiIiJimJJHERERETFMyaOIiIiIGKbkUUREREQMc2/uAOTcYrPZ+OWXX/Dz88NkMjV3O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7Jdu3bRvn17R+JYE6vV6kgcT+2DkW2enJzMJZdc4kgcq5bbbLY6v26vMmnSJHJychyvo0eP1ruOUW38qifbZ1LufBcZbHVJOVds7/rqOJ2x134gcn6KDLYant8a6kzmlcacx5p7vnNp8ujm5sbq1atZsWIFMTExvPrqq0RHR5Oaavw6BAAPDw/Hv00mU43v2WyV12VVXZuXmJjIrl27HK99+/bx7bffGm7n1HrPRE3x11ZvbGwsqampTJs2jaKiIm6//Xb++Mc/AuDt7d2gtqraa8rfQHl5eeHv7+/0cpU+UcGEB1io7cYEJip/fVZ
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df3 = pd.read_csv(\"../data/student.csv\")\n",
"df3.info()\n",
"df3[\"score\"] = (df3[\"math score\"] + df3[\"reading score\"] + df3[\"writing score\"]) / 3\n",
"print(df3.head())\n",
"print(df3.isnull().sum())\n",
"print(df3.describe())\n",
"print()\n",
"plt.scatter(df3[\"score\"], df3[\"parental level of education\"])\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Для всех выбранных тем отсутствуют пустые ячейки. Заполнение пустых ячеек не требуется. Данные вполне реальные."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Разбиение наборов на выборки."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Акции старбакс."
]
},
{
"cell_type": "code",
2024-10-07 21:07:16 +04:00
"execution_count": 69,
2024-10-07 21:06:58 +04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Обучающая выборка: (4821, 4)\n",
"Volume_Grouped\n",
" 0 2802\n",
" 1 1460\n",
" 2 369\n",
" 3 111\n",
" 4 40\n",
" 5 18\n",
"-1 10\n",
" 6 7\n",
" 7 4\n",
2024-10-07 21:07:16 +04:00
"Name: count, dtype: int64\n",
"Обучающая выборка после oversampling: (25218, 4)\n",
"Volume_Grouped\n",
" 0 2802\n",
" 4 2802\n",
" 1 2802\n",
" 2 2802\n",
" 3 2802\n",
" 5 2802\n",
"-1 2802\n",
" 7 2802\n",
" 6 2802\n",
"Name: count, dtype: int64\n",
"Контрольная выборка: (1607, 4)\n",
"Volume_Grouped\n",
" 0 934\n",
" 1 487\n",
" 2 123\n",
" 3 37\n",
" 4 13\n",
" 5 6\n",
"-1 4\n",
" 6 2\n",
" 7 1\n",
"Name: count, dtype: int64\n",
"Тестовая выборка: (1608, 4)\n",
"Volume_Grouped\n",
" 0 934\n",
" 1 487\n",
" 2 124\n",
" 3 37\n",
" 4 14\n",
" 5 6\n",
"-1 3\n",
" 6 2\n",
" 7 1\n",
2024-10-07 21:06:58 +04:00
"Name: count, dtype: int64\n"
]
}
],
"source": [
"data = df1[[\"Volume\", \"High\", \"Low\"]].copy()\n",
"data[\"Volume_Grouped\"] = pd.cut(data[\"Volume\"], bins=50, labels=False)\n",
"\n",
"interval_counts = data[\"Volume_Grouped\"].value_counts().sort_index()\n",
"\n",
"min_samples_per_interval = 5\n",
"for interval, count in interval_counts.items():\n",
" if count < min_samples_per_interval:\n",
" data.loc[data[\"Volume_Grouped\"] == interval, \"Volume_Grouped\"] = -1\n",
"\n",
"\n",
"df_coffee_train, df_coffee_val, df_coffee_test = split_stratified_into_train_val_test(\n",
" data, stratify_colname=\"Volume_Grouped\", frac_train=0.60, frac_val=0.20, frac_test=0.20)\n",
"\n",
"print(\"Обучающая выборка: \", df_coffee_train.shape)\n",
"print(df_coffee_train[\"Volume_Grouped\"].value_counts())\n",
"\n",
2024-10-07 21:07:16 +04:00
"X_resampled, y_resampled = apply_oversampling(df_coffee_train, df_coffee_train[\"Volume_Grouped\"])\n",
2024-10-07 21:06:58 +04:00
"df_coffee_train_adasyn = pd.DataFrame(X_resampled)\n",
"\n",
"print(\"Обучающая выборка после oversampling: \", df_coffee_train_adasyn.shape)\n",
2024-10-07 21:07:16 +04:00
"print(df_coffee_train_adasyn[\"Volume_Grouped\"].value_counts())\n",
2024-10-07 21:06:58 +04:00
"\n",
"print(\"Контрольная выборка: \", df_coffee_val.shape)\n",
"print(df_coffee_val[\"Volume_Grouped\"].value_counts())\n",
"\n",
"print(\"Тестовая выборка: \", df_coffee_test.shape)\n",
"print(df_coffee_test[\"Volume_Grouped\"].value_counts())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Магазины"
]
},
{
"cell_type": "code",
2024-10-07 21:07:16 +04:00
"execution_count": 68,
2024-10-07 21:06:58 +04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Обучающая выборка: (537, 4)\n",
"Sales_Grouped\n",
" 2 184\n",
" 3 148\n",
" 1 135\n",
" 4 45\n",
" 0 20\n",
"-1 5\n",
"Name: count, dtype: int64\n",
2024-10-07 21:07:16 +04:00
"Обучающая выборка после oversampling: (1104, 4)\n",
"Sales_Grouped\n",
" 3 184\n",
" 1 184\n",
" 2 184\n",
" 0 184\n",
"-1 184\n",
" 4 184\n",
"Name: count, dtype: int64\n",
2024-10-07 21:06:58 +04:00
"Контрольная выборка: (179, 4)\n",
"Sales_Grouped\n",
" 2 61\n",
" 3 49\n",
" 1 45\n",
" 4 15\n",
" 0 7\n",
"-1 2\n",
"Name: count, dtype: int64\n",
"Тестовая выборка: (180, 4)\n",
"Sales_Grouped\n",
" 2 61\n",
" 3 50\n",
" 1 45\n",
" 4 15\n",
" 0 7\n",
"-1 2\n",
"Name: count, dtype: int64\n"
]
}
],
"source": [
"data = df2[[\"Store_Sales\", \"Store_Area\", \"Daily_Customer_Count\"]].copy()\n",
"data[\"Sales_Grouped\"] = pd.cut(data[\"Store_Sales\"], bins=6, labels=False)\n",
"\n",
"interval_counts = data[\"Sales_Grouped\"].value_counts().sort_index()\n",
"\n",
"min_samples_per_interval = 10\n",
"for interval, count in interval_counts.items():\n",
" if count < min_samples_per_interval:\n",
" data.loc[data[\"Sales_Grouped\"] == interval, \"Sales_Grouped\"] = -1\n",
"\n",
"df_shop_train, df_shop_val, df_shop_test = split_stratified_into_train_val_test(\n",
" data, stratify_colname=\"Sales_Grouped\", frac_train=0.60, frac_val=0.20, frac_test=0.20)\n",
"\n",
"\n",
"print(\"Обучающая выборка: \", df_shop_train.shape)\n",
"print(df_shop_train[\"Sales_Grouped\"].value_counts())\n",
"\n",
2024-10-07 21:07:16 +04:00
"X_resampled, y_resampled = apply_oversampling(df_shop_train, df_shop_train[\"Sales_Grouped\"])\n",
"df_shop_train_adasyn = pd.DataFrame(X_resampled)\n",
2024-10-07 21:06:58 +04:00
"\n",
2024-10-07 21:07:16 +04:00
"print(\"Обучающая выборка после oversampling: \", df_shop_train_adasyn.shape)\n",
"print(df_shop_train_adasyn[\"Sales_Grouped\"].value_counts())\n",
2024-10-07 21:06:58 +04:00
"\n",
"print(\"Контрольная выборка: \", df_shop_val.shape)\n",
"print(df_shop_val[\"Sales_Grouped\"].value_counts())\n",
"\n",
"print(\"Тестовая выборка: \", df_shop_test.shape)\n",
"print(df_shop_test[\"Sales_Grouped\"].value_counts())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Оценки студентов"
]
},
{
"cell_type": "code",
2024-10-07 21:07:16 +04:00
"execution_count": 67,
2024-10-07 21:06:58 +04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Обучающая выборка: (600, 4)\n",
"score_grouped\n",
" 3 283\n",
" 2 181\n",
" 4 101\n",
" 1 31\n",
"-1 4\n",
2024-10-07 21:07:16 +04:00
"Name: count, dtype: int64\n",
"Обучающая выборка после oversampling: (1415, 4)\n",
"score_grouped\n",
" 2 283\n",
" 4 283\n",
" 3 283\n",
" 1 283\n",
"-1 283\n",
"Name: count, dtype: int64\n",
"Контрольная выборка: (200, 4)\n",
"score_grouped\n",
" 3 95\n",
" 2 61\n",
" 4 33\n",
" 1 10\n",
"-1 1\n",
"Name: count, dtype: int64\n",
"Тестовая выборка: (200, 4)\n",
"score_grouped\n",
" 3 94\n",
" 2 60\n",
" 4 34\n",
" 1 11\n",
"-1 1\n",
2024-10-07 21:06:58 +04:00
"Name: count, dtype: int64\n"
]
}
],
"source": [
"data = df3[[\"score\", \"gender\", \"race/ethnicity\"]].copy()\n",
"data[\"score_grouped\"] = pd.cut(data[\"score\"], bins=5, labels=False)\n",
"\n",
"data[\"gender\"] = label_encoder.fit_transform(data['gender'])\n",
"data[\"race/ethnicity\"] = label_encoder.fit_transform(data['race/ethnicity'])\n",
"\n",
"interval_counts = data[\"score_grouped\"].value_counts().sort_index()\n",
"\n",
"min_samples_per_interval = 10\n",
"for interval, count in interval_counts.items():\n",
" if count < min_samples_per_interval:\n",
" data.loc[data[\"score_grouped\"] == interval, \"score_grouped\"] = -1\n",
"\n",
"df_mark_train, df_mark_val, df_mark_test = split_stratified_into_train_val_test(\n",
" data, stratify_colname=\"score_grouped\", frac_train=0.60, frac_val=0.20, frac_test=0.20)\n",
"\n",
"\n",
"\n",
"\n",
"print(\"Обучающая выборка: \", df_mark_train.shape)\n",
"print(df_mark_train[\"score_grouped\"].value_counts())\n",
"\n",
2024-10-07 21:07:16 +04:00
"X_resampled, y_resampled = apply_oversampling(df_mark_train, df_mark_train[\"score_grouped\"])\n",
2024-10-07 21:06:58 +04:00
"df_mark_train_adasyn = pd.DataFrame(X_resampled)\n",
"\n",
"print(\"Обучающая выборка после oversampling: \", df_mark_train_adasyn.shape)\n",
2024-10-07 21:07:16 +04:00
"print(df_mark_train_adasyn[\"score_grouped\"].value_counts())\n",
2024-10-07 21:06:58 +04:00
"\n",
"print(\"Контрольная выборка: \", df_mark_val.shape)\n",
"print(df_mark_val[\"score_grouped\"].value_counts())\n",
"\n",
"print(\"Тестовая выборка: \", df_mark_test.shape)\n",
"print(df_mark_test[\"score_grouped\"].value_counts())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "aimvenv",
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}