# Step 1: Import libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix
import matplotlib.pyplot as plt # Import matplotlib.pyplot
# Step 2: Load Moodle data
# Generate sample data
data = pd.DataFrame({
'login_count': [10, 8, 15, 20, 5, 12, 18, 25, 3, 10],
'resource_views': [5, 3, 10, 12, 2, 7, 11, 15, 1, 6],
'total_time': [120, 90, 300, 400, 60, 180, 360, 500, 30, 150],
'final_grade': [55, 45, 75, 80, 30, 60, 70, 85, 20, 50]
})
# Replace with actual file path
# Step 3: Preprocess data
data['login_count'] = data['login_count'].fillna(0) # Handle missing values
data['total_time'] = pd.to_numeric(data['total_time'], errors='coerce').fillna(0)
data['passed'] = data['final_grade'] >= 50 # Binary target variable
# Step 4: Split dataset
X = data[['login_count', 'resource_views', 'total_time']] # Features
y = data['passed'] # Target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Step 5: Train model
model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)
# Step 6: Evaluate model
y_pred = model.predict(X_test)
print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred))
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
# Step 7: Feature importance visualization
importances = model.feature_importances_
features = X.columns
plt.bar(features, importances) # plt is now defined and can be used
plt.title("Feature Importance")
plt.show()
IyBTdGVwIDE6IEltcG9ydCBsaWJyYXJpZXMKaW1wb3J0IHBhbmRhcyBhcyBwZAppbXBvcnQgbnVtcHkgYXMgbnAKZnJvbSBza2xlYXJuLm1vZGVsX3NlbGVjdGlvbiBpbXBvcnQgdHJhaW5fdGVzdF9zcGxpdApmcm9tIHNrbGVhcm4uZW5zZW1ibGUgaW1wb3J0IFJhbmRvbUZvcmVzdENsYXNzaWZpZXIKZnJvbSBza2xlYXJuLm1ldHJpY3MgaW1wb3J0IGNsYXNzaWZpY2F0aW9uX3JlcG9ydCwgY29uZnVzaW9uX21hdHJ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