<jupyter_start><jupyter_text>Задание 1<jupyter_code>import numpy as np
import pandas as pd
from sklearn.datasets import load_boston
from sklearn

boston = load_boston()

X = pd.DataFrame(data = boston['data'], columns = boston['feature_names'])
y = boston['target']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=42)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()

X_train_scaled = pd.DataFrame(scaler.fit_transform(X_train), columns=X_train.columns)
X_test_scaled = pd.DataFrame(scaler.transform(X_test), columns=X_test.columns)
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE

tsne = TSNE(n_components=2, learning_rate=250, random_state=42)
X_train_tsne = tsne.fit_transform(X_train_scaled)

plt.style.use('fivethirtyeight')

%config InlineBackend.figure_format = 'svg'
%matplotlib inline

plt.scatter(X_train_tsne[:, 0], X_train_tsne[:, 1])
plt.show()<jupyter_output><empty_output><jupyter_text>Задание 2<jupyter_code>from sklearn.cluster import KMeans

model = KMeans(n_clusters=3, random_state=100)
labels_train = model.fit_predict(X_train_tsne)

plt.scatter(X_train_tsne[:, 0], X_train_tsne[:, 1], c=labels_train)
plt.show()
print('Кластер 0 средняя цена: {}'.format(y_train[labels_train == 0].mean()))
print('Кластер 1 средняя цена: {}'.format(y_train[labels_train == 1].mean()))
print('Кластер 2 средняя цена: {}'.format(y_train[labels_train == 2].mean()))

print('Кластер 0 средий CRIM: {}'.format(X_train.loc[labels_train == 0, 'CRIM'].mean()))
print('Кластер 1 средий CRIM: {}'.format(X_train.loc[labels_train == 1, 'CRIM'].mean()))
print('Кластер 2 средий CRIM: {}'.format(X_train.loc[labels_train == 2, 'CRIM'].mean()))<jupyter_output>Кластер 0 средняя цена: 26.564705882352943
Кластер 1 средняя цена: 16.277227722772277
Кластер 2 средняя цена: 23.67065868263473
Кластер 0 средий CRIM: 0.09462279411764707
Кластер 1 средий CRIM: 13.17982178217822
Кластер 2 средий CRIM: 0.6829679640718562<jupyter_text>Задание 3<jupyter_code>X_test_tsne = tsne.fit_transform(X_train_scaled)
labels_test = model.fit_predict(X_test_tsne)

print('Кластер 0 средняя цена: {}'.format(y_test[labels_test == 0].mean()))
print('Кластер 1 средняя цена: {}'.format(y_test[labels_test == 1].mean()))
print('Кластер 2 средняя цена: {}'.format(y_test[labels_test == 2].mean()))

print('Кластер 0 средий CRIM: {}'.format(X_test.loc[labels_test == 0, 'CRIM'].mean()))
print('Кластер 1 средий CRIM: {}'.format(X_test.loc[labels_test == 1, 'CRIM'].mean()))
print('Кластер 2 средий CRIM: {}'.format(X_test.loc[labels_test == 2, 'CRIM'].mean()))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14933135215453194, 'fraction_numerical': 0.05646359583952452, 'mean_word_length': 7.917218543046357, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '501508', 'license': 'MIT', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1107, 'n_words': 200, 'path': 'python_data_science/ex8.ipynb', 'repo_name': 'synacker/geekbrains', 'size': '2692'}