-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
190 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,55 @@ | ||
""" | ||
Efficient implementation of knowledge tracing machines using scikit-learn. | ||
""" | ||
import argparse | ||
from sklearn.pipeline import Pipeline | ||
from sklearn.preprocessing import OneHotEncoder | ||
from sklearn.linear_model import LogisticRegression | ||
from sklearn.model_selection import cross_validate, GroupShuffleSplit | ||
import pandas as pd | ||
|
||
|
||
df = pd.read_csv('data/dummy/data.csv') | ||
estimators = [ | ||
('onehot', OneHotEncoder()), | ||
('lr', LogisticRegression()) | ||
] | ||
pipe = Pipeline(estimators) | ||
parser = argparse.ArgumentParser(description='Run simple KTM') | ||
parser.add_argument('--dataset', type=str, nargs='?', default='dummy') | ||
parser.add_argument('--model', type=str, nargs='?', default='iswf') | ||
options = parser.parse_args() | ||
|
||
# Just check the encoded variables | ||
ohe = OneHotEncoder() | ||
print(ohe.fit_transform(df[['user', 'item']]).toarray()) | ||
|
||
# IRT | ||
pipe.fit(df[['user', 'item']], df['correct']) | ||
print(pipe.predict_proba(df[['user', 'item']])) | ||
df = pd.read_csv(f'data/{options.dataset}/data.csv') | ||
pipe = Pipeline([ | ||
('onehot', OneHotEncoder(handle_unknown='ignore')), | ||
('lr', LogisticRegression(solver='liblinear')) | ||
]) | ||
|
||
# PFA | ||
pipe.fit(df[['skill', 'wins', 'fails']], df['correct']) | ||
print(pipe.predict_proba(df[['skill', 'wins', 'fails']])) | ||
|
||
cv = GroupShuffleSplit(n_splits=5, random_state=42) | ||
METRICS = ['accuracy', 'roc_auc', 'neg_log_loss'] | ||
if options.model == 'irt': | ||
FEATURES = ['user', 'item'] | ||
elif options.model == 'pfa': | ||
FEATURES = ['skill', 'wins', 'fails'] | ||
else: | ||
FEATURES = ['item', 'skill', 'wins', 'fails'] | ||
|
||
cv_results = cross_validate( | ||
pipe, df[FEATURES], df['correct'], | ||
scoring=METRICS, # Use all scores | ||
return_train_score=True, n_jobs=-1, # Use all cores | ||
cv=cv, groups=df['user'], verbose=10 | ||
) | ||
for metric in METRICS: | ||
print(metric, cv_results[f"test_{metric}"].mean()) | ||
|
||
|
||
for i_train, i_test in cv.split(df, groups=df['user']): | ||
df_train = df.iloc[i_train] | ||
df_test = df.iloc[i_test] | ||
|
||
# IRT | ||
pipe.fit(df_train[['user', 'item']], df_train['correct']) | ||
print(pipe.predict_proba(df_test[['user', 'item']])[:, 1]) | ||
|
||
# PFA | ||
pipe.fit(df_train[['skill', 'wins', 'fails']], df_train['correct']) | ||
print(pipe.predict_proba(df_test[['skill', 'wins', 'fails']])[:, 1]) | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters