# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans 
from sklearn.datasets.samples_generator import make_blobs
from sklearn import model_selection
import pandas as pd

X, y = make_blobs(n_samples=2000, cluster_std=1.5, n_features=3, centers=5,  
                            random_state=19850725)

# +
fig = plt.figure(figsize=(15,10))
ax = fig.add_subplot(111, projection='3d')

ax.scatter(X[:,0], X[:,1], X[:,2], c=y, marker='o')

plt.show()

# +
splitter =  model_selection.StratifiedShuffleSplit(n_splits=1, test_size=0.25)
splits = list(splitter.split(X=X,y=y))
train_index = splits[0][0]
test_index = splits[0][1]

train_df = pd.DataFrame(X[train_index])
train_df['cluster'] = y[train_index]
print(len(train_df))


test_df = pd.DataFrame(X[test_index])
test_df['cluster'] = y[test_index]
print(len(test_df))

# +
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(121, projection='3d')

ax.scatter(train_df.iloc[:,0], train_df.iloc[:,1], train_df.iloc[:,2], c=train_df['cluster'], marker='o')

ax = fig.add_subplot(122, projection='3d')

ax.scatter(test_df.iloc[:,0], test_df.iloc[:,1], test_df.iloc[:,2], c=test_df['cluster'], marker='o')

plt.show()
# -

train_df.to_csv(path_or_buf="data/train-data.csv", header=False, index=True)
test_df.to_csv(path_or_buf="data/test-data.csv", header=False, index=True)

k_means = KMeans(init='k-means++', n_clusters=3, n_init=100)
k_means.fit(train_df.iloc[:,[0,1,2]])

training_assigments = k_means.predict(train_df.iloc[:,[0,1,2]])
test_assigments = k_means.predict(test_df.iloc[:,[0,1,2]])
print(training_assigments[0:100])

# +
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(121, projection='3d')

ax.scatter(train_df.iloc[:,0], train_df.iloc[:,1], train_df.iloc[:,2], c=training_assigments, marker='o')

ax = fig.add_subplot(122, projection='3d')

ax.scatter(test_df.iloc[:,0], test_df.iloc[:,1], test_df.iloc[:,2], c=test_assigments, marker='o')

plt.show()

# +
fig = plt.figure(figsize=(15,10))

ax = fig.add_subplot(321)
ax.scatter(train_df.iloc[:,0], train_df.iloc[:,1], c=training_assigments, marker='o')

ax = fig.add_subplot(322)
ax.scatter(test_df.iloc[:,0], test_df.iloc[:,1], c=test_assigments, marker='o')

ax = fig.add_subplot(323)
ax.scatter(train_df.iloc[:,0], train_df.iloc[:,2], c=training_assigments, marker='o')

ax = fig.add_subplot(324)
ax.scatter(test_df.iloc[:,0], test_df.iloc[:,2], c=test_assigments, marker='o')

ax = fig.add_subplot(325)
ax.scatter(train_df.iloc[:,2], train_df.iloc[:,3], c=training_assigments, marker='o')

ax = fig.add_subplot(326)
ax.scatter(test_df.iloc[:,2], test_df.iloc[:,3], c=test_assigments, marker='o')

plt.show()
# -



StarcoderdataJupyterScriptsDedupFiltered