# ---
# 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
# ---

# +
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb
from sklearn.cluster import KMeans
from sklearn.metrics import pairwise_distances_argmin_min
from mpl_toolkits.mplot3d import Axes3D
plt.rcParams['figure.figsize'] = (16, 9)
plt.style.use('ggplot')

dataframe = pd.read_csv(r"avocado.csv")
dataframe.head()

print(dataframe.groupby('year').size())

# -

dataframe.describe()

dataframe.drop(['year'],1).hist(color = "skyblue")
plt.show()

# +

sb.pairplot(dataframe.dropna(), hue='year',height=4,vars=["4046","4225","4770"],kind='scatter')
# -

X = np.array(dataframe[["4046","4225","4770"]])
y = np.array(dataframe['year'])
X.shape

# +

fig=plt.figure()
ax =Axes3D(fig)
colores=['gray','pink','skyblue','purple']
asignar=[]
for row in y:
   asignar.append(colores[row-2015])
ax.scatter(X[:, 0], X[:,1], X[:,2], c=asignar,s=70)

# +
Nc = range(1, 20)
kmeans = [KMeans(n_clusters=i) for i in Nc]
kmeans
score = [kmeans[i].fit(X).score(X) for i in range(len(kmeans))]

plt.plot(Nc,score)
plt.xlabel('Number of Clusters')
plt.ylabel('Score')
plt.title('Elbow Curve')
plt.show()
# -

kmeans = KMeans(n_clusters=3).fit(X)
centroids = kmeans.cluster_centers_
print(centroids)

labels= kmeans.predict(X)
C = kmeans.cluster_centers_
colores=['purple','skyblue','pink']
col=['indigo','navy','hotpink']
asignar=[]
for row in  labels:
    asignar.append(colores[row])
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=asignar,s=100)
ax.scatter(C[:, 0], C[:, 1], C[:, 2], marker='*', c=colores, s=100)

f1 = dataframe['4046'].values
f2 = dataframe['4225'].values
plt.scatter(f1, f2, c=asignar, s=70)
plt.scatter(C[:, 0], C[:, 1], marker='*', c=col, s=1000)
plt.show()

# +
f1 = dataframe['4046'].values
f2 = dataframe['4770'].values
 
plt.scatter(f1, f2, c=asignar, s=70)
plt.scatter(C[:, 0], C[:, 2], marker='*', c=col, s=1000)
plt.show()

# +

f1 = dataframe['4225'].values
f2 = dataframe['4770'].values
 
plt.scatter(f1, f2, c=asignar, s=70)
plt.scatter(C[:, 1], C[:, 2], marker='D', c=col, s=1000)
plt.show()
# -



copy =  pd.DataFrame()
copy['4046']=dataframe['4046'].values
copy['year']=dataframe['year'].values
copy['label'] = labels;
cantidadGrupo =  pd.DataFrame()
cantidadGrupo['color']=colores
cantidadGrupo['cantidad']=copy.groupby('4046').size()
cantidadGrupo

# +
group_referrer_index = copy['4046'] ==0
group_referrals = copy[group_referrer_index]
 
diversidadGrupo =  pd.DataFrame()
diversidadGrupo['year']=[0,1,2,3]
diversidadGrupo['4046']=group_referrals.groupby('4046').size()
diversidadGrupo
# -

closest, _ = pairwise_distances_argmin_min(kmeans.cluster_centers_, X)
closest

num=dataframe['4046'].values
for row in closest:
    print(num[row])



StarcoderdataJupyterScriptsDedupFiltered