MLP AI for Special Education
# Step 1: Import necessary libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.metrics import classification_report, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
# Step 2: Create a sample dataset
# Features: Attention Span, Response Time, Interaction Level, Comprehension Score
data = {
'attention_span': [20, 50, 15, 45, 10, 55, 12, 48, 25, 60], # in minutes
'response_time': [5, 3, 8, 2, 10, 2, 9, 3, 6, 2], # in seconds
'interaction_level': [1, 3, 1, 4, 1, 4, 2, 5, 2, 5], # scale of 1-5
'comprehension_score': [60, 85, 55, 90, 50, 88, 58, 92, 65, 95], # out of 100
'special_need': [
'Autism', 'None', 'Autism', 'None', 'Dyslexia', 'None', 'ADHD', 'None', 'ADHD', 'None'
]
}
df = pd.DataFrame(data)
# Step 3: Preprocessing
X = df[['attention_span', 'response_time', 'interaction_level', 'comprehension_score']]
y = df['special_need']
# Encode target labels
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y)
# Scale feature data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Step 4: Train-Test Split
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_encoded, test_size=0.3, random_state=42)
# Step 5: Apply SVM Classifier
svm_model = SVC(kernel='rbf', gamma='scale', random_state=42)
svm_model.fit(X_train, y_train)
# Step 6: Predict and Evaluate SVM
y_pred_svm = svm_model.predict(X_test)
print("\n--- SVM Classification Report ---")
print(classification_report(y_test, y_pred_svm, target_names=label_encoder.classes_))
# Step 7: Apply Decision Tree Classifier (for suggesting learning strategies)
dt_model = DecisionTreeClassifier(max_depth=3, random_state=42)
dt_model.fit(X_train, y_train)
# Step 8: Predict and Evaluate Decision Tree
y_pred_dt = dt_model.predict(X_test)
print("\n--- Decision Tree Classification Report ---")
print(classification_report(y_test, y_pred_dt, target_names=label_encoder.classes_))
# Step 9: Visualize Decision Tree
plt.figure(figsize=(15,10))
plot_tree(dt_model, filled=True, feature_names=['Attention', 'Response', 'Interaction', 'Comprehension'],
class_names=label_encoder.classes_)
plt.title("Decision Tree: Suggesting Learning Strategies")
plt.show()
# Step 10: Additional - Visualize Confusion Matrix
cm = confusion_matrix(y_test, y_pred_dt)
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=label_encoder.classes_, yticklabels=label_encoder.classes_)
plt.title("Decision Tree Confusion Matrix")
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()
Comments
Post a Comment