def predict_values(model, type, date, training_data_length=14, days_to_predict=25, window=24): training_data_length, days_to_predict = int(training_data_length), int(days_to_predict) modelclass = get_model(model) cc = CrashCounts(type, window) x, original = cc.crash_counts() date = datetime.strptime(date, "%Y%m%d") bucket = int(buckify(window, date)) date = x.index(bucket) x = [] predicted_values = [] actual_values = [] residuals = [] for i in xrange(1, days_to_predict): counts = original[date-training_data_length-i:date-i] model = modelclass(counts) predicted = model.predict_next() predicted_values.append(predicted) actual_values.append(original[-i]) x.append(len(original) - i) residuals.append(abs((predicted - original[-i]) / original[-i] * 100)) plt.plot(x, actual_values) plt.plot(x, predicted_values) plt.legend(("Actual", "Predicted")) plt.ylim(0, 1.2 * np.max(predicted_values + actual_values)) elementary_stats(residuals) plt.show()
def crashes_time_series(type="global", start="", end="", hours=24, *args): start, end = parse_start_end(start, end) hours = int(hours) cc = CrashCounts(type, hours) t, counts = cc.crash_counts(start, end) elementary_stats(counts) plt.title("{} crashes from {} to {} (bin={} hours)".format(type, start, end, hours)) plt.xlabel("Time") plt.ylabel("Crashes") plt.xlim(min(t), max(t)) plt.ylim(0, max(counts) * 1.2) plt.grid(True) plt.plot(t, counts) plt.show()