In [ ]:
# Importing libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
In [ ]:
# Loading the Data
df = pd.read_csv("online_gaming_behavior_dataset.csv")
# Printing first 5 rows of data
print("First 5 rows of data:")
display(df.head())
# Checking column datatypes
print("\nData Types:")
print(df.info())
# Checking for missing values
print("\nMissing Values:")
print(df.isnull().sum())
In [ ]:
# Dropping PlayerID
df = df.drop('PlayerID', axis=1)
# Converting text to numbers
le = LabelEncoder()
# List of columns that contain text
text_columns = ['Gender', 'Location', 'GameGenre', 'GameDifficulty', 'EngagementLevel']
for col in text_columns:
df[col] = le.fit_transform(df[col])
print(f"Converted '{col}' to numbers.")
# Checking result
print("\nNew Dataframe Head (All numbers now!):")
display(df.head())
In [ ]:
# Separate Features and Target
X = df.drop('EngagementLevel', axis=1)
y = df['EngagementLevel']
# Split data into Training and Testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Scale the data
scaler = StandardScaler()
# Transforming
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Printing the results
print("Data successfully split and scaled!")
print(f"Training Data Shape: {X_train_scaled.shape}")
print(f"Testing Data Shape: {X_test_scaled.shape}")
In [ ]:
# Initialising the Model
dt_model = DecisionTreeClassifier(random_state=42)
# Training the Model
print("Training Decision Tree... please wait.")
dt_model.fit(X_train_scaled, y_train)
# Making Predictions
y_pred_dt = dt_model.predict(X_test_scaled)
# Evaluating the Performance
accuracy_dt = accuracy_score(y_test, y_pred_dt)
# Printing the result
print(f"\nDecision Tree Accuracy: {accuracy_dt:.4f}")
print("\nClassification Report:")
print(classification_report(y_test, y_pred_dt))
# Show a Confusion Matrix
print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred_dt))
In [ ]:
# Initialising the Model
svm_model = SVC(kernel='rbf', random_state=42)
# Training the Model
print("Training SVM... this might take a minute...")
svm_model.fit(X_train_scaled, y_train)
# Making Predictions
y_pred_svm = svm_model.predict(X_test_scaled)
# Evaluating the Performance
accuracy_svm = accuracy_score(y_test, y_pred_svm)
print(f"\nSVM Accuracy: {accuracy_svm:.4f}")
print("\nClassification Report (SVM):")
print(classification_report(y_test, y_pred_svm))
In [ ]:
# Defining the parameter of settings to test
param_grid = {
'max_depth': [5, 10, 15, 20, None],
'criterion': ['gini', 'entropy']
}
# Setting up the Grid Search
grid_search = GridSearchCV(DecisionTreeClassifier(random_state=42), param_grid, cv=5, scoring='accuracy')
# Running the Search
print("Tuning Decision Tree... this checks 10 different combinations.")
grid_search.fit(X_train_scaled, y_train)
# Retrieving the results
best_tree = grid_search.best_estimator_
print(f"\nBest Parameters found: {grid_search.best_params_}")
print(f"Best Accuracy score from tuning: {grid_search.best_score_:.4f}")
# Testing this model on the real test set
y_pred_best = best_tree.predict(X_test_scaled)
print(f"Test Set Accuracy (Tuned Tree): {accuracy_score(y_test, y_pred_best):.4f}")
In [ ]:
# Preparing Data for Clustering
X_scaled = scaler.fit_transform(X)
# Finding the Optimal Number of Clusters using the Elbow Method
inertia = []
silhouette_scores = []
K_range = range(2, 6) # Testing 2 to 5 clusters
print("Testing different cluster counts...")
for k in K_range:
kmeans = KMeans(n_clusters=k, random_state=42, n_init=10)
kmeans.fit(X_scaled)
# Saving scores
inertia.append(kmeans.inertia_)
silhouette_scores.append(silhouette_score(X_scaled, kmeans.labels_))
print(f"k={k}: Inertia={kmeans.inertia_:.0f}, Silhouette={silhouette_scores[-1]:.4f}")
# Visualising the "Elbow"
plt.figure(figsize=(10, 5))
# Plotting Inertia
plt.subplot(1, 2, 1)
plt.plot(K_range, inertia, marker='o')
plt.title('Elbow Method (Inertia)')
plt.xlabel('Number of Clusters (k)')
plt.ylabel('Inertia (Lower is better)')
# Plotting Silhouette
plt.subplot(1, 2, 2)
plt.plot(K_range, silhouette_scores, marker='o', color='green')
plt.title('Silhouette Score')
plt.xlabel('Number of Clusters (k)')
plt.ylabel('Score (Higher is better)')
plt.tight_layout()
plt.show()
# Applying the Best Cluster
final_kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
df['Cluster'] = final_kmeans.fit_predict(X_scaled)
print("\nClustering Complete! 'Cluster' column added to dataframe.")
print(df['Cluster'].value_counts())
# Grouping by the new 'Cluster' column and calculating averages
cluster_summary = df.groupby('Cluster').mean(numeric_only=True)
# Selecting only the interesting columns to look at
interesting_cols = ['Age', 'PlayTimeHours', 'InGamePurchases',
'SessionsPerWeek', 'AvgSessionDurationMinutes', 'EngagementLevel']
display_summary = cluster_summary[interesting_cols]
print("Cluster Profiles (Averages):")
display(display_summary)
# Visual Comparison of Purchases vs PlayTime
plt.figure(figsize=(10, 6))
sns.scatterplot(data=df, x='PlayTimeHours', y='InGamePurchases', hue='Cluster', palette='viridis', alpha=0.6)
plt.title('Clusters: Play Time vs. Spending')
plt.show()