fork download
  1. # Step 1: Import libraries
  2. import pandas as pd
  3. import numpy as np
  4. from sklearn.model_selection import train_test_split
  5. from sklearn.ensemble import RandomForestClassifier
  6. from sklearn.metrics import classification_report, confusion_matrix
  7. import matplotlib.pyplot as plt # Import matplotlib.pyplot
  8.  
  9. # Step 2: Load Moodle data
  10. # Generate sample data
  11. data = pd.DataFrame({
  12. 'login_count': [10, 8, 15, 20, 5, 12, 18, 25, 3, 10],
  13. 'resource_views': [5, 3, 10, 12, 2, 7, 11, 15, 1, 6],
  14. 'total_time': [120, 90, 300, 400, 60, 180, 360, 500, 30, 150],
  15. 'final_grade': [55, 45, 75, 80, 30, 60, 70, 85, 20, 50]
  16. })
  17. # Replace with actual file path
  18.  
  19. # Step 3: Preprocess data
  20. data['login_count'] = data['login_count'].fillna(0) # Handle missing values
  21. data['total_time'] = pd.to_numeric(data['total_time'], errors='coerce').fillna(0)
  22. data['passed'] = data['final_grade'] >= 50 # Binary target variable
  23.  
  24. # Step 4: Split dataset
  25. X = data[['login_count', 'resource_views', 'total_time']] # Features
  26. y = data['passed'] # Target
  27. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
  28.  
  29. # Step 5: Train model
  30. model = RandomForestClassifier(random_state=42)
  31. model.fit(X_train, y_train)
  32.  
  33. # Step 6: Evaluate model
  34. y_pred = model.predict(X_test)
  35. print("Confusion Matrix:")
  36. print(confusion_matrix(y_test, y_pred))
  37. print("\nClassification Report:")
  38. print(classification_report(y_test, y_pred))
  39.  
  40. # Step 7: Feature importance visualization
  41. importances = model.feature_importances_
  42. features = X.columns
  43. plt.bar(features, importances) # plt is now defined and can be used
  44. plt.title("Feature Importance")
  45. plt.show()
  46.  
Success #stdin #stdout 3.81s 130652KB
stdin
Standard input is empty
stdout
Confusion Matrix:
[[1 1]
 [0 1]]

Classification Report:
              precision    recall  f1-score   support

       False       1.00      0.50      0.67         2
        True       0.50      1.00      0.67         1

    accuracy                           0.67         3
   macro avg       0.75      0.75      0.67         3
weighted avg       0.83      0.67      0.67         3