|
|
|
|
|
"""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)) |