# -*- coding: utf-8 -*-
"""3-1_생각해보기_코드.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1njatQjYPe-UsuMUncVaijvWQ6GtTHXhs

# Scikit-learn을 활용한 모델 구현

## 데이터 불러오기
"""

import pandas as pd
import numpy as np 

fifa2019 = pd.read_csv('fifa2019.csv')
df = pd.DataFrame.copy(fifa2019.sort_values(by = 'Overall', ascending = False ).head(200))
test_features=['Name','Stamina','Dribbling','ShortPassing','Penalties']
test_df= pd.DataFrame(df , columns = test_features)

#Stamina & Dribbling
Xy = np.array(test_df)
X = Xy[:,1:3]

"""# 학습하기"""

from sklearn.cluster import KMeans

model = KMeans(
    n_clusters=3, init='random',
    n_init=10, max_iter=300, 
    tol=1e-04, random_state=0
)
y_km = model.fit_predict(X)

"""# 군집화 결과 시각화하기"""

import matplotlib.pyplot as plt
# 1번 클러스터
plt.scatter(
    X[y_km == 0, 0], X[y_km == 0, 1],
    s=50, c='red',
    marker='o', edgecolor='black',
    label='cluster 1'
)

#2번 클러스터
plt.scatter(
    X[y_km == 1, 0], X[y_km == 1, 1],
    s=50, c='yellow',
    marker='x', edgecolor='black',
    label='cluster 2'
)

#3번 클러스터
plt.scatter(
    X[y_km == 2, 0], X[y_km == 2, 1],
    s=50, c='blue',
    marker='^', edgecolor='black',
    label='cluster 3'
)

# plot the centroids
plt.scatter(
    model.cluster_centers_[:, 0], model.cluster_centers_[:, 1],
    s=250, marker='*',
    c='black', edgecolor='black',
    label='centroids'
)
plt.legend(scatterpoints=1)
plt.grid()
plt.show()