def fit(self, deathtraining, casetraining, K1, K2, TRAINERROR): ts_1 = deathtraining ts_2 = casetraining T = len(ts_1) y = ts_1[K1 - 1:T] # size = T-K n = len(y) y = y.reshape(n, 1) X = np.ones((n, 1)) for i in range(K1 - 1): x_j = ts_1[i:n + i].reshape(n, 1) X = np.concatenate((X, x_j), axis=1) for i in range(K2 - 1): x_j2 = ts_2[i:n + i].reshape(n, 1) X = np.concatenate((X, x_j2), axis=1) #print(X.shape) # train precess model = linear_model.LeastSquares() model.fit(X, y) self.model = model #print(model.w) if TRAINERROR: y_hat = model.predict(X) #print(y_hat, y) #plt.plot(y_hat) tr_error = np.mean((y - y_hat)**2) print("Training error = %.3f" % tr_error)
def fit(self, t, T, K): ts = self.ts y = ts[K - 1:T] # size = T-K n = len(y) y = y.reshape(n, 1) X = np.ones((n, 1)) for i in range(K - 1): x_j = ts[i:n + i].reshape(n, 1) X = np.concatenate((X, x_j), axis=1) # train model = linear_model.LeastSquares() model.fit(X, y) yhat = model.predict(X) trainError = np.mean((yhat - y)**2) # print("Training error = %.3f" % trainError) # T+1 X_test = np.concatenate(([1], ts[T - K + 1:T]), axis=0).reshape(1, K) y_test = np.array(ts[T]).reshape(1, 1) y_test_overall = y_test y_pred = model.predict(X_test).reshape(1, 1) # print("predicted case number: %.3f, real case number: %.3f" % (y_pred, y_test)) y_pred_overall = y_pred # predict for i in range(1, t): # print("day#: %d" % (i + 1)) a = X_test[:, 2:] X_test = np.concatenate(([[1]], a, y_pred), axis=1) y_test_i = np.array(ts[T + i]).reshape(1, 1) y_test_overall = np.concatenate((y_test_overall, y_test_i), axis=0) y_pred = model.predict(X_test).reshape(1, 1) # print("predicted case number: %.3f, real case number: %.3f" % (y_pred, y_test_i)) y_pred_overall = np.concatenate((y_pred_overall, y_pred), axis=0) testError = np.mean((y_pred_overall - y_test_overall)**2) # print("Test error = %.3f" % testError) return [trainError, testError, model, y_test_overall, y]
plt.savefig(filename) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('-q', '--question', required=True) io_args = parser.parse_args() question = io_args.question if question == "2": data = load_dataset("outliersData.pkl") X = data['X'] y = data['y'] # Fit least-squares estimator model = linear_model.LeastSquares() model.fit(X, y) print(model.w) test_and_plot(model, X, y, title="Least Squares", filename="least_squares_outliers.pdf") elif question == "2.1": data = load_dataset("outliersData.pkl") X = data['X'] y = data['y'] ''' YOUR CODE HERE ''' model = linear_model.WeightedLeastSquares()
def fit_next_case_num(raw, feature1, feature2, country, t, T, K): print(" ") # print("predicting for country:%s" % (country)) ca = raw[raw["country_id"] == country] #print(ca) ca_case_ts = ca[feature1].values ca_death_ts = ca[feature2].values # 280*1 #print(ca_case_ts) ca_death_ts = np.log(ca_death_ts + 1) y = ca_case_ts[K - 1:T] # size = T-K n = len(y) y = y.reshape(n, 1) X = np.ones((n, 1)) for i in range(K - 1): x_j = ca_case_ts[i:n + i].reshape(n, 1) X = np.concatenate((X, x_j), axis=1) for i in range(K - 1): x_j2 = ca_death_ts[i:n + i].reshape(n, 1) X = np.concatenate((X, x_j2), axis=1) # train model = linear_model.LeastSquares() model.fit(X, y) # print(model.w) yhat = model.predict(X) """ for i in range(len(yhat)): if ( yhat[i] - y[i] )>= 10: print(i) #plt.plot(yhat-y) #plt.plot(y) filename = os.path.join("..", "figs", "plotofyhat&y.png") print("Saving", filename) plt.savefig(filename) """ trainError = np.mean((yhat - y)**2) print("Training error = %.3f" % trainError) # T+1 X_test = np.concatenate( ([1], ca_case_ts[T - K + 1:T], ca_death_ts[T - K + 1:T]), axis=0) #.reshape(1, 2*K-1) print(X_test.shape) y_test = np.array(ca_case_ts[T]).reshape(1, 1) y_test_overall = y_test y_pred = model.predict(X_test).reshape(1, 1) # print("predicted case number: %.3f, real case number: %.3f" % (y_pred_curr, y_test)) y_pred_overall = y_pred # predict for i in range(1, t): # print("day#: %d" % (i + 1)) # new X_(T+1) #a=X_test[:,2:] #print([[1]].shape) X_test = np.concatenate(([1], ca_case_ts[T - K + i:T + i - 1], ca_death_ts[T - K + i:T + i - 1]), axis=0) print(X_test.shape) y_test_i = np.array(ca_case_ts[T + i]).reshape(1, 1) y_test_overall = np.concatenate((y_test_overall, y_test_i), axis=0) y_pred = model.predict(X_test).reshape(1, 1) print("predicted case number: %.3f, real case number: %.3f" % (y_pred, y_test_i)) y_pred_overall = np.concatenate((y_pred_overall, y_pred), axis=0) # X_test = np.concatenate((X_test, X_test_i), axis=0) # y_pred = model.predict(X_test) testError = np.mean((y_pred_overall - y_test_overall)**2) print("Test error = %.3f" % testError) return [trainError, testError]