<jupyter_start><jupyter_text>Page contain : Unsupervised Learning 1 . KMean without PCA 2 . KMean with PCA 3 . KMean with Kernel PCA<jupyter_code>import matplotlib.pyplot as plt  
import pandas as pd  
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import warnings
warnings.filterwarnings("ignore")
plt.rcParams['font.size']=14
pd.set_option('display.max_columns', 200)
from IPython.display import Image
fifa_data = pd.read_csv('fifa_18.csv')  
fifa_data.shape
fifa_data.head()
def types(df,col):
    return df[col]
32+48
fifa_data.info()
df = fifa_data.copy()
dx =df[df.select_dtypes(include=['float64','int64']).columns]
dx<jupyter_output><empty_output><jupyter_text>1 . KMean without PCA<jupyter_code>data = fifa_data.loc[:, ['ball_control', 'sprint_speed']] 
data.head()<jupyter_output><empty_output><jupyter_text>In Stage one taking only two features at later stage, will take all features and  perform different types of pca.<jupyter_code>#standardScaler
ss = StandardScaler()
data.loc[:] = ss.fit_transform(data)
data.head()<jupyter_output><empty_output><jupyter_text>Kmean<jupyter_code>model = KMeans(random_state=1,n_clusters=4)
model.fit(data)
inertia= []
clusters= np.arange(1, 10)

for x in clusters:
    model=KMeans(x, random_state=1)
    model.fit(data)
    inertia.append(model.inertia_)
    
plt.figure(figsize=(13,5))
plt.grid(True)
plt.plot(clusters, inertia, '-o')
plt.xticks(clusters)
plt.title('Elbow plot to choose Clusters')
plt.ylabel('Inertia')
plt.xlabel('Clusters')
plt.show()<jupyter_output><empty_output><jupyter_text>From elbo plot it is clear that 3 clusters are best to choose as per Interia<jupyter_code>model_new = KMeans(random_state=1,n_clusters=3)
model_new.fit(data)
## Finding number of data in columns
cluster_map_b_pca= pd.DataFrame()
cluster_map_b_pca['data_index'] = data.index.values
cluster_map_b_pca['cluster'] = model_new.labels_

cluster_map_b_pca['cluster'].value_counts() # Clusters one have 537 data points
model_new.cluster_centers_
model.n_features_in_
# Finding <NAME> Messi in which cluster and where.
model = KMeans(3, random_state=1)  
model.fit(data) 
plt.figure(figsize=(8, 8))  
plt.scatter(data.iloc[:, 0], data.iloc[:, 1], c = model.labels_, cmap='rainbow') 
plt.xlabel(data.columns[0])
plt.ylabel(data.columns[1])
plt.grid(True)

centroids = model.cluster_centers_
for ind in range(3):
    plt.scatter(centroids[ind][0], centroids[ind][1], color = 'yellow', marker='d', s=200, edgecolors='k')

    
for ind in range(4):
    plt.annotate(s= fifa_data['name'][ind], 
                 xy = (data.iloc[ind, 0], data.iloc[ind, 1]), 
                 xytext = (data.iloc[ind, 0]+1.5, data.iloc[ind, 1]+0.5),
                 arrowprops=dict(arrowstyle='->'), fontsize=16, c= 'b')
    
plt.show()<jupyter_output><empty_output><jupyter_text>1 . KMean with PCA<jupyter_code>df.info()
df_int =df[df.select_dtypes(include= ['float64','int64']).columns]#
df_int.shape
df_int.fillna(0,inplace=True)
df_int.head(3) # Takeing only mumeric columns
df_int.loc[:] = ss.fit_transform(df_int)
df_int.head()
pca = PCA()
df_int_pca = pca.fit_transform(df_int)
print('PCA done')
df_int_pca.shape[1]
np.round(pca.explained_variance_ratio_, 3) 
plt.rcParams['axes.grid']= True
plt.rcParams['font.size']= 14
plt.rcParams['figure.figsize']= (40,5)
plt.bar(range(1, 81), pca.explained_variance_ratio_ )
plt.xticks(range(1, 81))
plt.show()
# From above plot its clear that pca for 20 data points are having its contribution

df_int_pca[:, :20]
pca = PCA(n_components= 20)
df_int_pca_final = pca.fit_transform(df_int_pca)
print('PCA Final done')
df_int_pca_final.shape
inertia= []
clusters= np.arange(1, 10)

for x in clusters:
    model=KMeans(x, random_state=1)
    model.fit(df_int_pca_final)
    inertia.append(model.inertia_)
    
plt.figure(figsize=(13,5))
plt.grid(True)
plt.plot(clusters, inertia, '-o')
plt.xticks(clusters)
plt.title('Elbow plot to choose Clusters after PCA')
plt.ylabel('Inertia')
plt.xlabel('Clusters')
plt.show()
model_final = KMeans(random_state=1,n_clusters=9)
model_final.fit(data)
cluster_map_a_pca= pd.DataFrame()
cluster_map_a_pca['data_index'] = data.index.values
cluster_map_a_pca['cluster'] = model_final.labels_
cluster_map_a_pca['cluster'].value_counts() # Clusters Eight have 353 data points<jupyter_output><empty_output><jupyter_text>we cannot plot as data dimention is greater than 2  3 . KMean with  Kernal PCA<jupyter_code>from sklearn.decomposition import KernelPCA
def Kernal_pca(df,kernal):
    print('Shape of data before '+kernal+'_PCA',df.shape)
    temp=pd.DataFrame()
    temp = KernelPCA(kernel=kernal)
    temp = temp.fit_transform(df_int)
    print('Shape of data after '+kernal+'_PCA',temp.shape)
    return temp
X_transformed_linear = Kernal_pca(df_int,'linear')
X_transformed_poly = Kernal_pca(df_int,'poly')
X_transformed_rbf = Kernal_pca(df_int,'rbf')<jupyter_output>Shape of data before rbf_PCA (1000, 80)
Shape of data after rbf_PCA (1000, 999)
{'fraction_non_alphanumeric': 0.13664349553128102, 'fraction_numerical': 0.02224428997020854, 'mean_word_length': 6.1331444759206795, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '400811', 'license': 'MIT', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1937, 'n_words': 383, 'path': 'Unsupervised_learning_means_clustering_algorithm.ipynb', 'repo_name': 'DS-Nikhil-AI/ML', 'size': '5040'}