File size: 2,595 Bytes
108e0c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
"""skipwithpredictor.159

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1C7AO89jheeQ3C61BPsSdIfK5tCgcL7IT
"""

import pandas as pd
import numpy as np

df = pd.read_csv('/content/online_course_engagement_data.csv')

df.dtypes

df.info()

df.isnull().sum()

df.drop('UserID', axis=1,inplace=True)

df['CourseCategory'].unique()

cat_mapping={
    'Heatlh': 1,
    'Arts': 2,
    'Science': 3,
    'Programming': 4,
    'Business': 5
}

df['CourseCategory'] = df['CourseCategory'].map(cat_mapping)

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()

df['QuizScores'] = scaler.fit_transform(df[['QuizScores']])
df['CompletionRate'] = scaler.fit_transform(df[['CompletionRate']])

df.head(15)

df.dtypes

import matplotlib.pyplot as plt
import seaborn as sns

int_col = df.select_dtypes(include='int').columns
float_col = df.select_dtypes(include='float').columns

plt.figure(figsize=(15,15))

for i, col in enumerate(int_col, 1):
  plt.subplot(3,2,i)
  counts = df[col].value_counts()
  plt.bar(counts.index, counts)
  plt.title(f'Bar Chart of {col}')
  plt.xlabel(col)
  plt.ylabel('Frequency')

  for x, y in zip(counts.index, counts):
    plt.text(x, y, str(y), ha='center', va='bottom')

  plt.tight_layout()
  plt.show

plt.figure(figsize=(12, 6))

for i, col in enumerate(float_col, 1):
  plt.subplot(1, 3, 1)
  sns.boxplot(y=df[col])
  plt.title(f'Box Plot of {col}')
  plt.ylabel(col)

  plt.tight_layout()
  plt.show()

cor = df.corr()

plt.figure(figsize=(10, 6))
sns.heatmap(cor,annot=True, cmap="coolwarm", fmt=".2f")

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import xgboost as xgb
import lightgbm as lgb
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

X = df.drop('CourseCompletion', axis=1)
y = df['CourseCompletion']

seed = 42

Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, test_size=0.2, random_state=seed)

models = {
    'RandomForest': RandomForestClassifier(random_state=seed),
    'XGBoost': xgb.XGBClassifier(random_state=seed),
    'LightGBM': lgb.LGBMClassifier(random_state=seed)
}

result = {}

for name, model in models.items():
  model.fit(Xtrain, ytrain)
  y_pred = model.predict(Xtest)
  accuracy = accuracy_score(ytest, y_pred)
  result[name] = accuracy
  print(f'{name} Accuracy: {accuracy:.2f}')

  print('Classification Report:')
  print(classification_report(ytest, y_pred))
  print('Confusion Matrix:')
  print(confusion_matrix(ytest, y_pred))