MLP Personalize Training
# Step 1: Import Required Libraries
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import seaborn as sns
# Step 2: Create Sample Data
# Each student: video_time, story_time, exercise_time (in minutes)
data = {
'video_time': [30, 5, 10, 50, 40, 15, 5, 45, 8, 60],
'story_time': [5, 30, 25, 5, 10, 35, 45, 10, 40, 5],
'exercise_time': [10, 15, 40, 5, 10, 20, 30, 5, 30, 10],
'preferred_material': [
'Video', 'Story', 'Exercise', 'Video', 'Video', 'Story', 'Story', 'Video', 'Story', 'Video'
]
}
df = pd.DataFrame(data)
# Step 3: Preprocess Data
features = ['video_time', 'story_time', 'exercise_time']
X = df[features]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Step 4: Apply Clustering (KMeans) - Group Students by Learning Style
kmeans = KMeans(n_clusters=3, random_state=42)
df['cluster'] = kmeans.fit_predict(X_scaled)
# Step 5: Visualize Clusters
plt.figure(figsize=(8,5))
sns.scatterplot(data=df, x='video_time', y='story_time', hue='cluster', palette='Set1', s=100)
plt.title('Student Clustering Based on Learning Behavior')
plt.xlabel('Video Time (minutes)')
plt.ylabel('Story Time (minutes)')
plt.show()
# Step 6: Train a KNN Model to Recommend Learning Material
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_scaled, df['preferred_material'])
# Step 7: Predict for a New Student
# Example: new student data
new_student = np.array([[35, 10, 5]]) # This student spends more time on video
new_student_scaled = scaler.transform(new_student)
predicted_material = knn.predict(new_student_scaled)
print("\nRecommended Learning Material for New Student:", predicted_material[0])
# Step 8: Bonus - Predict Cluster (Learning Style) for New Student
predicted_cluster = kmeans.predict(new_student_scaled)
print("Predicted Learning Style Group (Cluster):", predicted_cluster[0])
Comments
Post a Comment