Repository navigation
Expand file tree
/
Copy pathAdaptive-Learning-System.py
More file actions
118 lines (96 loc) · 3.87 KB
/
Copy pathAdaptive-Learning-System.py
File metadata and controls
118 lines (96 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# adaptive_learning_system.py
"""
Adaptive Learning System Module for Personalized Learning Path Recommendation System
This module contains functions for implementing an adaptive learning system that adjusts
recommendations based on student progress and learning style.
Techniques Used:
- Reinforcement Learning
- Dynamic Adjustment
Algorithms Used:
- Deep Q-Learning (DQN)
- Contextual Bandits
Libraries/Tools:
- TensorFlow
- Keras
"""
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.preprocessing import StandardScaler
import joblib
class AdaptiveLearningSystem:
def __init__(self, state_size, action_size):
"""
Initialize the AdaptiveLearningSystem class.
:param state_size: int, size of the state space
:param action_size: int, size of the action space
"""
self.state_size = state_size
self.action_size = action_size
self.model = self._build_model()
self.scaler = StandardScaler()
def _build_model(self):
"""
Build the DQN model.
:return: Sequential, the DQN model
"""
model = Sequential()
model.add(Dense(24, input_dim=self.state_size, activation='relu'))
model.add(Dense(24, activation='relu'))
model.add(Dense(self.action_size, activation='linear'))
model.compile(loss='mse', optimizer=tf.keras.optimizers.Adam(lr=0.001))
return model
def preprocess_data(self, data):
"""
Preprocess the data for training and prediction.
:param data: DataFrame, input data
:return: DataFrame, preprocessed data
"""
data = data.fillna(data.mean())
data = self.scaler.fit_transform(data)
return data
def train(self, data, target_column, epochs=10):
"""
Train the adaptive learning system using DQN.
:param data: DataFrame, input data
:param target_column: str, name of the target column
:param epochs: int, number of epochs for training
"""
X = data.drop(columns=[target_column])
y = data[target_column]
for epoch in range(epochs):
for i in range(len(X)):
state = X.iloc[i].values.reshape(1, -1)
action = y.iloc[i]
target = self.model.predict(state)
target[0][action] = action
self.model.fit(state, target, epochs=1, verbose=0)
joblib.dump(self.scaler, 'models/scaler.pkl')
self.model.save('models/adaptive_learning_model.h5')
def predict(self, student_data):
"""
Predict the next best action for the student.
:param student_data: DataFrame, data of the student
:return: int, predicted action
"""
scaler = joblib.load('models/scaler.pkl')
model = tf.keras.models.load_model('models/adaptive_learning_model.h5')
student_data = scaler.transform(student_data)
action_values = model.predict(student_data)
return np.argmax(action_values[0])
if __name__ == "__main__":
data_filepath = 'data/processed/preprocessed_student_data.csv'
student_data_filepath = 'data/processed/new_student_data.csv'
target_column = 'learning_path'
adaptive_system = AdaptiveLearningSystem(state_size=5, action_size=3) # Example sizes
data = pd.read_csv(data_filepath)
data = adaptive_system.preprocess_data(data)
# Train the adaptive learning system
adaptive_system.train(data, target_column)
# Predict the next best action for a new student
new_student_data = pd.read_csv(student_data_filepath)
new_student_data = adaptive_system.preprocess_data(new_student_data)
next_action = adaptive_system.predict(new_student_data)
print("Next best action for the student:", next_action)