InĀ [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#This is used to scale the data from the set.
from sklearn.preprocessing import StandardScaler
#GridSearchCV is used to help tune hyperparameters
from sklearn.model_selection import train_test_split, GridSearchCV
#Used for Decision Tree algorithms and plotting.
from sklearn.tree import DecisionTreeClassifier, plot_tree
#Used for SVM algorithms.
from sklearn.svm import SVC
#Metrics which are are used to evaluate each model.
from sklearn import metrics
from sklearn.metrics import confusion_matrix, classification_report, precision_recall_curve, roc_auc_score, silhouette_score
#Clustering Algorithms.
from sklearn.cluster import KMeans, AgglomerativeClustering
import warnings
warnings.filterwarnings("ignore")
InĀ [2]:
#This loads the Gaming Behaviour Dataset.
df = pd.read_csv('online_gaming_behavior_dataset.csv')
InĀ [3]:
#This shows the first 5 rows of the loaded dataset.
df.head()
Out[3]:
| PlayerID | Age | Gender | Location | GameGenre | PlayTimeHours | InGamePurchases | GameDifficulty | SessionsPerWeek | AvgSessionDurationMinutes | PlayerLevel | AchievementsUnlocked | EngagementLevel | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 9000 | 43 | Male | Other | Strategy | 16.271119 | 0 | Medium | 6 | 108 | 79 | 25 | Medium |
| 1 | 9001 | 29 | Female | USA | Strategy | 5.525961 | 0 | Medium | 5 | 144 | 11 | 10 | Medium |
| 2 | 9002 | 22 | Female | USA | Sports | 8.223755 | 0 | Easy | 16 | 142 | 35 | 41 | High |
| 3 | 9003 | 35 | Male | USA | Action | 5.265351 | 1 | Easy | 9 | 85 | 57 | 47 | Medium |
| 4 | 9004 | 33 | Male | Europe | Action | 15.531945 | 0 | Medium | 2 | 131 | 95 | 37 | Medium |
InĀ [4]:
#This returns information about the dataset, including the column names, non-null count and the data type of each column.
df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 40034 entries, 0 to 40033 Data columns (total 13 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 PlayerID 40034 non-null int64 1 Age 40034 non-null int64 2 Gender 40034 non-null object 3 Location 40034 non-null object 4 GameGenre 40034 non-null object 5 PlayTimeHours 40034 non-null float64 6 InGamePurchases 40034 non-null int64 7 GameDifficulty 40034 non-null object 8 SessionsPerWeek 40034 non-null int64 9 AvgSessionDurationMinutes 40034 non-null int64 10 PlayerLevel 40034 non-null int64 11 AchievementsUnlocked 40034 non-null int64 12 EngagementLevel 40034 non-null object dtypes: float64(1), int64(7), object(5) memory usage: 4.0+ MB
InĀ [5]:
#This shows how many unique values are in each column of the dataset.
df.nunique()
Out[5]:
PlayerID 40034 Age 35 Gender 2 Location 4 GameGenre 5 PlayTimeHours 40034 InGamePurchases 2 GameDifficulty 3 SessionsPerWeek 20 AvgSessionDurationMinutes 170 PlayerLevel 99 AchievementsUnlocked 50 EngagementLevel 3 dtype: int64
InĀ [6]:
#This drops the PlayerID column from the dataset, since it is only an identifier.
df=df.drop(['PlayerID'],axis=1)
InĀ [7]:
#This organises the columns into 2 groups: numerical columns, and categorical columns.
num_cols=['Age','PlayTimeHours','SessionsPerWeek','AvgSessionDurationMinutes','PlayerLevel','AchievementsUnlocked']
cat_cols=['Gender','Location','GameGenre','GameDifficulty', 'InGamePurchases']
InĀ [8]:
#This gives summary statistics of all numerical columns.
df[num_cols].describe().T
Out[8]:
| count | mean | std | min | 25% | 50% | 75% | max | |
|---|---|---|---|---|---|---|---|---|
| Age | 40034.0 | 31.992531 | 10.043227 | 15.000000 | 23.000000 | 32.000000 | 41.000000 | 49.000000 |
| PlayTimeHours | 40034.0 | 12.024365 | 6.914638 | 0.000115 | 6.067501 | 12.008002 | 17.963831 | 23.999592 |
| SessionsPerWeek | 40034.0 | 9.471774 | 5.763667 | 0.000000 | 4.000000 | 9.000000 | 14.000000 | 19.000000 |
| AvgSessionDurationMinutes | 40034.0 | 94.792252 | 49.011375 | 10.000000 | 52.000000 | 95.000000 | 137.000000 | 179.000000 |
| PlayerLevel | 40034.0 | 49.655568 | 28.588379 | 1.000000 | 25.000000 | 49.000000 | 74.000000 | 99.000000 |
| AchievementsUnlocked | 40034.0 | 24.526477 | 14.430726 | 0.000000 | 12.000000 | 25.000000 | 37.000000 | 49.000000 |
InĀ [9]:
#This creates histograms for each numerical column.
df[num_cols].hist(figsize=(12,12),color='lime', edgecolor='black', linewidth=1.5)
plt.tight_layout()
plt.show()
InĀ [10]:
#This prints percentiles for each categorical column.
for i in cat_cols:
print(df[i].value_counts (normalize=True))
print('*'*40)
Gender Male 0.598466 Female 0.401534 Name: proportion, dtype: float64 **************************************** Location USA 0.399660 Europe 0.299845 Asia 0.202203 Other 0.098291 Name: proportion, dtype: float64 **************************************** GameGenre Sports 0.201029 Action 0.200804 Strategy 0.200130 Simulation 0.199406 RPG 0.198631 Name: proportion, dtype: float64 **************************************** GameDifficulty Easy 0.49995 Medium 0.30002 Hard 0.20003 Name: proportion, dtype: float64 **************************************** InGamePurchases 0 0.799146 1 0.200854 Name: proportion, dtype: float64 ****************************************
InĀ [11]:
#This creates a crosstab to show the relation between EngagementLevel and other categorical variables, then plots it to a bar graph.
for i in cat_cols:
if i!='EngagementLevel':
(pd.crosstab(df[i],df['EngagementLevel'])).plot(kind='bar',figsize=(8,4))
plt.ylabel('Engagement')
#Shows the crosstab results as percentiles.
show_percent = pd.crosstab(df[i], df['EngagementLevel'], normalize='index') * 100
display(show_percent)
| EngagementLevel | High | Low | Medium |
|---|---|---|---|
| Gender | |||
| Female | 26.102644 | 25.636081 | 48.261275 |
| Male | 25.627113 | 25.890062 | 48.482825 |
| EngagementLevel | High | Low | Medium |
|---|---|---|---|
| Location | |||
| Asia | 26.324892 | 25.423101 | 48.252007 |
| Europe | 25.558147 | 26.182939 | 48.258914 |
| Other | 25.667090 | 26.327827 | 48.005083 |
| USA | 25.793750 | 25.543750 | 48.662500 |
| EngagementLevel | High | Low | Medium |
|---|---|---|---|
| GameGenre | |||
| Action | 25.674835 | 25.612638 | 48.712526 |
| RPG | 25.000000 | 26.370724 | 48.629276 |
| Simulation | 26.356007 | 26.155581 | 47.488413 |
| Sports | 25.882207 | 25.770378 | 48.347416 |
| Strategy | 26.173240 | 25.037444 | 48.789316 |
| EngagementLevel | High | Low | Medium |
|---|---|---|---|
| GameDifficulty | |||
| Easy | 25.640769 | 26.010492 | 48.348738 |
| Hard | 26.373626 | 25.924076 | 47.702298 |
| Medium | 25.743069 | 25.326784 | 48.930147 |
| EngagementLevel | High | Low | Medium |
|---|---|---|---|
| InGamePurchases | |||
| 0 | 25.636858 | 25.902541 | 48.460601 |
| 1 | 26.538988 | 25.332670 | 48.128342 |
InĀ [12]:
#This shows the mean of numerical variables grouped by the engagement level.
df.groupby(['EngagementLevel'])[num_cols].mean()
Out[12]:
| Age | PlayTimeHours | SessionsPerWeek | AvgSessionDurationMinutes | PlayerLevel | AchievementsUnlocked | |
|---|---|---|---|---|---|---|
| EngagementLevel | ||||||
| High | 31.920085 | 12.069238 | 14.254547 | 131.921827 | 50.823723 | 25.095975 |
| Low | 31.896939 | 12.104915 | 4.530511 | 66.882119 | 46.101414 | 22.661565 |
| Medium | 32.082120 | 11.957503 | 9.553267 | 89.856405 | 50.926293 | 25.216424 |
InĀ [13]:
#This creates a heatmap to determine the correlation between numerical variables.
plt.figure(figsize=(16,8))
sns.heatmap(df[num_cols].corr(), annot=True, cmap='RdBu_r')
plt.show()
InĀ [14]:
#This creates dummy columns for the Game Genre and Location variables.
df = pd.get_dummies(df, columns=["GameGenre", "Location"], drop_first=True)
#This maps the GameDifficulty, Gender and EngagementLevel variables.
map_difficulty = {'Easy': 1, 'Medium': 2, 'Hard': 3}
map_gender = {'Male': 0, 'Female': 1}
map_engagement = {'Low': 1, 'Medium': 2, 'High': 3}
df['GameDifficulty'] = df['GameDifficulty'].map(map_difficulty)
df['Gender'] = df['Gender'].map(map_gender)
df['EngagementLevel'] = df['EngagementLevel'].map(map_engagement)
#This seperates our target variable, EngagementLevel, from the other variables in the dataset.
X = df.drop(columns=['EngagementLevel'])
Y = df['EngagementLevel']
InĀ [15]:
#This splits the data into a 70% train and 30% test set.
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.3, random_state = 50, stratify = Y)
InĀ [16]:
#This scales our data.
scaler = StandardScaler()
#This fits and transforms the data for X_train.
X_train_scaled = scaler.fit_transform(X_train)
X_train_scaled=pd.DataFrame(X_train_scaled, columns=X.columns)
#This transforms the data for X_test.
X_test_scaled = scaler.transform(X_test)
X_test_scaled=pd.DataFrame(X_test_scaled, columns=X.columns)
InĀ [17]:
#This function allows for a classification report to be printed and a confusion matrix to be plotted.
#This saves writing code for each model.
def metrics_score(actual, predicted):
print(classification_report(actual, predicted))
cm = confusion_matrix(actual, predicted)
plt.figure(figsize=(8,5))
sns.heatmap(cm, annot=True, fmt='.2f', xticklabels=['Low', 'Medium', 'High'], yticklabels=['Low', 'Medium', 'High'])
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show()
InĀ [18]:
#This creates a SVM using the linear kernel, and fits data to it.
svm = SVC(kernel='linear')
model = svm.fit(X= X_train_scaled, y = y_train)
InĀ [19]:
#This checks performance on the train data for the Linear SVM model.
y_pred_train_svm = model.predict(X_train_scaled)
metrics_score(y_train, y_pred_train_svm)
precision recall f1-score support
1 0.79 0.71 0.75 7227
2 0.81 0.87 0.84 13561
3 0.88 0.84 0.86 7235
accuracy 0.82 28023
macro avg 0.83 0.81 0.82 28023
weighted avg 0.82 0.82 0.82 28023
InĀ [20]:
#This checks performance on the test data for the Linear SVM model.
y_pred_test_svm = model.predict(X_test_scaled)
metrics_score(y_test, y_pred_test_svm)
precision recall f1-score support
1 0.79 0.71 0.75 3097
2 0.81 0.87 0.84 5813
3 0.89 0.86 0.87 3101
accuracy 0.83 12011
macro avg 0.83 0.81 0.82 12011
weighted avg 0.83 0.83 0.82 12011
InĀ [21]:
#This shows the coefficients of the Linear SVM model, adding it to a DataFrame.
cols = X.columns
coef_svm = svm.coef_
pd.DataFrame(coef_svm, columns=cols).T.sort_values(by=0, ascending=False)
Out[21]:
| 0 | 1 | 2 | |
|---|---|---|---|
| Location_Europe | 0.031967 | 0.017114 | -0.000226 |
| Location_USA | 0.021377 | 0.010977 | 0.011372 |
| Location_Other | 0.015172 | -0.006107 | 0.011677 |
| Gender | 0.013748 | 0.003636 | -0.001112 |
| GameDifficulty | 0.008906 | -0.014510 | -0.010850 |
| PlayTimeHours | 0.007896 | 0.006242 | -0.013970 |
| GameGenre_Sports | 0.006009 | 0.007954 | 0.019745 |
| GameGenre_Simulation | -0.002108 | -0.001512 | -0.011538 |
| GameGenre_Strategy | -0.003431 | 0.008282 | 0.009721 |
| GameGenre_RPG | -0.003809 | -0.004110 | 0.001992 |
| InGamePurchases | -0.009646 | -0.009738 | -0.029297 |
| Age | -0.016962 | 0.004590 | 0.036140 |
| AchievementsUnlocked | -0.237361 | -0.093110 | -0.087680 |
| PlayerLevel | -0.238035 | -0.128655 | -0.100413 |
| AvgSessionDurationMinutes | -1.055377 | -0.970449 | -1.808935 |
| SessionsPerWeek | -1.636695 | -1.234154 | -1.954769 |
InĀ [22]:
#This calculated the odds from the coefficient.
odds = np.exp(svm.coef_[0])
#This shows the odds by adding it to a DataFrame.
pd.DataFrame(odds, X_train_scaled.columns, columns = ['odds']).sort_values(by ='odds', ascending = False)
Out[22]:
| odds | |
|---|---|
| Location_Europe | 1.032483 |
| Location_USA | 1.021607 |
| Location_Other | 1.015287 |
| Gender | 1.013842 |
| GameDifficulty | 1.008946 |
| PlayTimeHours | 1.007927 |
| GameGenre_Sports | 1.006027 |
| GameGenre_Simulation | 0.997895 |
| GameGenre_Strategy | 0.996575 |
| GameGenre_RPG | 0.996198 |
| InGamePurchases | 0.990400 |
| Age | 0.983181 |
| AchievementsUnlocked | 0.788707 |
| PlayerLevel | 0.788175 |
| AvgSessionDurationMinutes | 0.348061 |
| SessionsPerWeek | 0.194622 |
InĀ [23]:
#This creates a SVM using the RBF kernel, and fits data to it.
svm_rbf=SVC(kernel='rbf',probability=True)
svm_rbf.fit(X_train_scaled,y_train)
Out[23]:
SVC(probability=True)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
SVC(probability=True)
InĀ [24]:
#This checks performance on the train data for the RBF SVM model.
y_pred_train_rbf = svm_rbf.predict(X_train_scaled)
metrics_score(y_train, y_pred_train_rbf)
precision recall f1-score support
1 0.91 0.85 0.88 7227
2 0.89 0.95 0.92 13561
3 0.93 0.87 0.90 7235
accuracy 0.91 28023
macro avg 0.91 0.89 0.90 28023
weighted avg 0.91 0.91 0.90 28023
InĀ [25]:
#This checks performance on the test data for the RBF SVM model.
y_pred_test_rbf = svm_rbf.predict(X_test_scaled)
metrics_score(y_test, y_pred_test_rbf)
precision recall f1-score support
1 0.89 0.83 0.86 3097
2 0.87 0.93 0.90 5813
3 0.92 0.86 0.89 3101
accuracy 0.89 12011
macro avg 0.89 0.88 0.88 12011
weighted avg 0.89 0.89 0.89 12011
InĀ [26]:
#This calculates and prints the AUC-ROC score for the RBF SVM model.
y_scores_svm=svm_rbf.predict_proba(X_train_scaled)
auc_roc = roc_auc_score(y_train, y_scores_svm, multi_class='ovr')
print("AUC-ROC Score: ", auc_roc)
AUC-ROC Score: 0.9492544538462333
InĀ [27]:
#This creates a SVM using the Polynomial kernel, and fits data to it.
svm_poly = SVC(kernel='poly', degree=3)
svm_poly.fit(X_train_scaled,y_train)
Out[27]:
SVC(kernel='poly')In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
SVC(kernel='poly')
InĀ [28]:
#This checks performance on the train data for the Polynomial SVM model.
y_pred_train_poly = svm_poly.predict(X_train_scaled)
metrics_score(y_train, y_pred_train_poly)
precision recall f1-score support
1 0.90 0.80 0.85 7227
2 0.85 0.95 0.90 13561
3 0.92 0.83 0.87 7235
accuracy 0.88 28023
macro avg 0.89 0.86 0.87 28023
weighted avg 0.88 0.88 0.88 28023
InĀ [29]:
#This checks performance on the test data for the Polynomial SVM model.
y_pred_test_poly = svm_poly.predict(X_test_scaled)
metrics_score(y_test, y_pred_test_poly)
precision recall f1-score support
1 0.88 0.78 0.83 3097
2 0.84 0.93 0.88 5813
3 0.91 0.83 0.87 3101
accuracy 0.86 12011
macro avg 0.87 0.85 0.86 12011
weighted avg 0.87 0.86 0.86 12011
InĀ [30]:
#This creates a SVM using the Sigmoid kernel, and fits data to it.
svm_sigmoid = SVC(kernel='sigmoid')
svm_sigmoid.fit(X_train_scaled,y_train)
Out[30]:
SVC(kernel='sigmoid')In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
SVC(kernel='sigmoid')
InĀ [31]:
#This checks performance on the train data for the Sigmoid SVM model.
y_pred_train_sigmoid = svm_sigmoid.predict(X_train_scaled)
metrics_score(y_train, y_pred_train_sigmoid)
precision recall f1-score support
1 0.58 0.59 0.58 7227
2 0.67 0.67 0.67 13561
3 0.71 0.71 0.71 7235
accuracy 0.66 28023
macro avg 0.65 0.66 0.66 28023
weighted avg 0.66 0.66 0.66 28023
InĀ [32]:
#This checks performance on the test data for the Sigmoid SVM model.
y_pred_test_sigmoid = svm_sigmoid.predict(X_test_scaled)
metrics_score(y_test, y_pred_test_sigmoid)
precision recall f1-score support
1 0.59 0.61 0.60 3097
2 0.69 0.68 0.68 5813
3 0.73 0.72 0.73 3101
accuracy 0.67 12011
macro avg 0.67 0.67 0.67 12011
weighted avg 0.67 0.67 0.67 12011
InĀ [33]:
#This creates a Decision Tree and fits the data to it.
dtree = DecisionTreeClassifier(random_state=50)
dtree.fit(X_train, y_train)
Out[33]:
DecisionTreeClassifier(random_state=50)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
DecisionTreeClassifier(random_state=50)
InĀ [34]:
#This plots the first 3 layers of the Decision Tree.
plt.figure(figsize=(20,7))
plot_tree(dtree, feature_names=X.columns, class_names=['Low','Medium','High'], filled=True, rounded=True, max_depth = 2)
plt.show()
InĀ [35]:
#This plots the entire decision tree.
plt.figure(figsize=(30,12))
plot_tree(dtree, feature_names=X.columns, class_names=['Low','Medium','High'], filled=True, rounded=True)
plt.show()
InĀ [36]:
#This checks performance on the test data for the Decision Tree model.
y_pred = dtree.predict(X_test)
metrics_score(y_test, y_pred)
precision recall f1-score support
1 0.80 0.80 0.80 3097
2 0.87 0.86 0.87 5813
3 0.81 0.83 0.82 3101
accuracy 0.84 12011
macro avg 0.83 0.83 0.83 12011
weighted avg 0.84 0.84 0.84 12011
InĀ [37]:
#This calculates and prints the AUC-ROC score for the Decision Tree model.
y_scores_dtree = dtree.predict_proba(X_test)
auc_roc_dtree = roc_auc_score(y_test,y_scores_dtree, multi_class='ovr')
print("AUC-ROC Score: ", auc_roc_dtree)
AUC-ROC Score: 0.8724360935646601
InĀ [38]:
#This creates a SVM using the RBF kernel, and fits data to it.
#This model has tuned hyperparameters, such as C, the regularization parameter, and gamma, the influence of each training point.
#class_weight='balanced' adjusts and balances the weights of each class for EngagementLevel.
svm_rbf_tuned = SVC(kernel='rbf',C=50, gamma=0.1, class_weight='balanced', probability=True)
svm_rbf_tuned.fit(X_train_scaled,y_train)
Out[38]:
SVC(C=50, class_weight='balanced', gamma=0.1, probability=True)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
SVC(C=50, class_weight='balanced', gamma=0.1, probability=True)
InĀ [39]:
#This checks performance on the train data for the RBF SVM model using tuned hyperparameters.
y_pred_train_tuned = svm_rbf_tuned.predict(X_train_scaled)
metrics_score(y_train, y_pred_train_tuned)
precision recall f1-score support
1 0.96 0.97 0.96 7227
2 0.98 0.97 0.97 13561
3 0.96 0.97 0.97 7235
accuracy 0.97 28023
macro avg 0.97 0.97 0.97 28023
weighted avg 0.97 0.97 0.97 28023
InĀ [40]:
#This checks performance on the test data for the RBF SVM model using tuned hyperparameters.
y_pred_test_tuned = svm_rbf_tuned.predict(X_test_scaled)
metrics_score(y_test, y_pred_test_tuned)
precision recall f1-score support
1 0.76 0.79 0.78 3097
2 0.85 0.83 0.84 5813
3 0.82 0.82 0.82 3101
accuracy 0.82 12011
macro avg 0.81 0.81 0.81 12011
weighted avg 0.82 0.82 0.82 12011
InĀ [41]:
#This calculates and prints the AUC-ROC score for the RBF SVM model using tuned hyperparameters.
y_scores_tuned=svm_rbf_tuned.predict_proba(X_train_scaled)
auc_roc_tuned = roc_auc_score(y_train, y_scores_tuned, multi_class='ovr')
print("AUC-ROC Score: ", auc_roc_tuned)
AUC-ROC Score: 0.9913914716746848
InĀ [45]:
#This is a parameter grid of different hyperparameters, which will be used for GridSearchCV.
tree_param = {
'criterion' :['gini', 'entropy'],
'max_depth': [10, 20, 30, None],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4],
}
#This creates a Decision Tree.
dtree_tuned = DecisionTreeClassifier(random_state=50)
#This uses GridSearchCV to help find the best parameters to tune the Decision Tree model.
grid_search_dtree = GridSearchCV(estimator = dtree_tuned, param_grid=tree_param, cv=5, n_jobs=-1, verbose=True)
#This fits the train and test data.
grid_search_dtree.fit(X_train, y_train)
#This displays the best parameters found by GridSearchCV.
display(grid_search_dtree.best_params_)
#This applies the found parameters to the Decision Tree model.
dtree_tuned = grid_search_dtree.best_estimator_
Fitting 5 folds for each of 72 candidates, totalling 360 fits
{'criterion': 'entropy',
'max_depth': 10,
'min_samples_leaf': 4,
'min_samples_split': 10}
InĀ [46]:
#This plots the first 3 layers of the tuned Decision Tree.
plt.figure(figsize=(20,7))
plot_tree(dtree_tuned, feature_names=X.columns, class_names=['Low','Medium','High'], filled=True, rounded=True, max_depth = 2)
plt.show()
InĀ [47]:
#This plots the entire tuned Decision Tree.
plt.figure(figsize=(30,12))
plot_tree(dtree_tuned, feature_names=X.columns, class_names=['Low','Medium','High'], filled=True, rounded=True)
plt.show()
InĀ [48]:
#This checks performance on the test data for the tuned Decision Tree model.
y_pred_tuned = dtree_tuned.predict(X_test)
metrics_score(y_test, y_pred_tuned)
precision recall f1-score support
1 0.89 0.87 0.88 3097
2 0.91 0.94 0.92 5813
3 0.92 0.89 0.90 3101
accuracy 0.91 12011
macro avg 0.91 0.90 0.90 12011
weighted avg 0.91 0.91 0.91 12011
InĀ [49]:
#This calculates and prints the AUC-ROC score for the tuned Decision Tree model.
y_scores_dtree_tuned = dtree_tuned.predict_proba(X_test)
auc_roc_dtree = roc_auc_score(y_test,y_scores_dtree_tuned, multi_class='ovr')
print("AUC-ROC Score: ", auc_roc_dtree)
AUC-ROC Score: 0.9366403804622262
InĀ [50]:
#This picks random sample data from the dataset, then fits and transforms it.
sample = df.sample(n=200, random_state=32)
sample_scale = scaler.fit_transform(sample[['PlayerLevel','AchievementsUnlocked']])
sample2 = df.sample(n=200, random_state=87)
sample2_scale = scaler.fit_transform(sample2[['Age','PlayTimeHours']])
InĀ [53]:
#This creates a K-Means model using 3 clusters, and fits data to it.
kmeans = KMeans(n_clusters=3, random_state=7)
kmeans.fit(sample_scale)
#This applies cluster labels using K-Means.
kmeans_labels = kmeans.labels_
#This applies the labels from K-Means to the data, and plots it to a scatter graph.
sample['Cluster'] = kmeans_labels
sns.scatterplot(x=sample['PlayerLevel'],y=sample['AchievementsUnlocked'],hue=sample['Cluster'],data=sample,palette='Set1')
Out[53]:
<Axes: xlabel='PlayerLevel', ylabel='AchievementsUnlocked'>
InĀ [54]:
#This finds and prints both the Silhouette and Inertia scores for the K-Means model.
silhouette_km = silhouette_score(sample_scale, kmeans_labels)
inertia_km = kmeans.inertia_
print("Silhouette Score: ", silhouette_km)
print("Inertia Score: ", inertia_km)
Silhouette Score: 0.4013138016087001 Inertia Score: 147.4557070418625
InĀ [55]:
#This finds the Within Cluster Sum of Squares to find the best amount of clusters, using the elbow method.
WCSS= []
K = range(1,9)
for i in K:
Kmeanmodel=KMeans(n_clusters=i, init='k-means++', max_iter=300, n_init=10, random_state=42)
Kmeanmodel.fit(df[['PlayerLevel','AchievementsUnlocked']])
WCSS.append(Kmeanmodel.inertia_)
#This plots the WCSS to a graph.
plt.plot(range(1,9),WCSS)
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
InĀ [56]:
#This creates a Agglomerative Clustering model using 3 clusters, and fits data to it.
agg_clustering = AgglomerativeClustering(n_clusters=3)
#This applies cluster labels using Agglomerative Clustering.
agg_cluster_labels = agg_clustering.fit_predict(sample_scale)
#This applies the labels from Agglomerative Clustering to the data, and plots it to a scatter graph.
sample['Cluster'] = agg_cluster_labels
sns.scatterplot(x=sample['PlayerLevel'],y=sample['AchievementsUnlocked'],hue=sample['Cluster'],palette='Set1')
Out[56]:
<Axes: xlabel='PlayerLevel', ylabel='AchievementsUnlocked'>
InĀ [57]:
#This finds and prints the Silhouette score for the Agglomerative Clustering model.
silhouette_agg = silhouette_score(sample_scale, agg_cluster_labels)
print("Silhouette Score:", silhouette_agg)
Silhouette Score: 0.3802371074372398
InĀ [58]:
#This creates another Agglomerative Clustering model using 4 clusters, and fits data to it.
agg_clustering2 = AgglomerativeClustering(n_clusters=4)
#This applies cluster labels using Agglomerative Clustering.
agg_cluster_labels2 = agg_clustering2.fit_predict(sample2_scale)
#This applies the labels from Agglomerative Clustering to the data, and plots it to a scatter graph.
#The data here is the age of the player and the respective play time hours.
sample2['Cluster'] = agg_cluster_labels2
sns.scatterplot(x=sample2['Age'],y=sample2['PlayTimeHours'],hue=sample2['Cluster'],palette='Set1')
Out[58]:
<Axes: xlabel='Age', ylabel='PlayTimeHours'>
InĀ [59]:
#This finds and prints the Silhouette score for the Agglomerative Clustering model.
silhouette_agg2 = silhouette_score(sample2_scale, agg_cluster_labels2)
print("Silhouette Score:", silhouette_agg2)
Silhouette Score: 0.40824535777628673