Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
861 changes: 861 additions & 0 deletions 8th hometask/1th_task.ipynb

Large diffs are not rendered by default.

259 changes: 259 additions & 0 deletions 8th hometask/2th_task.ipynb

Large diffs are not rendered by default.

190 changes: 190 additions & 0 deletions 8th hometask/3th_task.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a65a79c4",
"metadata": {},
"source": [
"#### *Задание 3"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f3e1c980",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"id": "427e6e7c",
"metadata": {},
"source": [
"Примените модель KMeans, построенную в предыдущем задании, к данным из тестового набора."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "dbdcad55",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.cluster import KMeans\n",
"import pickle"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9bad59c8",
"metadata": {},
"outputs": [],
"source": [
"with open('models/kmeans.pkl', 'rb') as file_kmeans:\n",
" kmeans = pickle.load(file = file_kmeans)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "59e842ce",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"sklearn.cluster._kmeans.KMeans"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(kmeans)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d9d2c30d",
"metadata": {},
"outputs": [],
"source": [
"x_test_scaled = pd.read_pickle('data/x_test_scaled.pkl')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a00946aa",
"metadata": {},
"outputs": [],
"source": [
"labels_test = kmeans.predict(x_test_scaled)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "17828b71",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2 63\n",
"1 41\n",
"0 23\n",
"dtype: int64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.value_counts(labels_test)"
]
},
{
"cell_type": "markdown",
"id": "00d51006",
"metadata": {},
"source": [
"Вычислите средние значения price и CRIM в разных кластерах на тестовых данных."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "7a9e1b0e",
"metadata": {},
"outputs": [],
"source": [
"x_test = pd.read_pickle('data/x_test.pkl')\n",
"y_test = pd.read_pickle('data/y_test.pkl')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "6cacb8f0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cluster #2:\tmean_price = 22.262,\tmean_CRIM = 0.275.\n",
"Cluster #1:\tmean_price = 16.034,\tmean_CRIM = 12.071.\n",
"Cluster #0:\tmean_price = 28.678,\tmean_CRIM = 0.068.\n"
]
}
],
"source": [
"for i in list(pd.value_counts(labels_test).index):\n",
" price = np.round(y_test.loc[labels_test == i, ['Price']].mean()[0], 3)\n",
" crim = np.round(x_test.loc[labels_test == i, ['CRIM']].mean()[0], 3)\n",
" print(f'Cluster #{i}:\\tmean_price = {price},\\tmean_CRIM = {crim}.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a3b82f0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file added 8th hometask/data/x_test.pkl
Binary file not shown.
Binary file added 8th hometask/data/x_test_scaled.pkl
Binary file not shown.
Binary file added 8th hometask/data/x_train.pkl
Binary file not shown.
Binary file added 8th hometask/data/x_train_scaled.pkl
Binary file not shown.
Binary file added 8th hometask/data/x_train_tsne.pkl
Binary file not shown.
Binary file added 8th hometask/data/y_test.pkl
Binary file not shown.
Binary file added 8th hometask/data/y_train.pkl
Binary file not shown.
Binary file added 8th hometask/models/kmeans.pkl
Binary file not shown.