<jupyter_start><jupyter_text><NAME> 99201465  0. Importing the necessary modules<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
from sklearn.cluster import KMeans<jupyter_output><empty_output><jupyter_text>1. Loading the data<jupyter_code>df = pd.read_csv('2019-2020 All leagues Stats.csv')
df.head()
df.shape<jupyter_output><empty_output><jupyter_text>2. Selecting features Replacing the - with nan:<jupyter_code>df.replace('-', np.nan, inplace=True)<jupyter_output><empty_output><jupyter_text>Replacing the no data cells:<jupyter_code>df.replace('No data found', np.nan, inplace=True)<jupyter_output><empty_output><jupyter_text>Removing the rows with type of goal not equal to player<jupyter_code>df2 = df[df['Type Of Goal'] == 'Player']
df2 = df2.drop('Type Of Goal', axis=1)<jupyter_output><empty_output><jupyter_text>Removing the rows without a target:<jupyter_code>df2 = df2[~df2['Position'].isna()]<jupyter_output><empty_output><jupyter_text>Remoing the Position and other not related columns:<jupyter_code>y_true = df2['Position']
df2 = df2.drop(['Position', 'KEY', 'Player'], axis=1)<jupyter_output><empty_output><jupyter_text>Missing Value Ratio Filter:<jupyter_code>df2 = df2.drop(df2.columns[df2.isna().sum() / len(df2) > 0.8], axis=1)<jupyter_output><empty_output><jupyter_text>Label Encoding:<jupyter_code>null_mask = df2.isnull()
df2 = df2.astype(str).apply(LabelEncoder().fit_transform).where(~null_mask, df2)<jupyter_output><empty_output><jupyter_text>Filling the NaN values:<jupyter_code>df2 = df2.fillna(df2.mode().loc[0])<jupyter_output><empty_output><jupyter_text>Preprocessing the target:<jupyter_code>y_true.replace(list(y_true.unique()), list(range(4)), inplace=True)<jupyter_output><empty_output><jupyter_text>3. Clustering using kmeans<jupyter_code>kmeans = KMeans(n_clusters=4).fit(df2)
y_pred = kmeans.predict(df2)<jupyter_output><empty_output><jupyter_text>4. Visualizing the results<jupyter_code>df2.head()<jupyter_output><empty_output><jupyter_text>predictions:<jupyter_code>sns.scatterplot(x='Appearances', y='Goals', data=df2, 
                hue=y_pred, palette="deep")<jupyter_output><empty_output><jupyter_text>Ground truth:<jupyter_code>sns.scatterplot(x='Appearances', y='Goals', data=df2, 
                hue=y_true, palette="deep")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1421960454354228, 'fraction_numerical': 0.023138409760201935, 'mean_word_length': 9.809090909090909, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 63, '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': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '403429', 'license': 'MIT', 'n_tokens_mistral': 956, 'n_tokens_neox': 916, 'n_words': 127, 'path': 'Exams/Mid-term/question_01.ipynb', 'repo_name': 'alimirferdos/Data_Mining_Course', 'size': '2390'}