Created
May 17, 2016 17:27
-
-
Save jameskyle/26d7ddb2df1864050bfaf7f109e2f6e7 to your computer and use it in GitHub Desktop.
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
from __future__ import print_function, division | |
import datetime as dt | |
import pandas as pd | |
import numpy as np | |
import scipy.optimize as sopt | |
def gradebook(): | |
columns = ("Assignment", "Due", "Grade", "Weight") | |
rows = [ | |
("Assignment One", dt.datetime(2016, 5, 23), np.nan, 0.02), | |
("Assignment Two", dt.datetime(2016, 5, 23), np.nan, 0.02), | |
("Peer Feedback One", dt.datetime(2016, 5, 30), np.nan, 0.005), | |
("Peer Feedback Two", dt.datetime(2016, 5, 30), np.nan, 0.005), | |
("Assignment Three", dt.datetime(2016, 5, 30), np.nan, 0.065), | |
("Assignment Four", dt.datetime(2016, 5, 30), np.nan, 0.065), | |
("Peer Feedback Three", dt.datetime(2016, 6, 6), np.nan, 0.005), | |
("Peer Feedback Four", dt.datetime(2016, 6, 6), np.nan, 0.005), | |
("Assignment Five", dt.datetime(2016, 6, 6), np.nan, 0.065), | |
("Peer Feedback Five", dt.datetime(2016, 6, 13), np.nan, 0.005), | |
("Assignment Six", dt.datetime(2016, 6, 13), np.nan, 0.065), | |
("Assignment Seven", dt.datetime(2016, 6, 13), np.nan, 0.065), | |
("Peer Feedback Six", dt.datetime(2016, 6, 20), np.nan, 0.005), | |
("Peer Feedback Seven", dt.datetime(2016, 6, 20), np.nan, 0.005), | |
("Assignment Eight", dt.datetime(2016, 6, 20), np.nan, 0.065), | |
("Assignment Nine", dt.datetime(2016, 6, 20), np.nan, 0.065), | |
("Peer Feedback Eight", dt.datetime(2016, 6, 27), np.nan, 0.005), | |
("Peer Feedback Nine", dt.datetime(2016, 6, 27), np.nan, 0.005), | |
("Assignment Ten", dt.datetime(2016, 6, 27), np.nan, 0.065), | |
("Peer Feedback Ten", dt.datetime(2016, 7, 4), np.nan, 0.005), | |
("Assignment Eleven", dt.datetime(2016, 7, 4), np.nan, 0.065), | |
("Peer Feedback Eleven", dt.datetime(2016, 7, 11), np.nan, 0.005), | |
("Term Project Report", dt.datetime(2016, 7, 25), np.nan, 0.1), | |
("Peer Feedback Term Project Report", dt.datetime(2016, 8, 3), np.nan, 0.005), | |
("Exam", dt.datetime(2016, 8, 3), np.nan, 0.15), | |
("Final Portfolio", dt.datetime(2016, 8, 3), np.nan, 0.025), | |
("Piazza Participation", dt.datetime(2016, 8, 3), np.nan, 0.04), | |
] | |
return pd.DataFrame(rows, columns=columns) | |
def calc(grades): | |
df = gradebook() | |
df.loc[df['Grade'].isnull(), 'Grade'] = grades | |
return np.abs(check_grade(df) - .80) | |
def optimize(gb=None): | |
if gb is None: | |
gb = gradebook() | |
nullidxs = gb['Grade'].isnull() | |
nullsize = gb[nullidxs].shape[0] | |
guess = [.5] * nullsize | |
bounds = ((0.0, 1.0),) * nullsize | |
res = sopt.minimize( | |
calc, | |
guess, | |
method="SLSQP", | |
bounds=bounds, | |
) | |
optimized = gb.copy() | |
optimized.loc[nullidxs, 'Grade'] = res.x | |
return optimized | |
def check_grade(df): | |
return (df['Grade'] * df['Weight']).sum() | |
def main(): | |
result = optimize() | |
print(result.to_string()) | |
print("Optimized for final grade of: {0}".format(check_grade(result))) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment