diff --git a/API/__pycache__/app.cpython-310.pyc b/API/__pycache__/app.cpython-310.pyc index ed06d17969dd11b823a4e3e47489627a8cbf771a..e0cb02c71747bd6c7c0edfdb70161572f9f8e08a 100644 Binary files a/API/__pycache__/app.cpython-310.pyc and b/API/__pycache__/app.cpython-310.pyc differ diff --git a/API/run.py b/API/run.py index 8b37567de7b79ffcae908852f06862f03f6e0464..27caf22fe4b32836afcf53f4327528f1b7ebf032 100644 --- a/API/run.py +++ b/API/run.py @@ -1,3 +1,23 @@ +# import libraries +import numpy as np +import pandas as pd +import matplotlib.pyplot as plt +import seaborn as sns +from collections import Counter +import statistics +import math + +#for LSTM model +from sklearn.preprocessing import MinMaxScaler +from keras.models import Sequential +from keras.layers import Dense, LSTM, Dropout + +# ignore warnings +import warnings +warnings.filterwarnings("ignore") + +from datetime import datetime as dt + import pymysql import json from app import app @@ -22,322 +42,47 @@ plt.rcParams.update({'figure.figsize':(9,7), 'figure.dpi':120}) import pmdarima as pm -app.config['SECRET_KEY']='a1b1' - -@app.route('/predict',methods=['POST']) -def predict(): - - data=request.get_json() - - return model1(data) - -def model1(income_data): - - X=[] - Y=[] - - data_arr=json.dumps(income_data) - din=json.loads(data_arr) - for i in din: - X.append(i) - Y.append(din[i]) - - data_time = np.asarray(X) - data_count = np.asarray(Y) - - from sklearn.model_selection import train_test_split - X_Train, X_Test, Y_Train, Y_Test = train_test_split(X, Y, test_size = 1/3, random_state = 0) - - df = pd.DataFrame({'time': data_time, 'count': data_count}) - - - df.time = pd.to_datetime(df.time) - - regr = linear_model.LinearRegression() - regr.fit(df.time.values.reshape(-1, 1), df['count'].values.reshape(-1, 1)) - - y_pred = regr.predict(df.time.values.astype(float).reshape(-1, 1)) - df['pred'] = y_pred - - print(df) - - - df = df.set_index(data_time) - - out=df.to_json(orient='columns') - - y = json.loads(out) - - return jsonify(y['pred']) - - - - -@app.route('/m1',methods=['POST']) -def m1(): - - data=request.get_json() - - return model1(data) - -def model1(income_data): - - X=[] - Y=[] - - data_arr=json.dumps(income_data) - din=json.loads(data_arr) - for i in din: - X.append(i) - Y.append(din[i]) - - data_time = np.asarray(X) - data_count = np.asarray(Y) - - - - from sklearn.model_selection import train_test_split - X_Train, X_Test, Y_Train, Y_Test = train_test_split(X, Y, test_size = 1/3, random_state = 0) - - df = pd.DataFrame({'time': data_time, 'count': data_count}) - - - df.time = pd.to_datetime(df.time) - - regr = linear_model.LinearRegression() - - regr.fit(df.time.values.reshape(-1, 1), df['count'].values.reshape(-1, 1)) - - y_pred = regr.predict(df.time.values.astype(float).reshape(-1, 1)) - df['pre'] = y_pred - - print(df) - - - df = df.set_index(data_time) - - out=df.to_json(orient='columns') - - y = json.loads(out) - - ax = plt.gca() - df.plot(kind='line',x='time',y='count',color='blue',ax=ax) - - df.plot(kind='line',x='time',y='pre',color='red',ax=ax) - plt.show() - - return jsonify(y['pre']) - - -@app.route('/m2',methods=['POST']) -def m2(): - - data=request.get_json() - - return model2(data) - -def model2(income_data): - - X=[] - Y=[] - - data_arr=json.dumps(income_data) - din=json.loads(data_arr) - for i in din: - X.append(i) - Y.append(din[i]) - - data_time = np.asarray(X) - data_count = np.asarray(Y) - - - df = pd.DataFrame({'time': data_time, 'count': data_count}) - df.time = pd.to_datetime(df.time) - - - - - model = LorentzianModel() - params = model.guess(df['count'], x=df.time.values.astype(float)) - result = model.fit(df['count'], params, x=df.time.values.astype(float)) - result.plot_fit() - plt.show() - - - y = np.array(result.best_fit) - df['pre'] =y - df = df.set_index(data_time) - print(result.fit_report()) - out=df.to_json(orient='columns') - - y = json.loads(out) - - - - return jsonify(y['pre']) - - - - -@app.route('/p1',methods=['POST']) -def p1(): - - data=request.get_json() - - return model2(data) - -def model2(income_data): - - X=[] - Y=[] - - data_arr=json.dumps(income_data) - din=json.loads(data_arr) - for i in din: - X.append(i) - Y.append(din[i]) - - - - data_time = np.asarray(X) - data_count = np.asarray(Y) - - data = pd.DataFrame({'date': data_time, 'value': data_count}) - data.date = pd.to_datetime(data.date) - data.set_index(data_time) - - - csv_data = data.to_csv('test2.csv', index=False) - print(csv_data) - - data = pd.read_csv('test2.csv', parse_dates=['date'], index_col='date') - - # Seasonal - fit stepwise auto-ARIMA - smodel = pm.auto_arima(data, start_p=1, start_q=1, - test='adf', - max_p=3, max_q=3, m=12, - start_P=0, seasonal=True, - d=None, D=1, trace=True, - error_action='ignore', - suppress_warnings=True, - stepwise=True) - - smodel.summary() - - # Forecast - n_periods =5 - fitted, confint = smodel.predict(n_periods=n_periods, return_conf_int=True) - index_of_fc = pd.date_range(data.index[-1], periods = n_periods, freq='MS') - - # make series for plotting purpose - fitted_series = pd.Series(fitted, index=index_of_fc) - lower_series = pd.Series(confint[:, 0], index=index_of_fc) - upper_series = pd.Series(confint[:, 1], index=index_of_fc) - - - - dy = fitted_series.to_frame() - - - - - print(dy) - - out=dy.to_json(orient='index',date_format='iso') - - result = json.loads(out) - - print(out) - - return jsonify(result) - - - - -@app.route('/p2',methods=['POST']) -def p2(): - - data=request.get_json() - - return model2(data) - -def model2(income_data): +@app.route('/start',methods=['POST']) - X=[] - Y=[] +def start(): - data_arr=json.dumps(income_data) - din=json.loads(data_arr) - for i in din: - X.append(i) - Y.append(din[i]) - - + data=request.get_json() - data_time = np.asarray(X) - data_count = np.asarray(Y) - - data = pd.DataFrame({'date': data_time, 'value': data_count}) - data.date = pd.to_datetime(data.date) - data.set_index(data_time) - - csv_data = data.to_csv('test2.csv', index=False) - print(csv_data) + return data - data = pd.read_csv('test2.csv', parse_dates=['date'], index_col='date') - # Seasonal - fit stepwise auto-ARIMA - smodel = pm.auto_arima(data, start_p=1, start_q=1, - test='adf', - max_p=3, max_q=3, m=12, - start_P=0, seasonal=True, - d=None, D=1, trace=True, - error_action='ignore', - suppress_warnings=True, - stepwise=True) +def read_json(income_data): - smodel.summary() + X=[] + Y=[] - # Forecast - n_periods =5 - fitted, confint = smodel.predict(n_periods=n_periods, return_conf_int=True) - index_of_fc = pd.date_range(data.index[-1], periods = n_periods, freq='MS') + data_arr=json.dumps(income_data) + din=json.loads(data_arr) + for i in din: + X.append(i) + Y.append(din[i]) + + data_time = np.asarray(X) + data_count = np.asarray(Y) - # make series for plotting purpose - fitted_series = pd.Series(fitted, index=index_of_fc) - lower_series = pd.Series(confint[:, 0], index=index_of_fc) - upper_series = pd.Series(confint[:, 1], index=index_of_fc) - - - - # Plot - plt.plot(data) - plt.plot(fitted_series, color='darkgreen') - plt.fill_between(lower_series.index, - lower_series, - upper_series, - color='k', alpha=.15) + return "json" - plt.title("Final Forecast") - plt.show() +@app.route('/tranning',methods=['POST']) - dy = fitted_series.to_frame() - - - - - print(dy) +def tranning(): + return "tranning" - out=dy.to_json(orient='index',date_format='iso') - result = json.loads(out) +@app.route('/prediction',methods=['POST']) - print(out) - - return jsonify(result) +def prediction(): + return "prediction" +@app.route('/optimize',methods=['POST']) +def optimize(): + return "optimize" if __name__ == "__main__": app.run() \ No newline at end of file diff --git a/API/.idea/Prediction_api.iml b/API_bk/.idea/Prediction_api.iml similarity index 100% rename from API/.idea/Prediction_api.iml rename to API_bk/.idea/Prediction_api.iml diff --git a/API/.idea/inspectionProfiles/profiles_settings.xml b/API_bk/.idea/inspectionProfiles/profiles_settings.xml similarity index 100% rename from API/.idea/inspectionProfiles/profiles_settings.xml rename to API_bk/.idea/inspectionProfiles/profiles_settings.xml diff --git a/API/.idea/misc.xml b/API_bk/.idea/misc.xml similarity index 100% rename from API/.idea/misc.xml rename to API_bk/.idea/misc.xml diff --git a/API/.idea/modules.xml b/API_bk/.idea/modules.xml similarity index 100% rename from API/.idea/modules.xml rename to API_bk/.idea/modules.xml diff --git a/API/.idea/workspace.xml b/API_bk/.idea/workspace.xml similarity index 100% rename from API/.idea/workspace.xml rename to API_bk/.idea/workspace.xml diff --git a/API/1D_json.txt b/API_bk/1D_json.txt similarity index 100% rename from API/1D_json.txt rename to API_bk/1D_json.txt diff --git a/API/JwtApi.py b/API_bk/JwtApi.py similarity index 100% rename from API/JwtApi.py rename to API_bk/JwtApi.py diff --git a/API/JwtApi.wsgi b/API_bk/JwtApi.wsgi similarity index 100% rename from API/JwtApi.wsgi rename to API_bk/JwtApi.wsgi diff --git a/API/__init__.py b/API_bk/__init__.py similarity index 100% rename from API/__init__.py rename to API_bk/__init__.py diff --git a/API_bk/__pycache__/app.cpython-310.pyc b/API_bk/__pycache__/app.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ed06d17969dd11b823a4e3e47489627a8cbf771a Binary files /dev/null and b/API_bk/__pycache__/app.cpython-310.pyc differ diff --git a/API/__pycache__/app.cpython-36.pyc b/API_bk/__pycache__/app.cpython-36.pyc similarity index 100% rename from API/__pycache__/app.cpython-36.pyc rename to API_bk/__pycache__/app.cpython-36.pyc diff --git a/API/__pycache__/app.cpython-38.pyc b/API_bk/__pycache__/app.cpython-38.pyc similarity index 100% rename from API/__pycache__/app.cpython-38.pyc rename to API_bk/__pycache__/app.cpython-38.pyc diff --git a/API/__pycache__/db_config.cpython-310.pyc b/API_bk/__pycache__/db_config.cpython-310.pyc similarity index 100% rename from API/__pycache__/db_config.cpython-310.pyc rename to API_bk/__pycache__/db_config.cpython-310.pyc diff --git a/API/__pycache__/db_config.cpython-36.pyc b/API_bk/__pycache__/db_config.cpython-36.pyc similarity index 100% rename from API/__pycache__/db_config.cpython-36.pyc rename to API_bk/__pycache__/db_config.cpython-36.pyc diff --git a/API/__pycache__/db_config.cpython-38.pyc b/API_bk/__pycache__/db_config.cpython-38.pyc similarity index 100% rename from API/__pycache__/db_config.cpython-38.pyc rename to API_bk/__pycache__/db_config.cpython-38.pyc diff --git a/API/__pycache__/run.cpython-310.pyc b/API_bk/__pycache__/run.cpython-310.pyc similarity index 100% rename from API/__pycache__/run.cpython-310.pyc rename to API_bk/__pycache__/run.cpython-310.pyc diff --git a/API/__pycache__/run.cpython-38.pyc b/API_bk/__pycache__/run.cpython-38.pyc similarity index 100% rename from API/__pycache__/run.cpython-38.pyc rename to API_bk/__pycache__/run.cpython-38.pyc diff --git a/API/__pycache__/views.cpython-310.pyc b/API_bk/__pycache__/views.cpython-310.pyc similarity index 100% rename from API/__pycache__/views.cpython-310.pyc rename to API_bk/__pycache__/views.cpython-310.pyc diff --git a/API/__pycache__/views.cpython-38.pyc b/API_bk/__pycache__/views.cpython-38.pyc similarity index 100% rename from API/__pycache__/views.cpython-38.pyc rename to API_bk/__pycache__/views.cpython-38.pyc diff --git a/API/api.py b/API_bk/api.py similarity index 100% rename from API/api.py rename to API_bk/api.py diff --git a/API_bk/app.py b/API_bk/app.py new file mode 100644 index 0000000000000000000000000000000000000000..7232d4ace11b3923eb78839920e3e65ba7823774 --- /dev/null +++ b/API_bk/app.py @@ -0,0 +1,3 @@ +from flask import Flask + +app = Flask(__name__) \ No newline at end of file diff --git a/API/app.pyc b/API_bk/app.pyc similarity index 100% rename from API/app.pyc rename to API_bk/app.pyc diff --git a/API/back.py b/API_bk/back.py similarity index 100% rename from API/back.py rename to API_bk/back.py diff --git a/API/best_fit.py b/API_bk/best_fit.py similarity index 100% rename from API/best_fit.py rename to API_bk/best_fit.py diff --git a/API/dae.csv b/API_bk/dae.csv similarity index 100% rename from API/dae.csv rename to API_bk/dae.csv diff --git a/API/data.csv b/API_bk/data.csv similarity index 100% rename from API/data.csv rename to API_bk/data.csv diff --git a/API/data.xlsx b/API_bk/data.xlsx similarity index 100% rename from API/data.xlsx rename to API_bk/data.xlsx diff --git a/API/db_config.py b/API_bk/db_config.py similarity index 100% rename from API/db_config.py rename to API_bk/db_config.py diff --git a/API/db_config.pyc b/API_bk/db_config.pyc similarity index 100% rename from API/db_config.pyc rename to API_bk/db_config.pyc diff --git a/API/json.txt b/API_bk/json.txt similarity index 100% rename from API/json.txt rename to API_bk/json.txt diff --git a/API/models.py b/API_bk/models.py similarity index 100% rename from API/models.py rename to API_bk/models.py diff --git a/API/p2.py b/API_bk/p2.py similarity index 100% rename from API/p2.py rename to API_bk/p2.py diff --git a/API/p3.py b/API_bk/p3.py similarity index 100% rename from API/p3.py rename to API_bk/p3.py diff --git a/API/p4.py b/API_bk/p4.py similarity index 100% rename from API/p4.py rename to API_bk/p4.py diff --git a/API/resources.py b/API_bk/resources.py similarity index 100% rename from API/resources.py rename to API_bk/resources.py diff --git a/API/run._oopy b/API_bk/run._oopy similarity index 100% rename from API/run._oopy rename to API_bk/run._oopy diff --git a/API_bk/run.py b/API_bk/run.py new file mode 100644 index 0000000000000000000000000000000000000000..8b37567de7b79ffcae908852f06862f03f6e0464 --- /dev/null +++ b/API_bk/run.py @@ -0,0 +1,343 @@ +import pymysql +import json +from app import app + +from flask import Flask, jsonify, request, make_response +from flask import flash, request + +import datetime +from werkzeug.security import generate_password_hash, check_password_hash +from functools import wraps +import requests +import numpy as np +import pandas as pd +from sklearn import linear_model +from datetime import datetime, timedelta + +from statsmodels.tsa.ar_model import AR + +from statsmodels.graphics.tsaplots import plot_acf, plot_pacf +import matplotlib.pyplot as plt +plt.rcParams.update({'figure.figsize':(9,7), 'figure.dpi':120}) +import pmdarima as pm + + +app.config['SECRET_KEY']='a1b1' + +@app.route('/predict',methods=['POST']) +def predict(): + + data=request.get_json() + + return model1(data) + +def model1(income_data): + + X=[] + Y=[] + + data_arr=json.dumps(income_data) + din=json.loads(data_arr) + for i in din: + X.append(i) + Y.append(din[i]) + + data_time = np.asarray(X) + data_count = np.asarray(Y) + + from sklearn.model_selection import train_test_split + X_Train, X_Test, Y_Train, Y_Test = train_test_split(X, Y, test_size = 1/3, random_state = 0) + + df = pd.DataFrame({'time': data_time, 'count': data_count}) + + + df.time = pd.to_datetime(df.time) + + regr = linear_model.LinearRegression() + regr.fit(df.time.values.reshape(-1, 1), df['count'].values.reshape(-1, 1)) + + y_pred = regr.predict(df.time.values.astype(float).reshape(-1, 1)) + df['pred'] = y_pred + + print(df) + + + df = df.set_index(data_time) + + out=df.to_json(orient='columns') + + y = json.loads(out) + + return jsonify(y['pred']) + + + + +@app.route('/m1',methods=['POST']) +def m1(): + + data=request.get_json() + + return model1(data) + +def model1(income_data): + + X=[] + Y=[] + + data_arr=json.dumps(income_data) + din=json.loads(data_arr) + for i in din: + X.append(i) + Y.append(din[i]) + + data_time = np.asarray(X) + data_count = np.asarray(Y) + + + + from sklearn.model_selection import train_test_split + X_Train, X_Test, Y_Train, Y_Test = train_test_split(X, Y, test_size = 1/3, random_state = 0) + + df = pd.DataFrame({'time': data_time, 'count': data_count}) + + + df.time = pd.to_datetime(df.time) + + regr = linear_model.LinearRegression() + + regr.fit(df.time.values.reshape(-1, 1), df['count'].values.reshape(-1, 1)) + + y_pred = regr.predict(df.time.values.astype(float).reshape(-1, 1)) + df['pre'] = y_pred + + print(df) + + + df = df.set_index(data_time) + + out=df.to_json(orient='columns') + + y = json.loads(out) + + ax = plt.gca() + df.plot(kind='line',x='time',y='count',color='blue',ax=ax) + + df.plot(kind='line',x='time',y='pre',color='red',ax=ax) + plt.show() + + return jsonify(y['pre']) + + +@app.route('/m2',methods=['POST']) +def m2(): + + data=request.get_json() + + return model2(data) + +def model2(income_data): + + X=[] + Y=[] + + data_arr=json.dumps(income_data) + din=json.loads(data_arr) + for i in din: + X.append(i) + Y.append(din[i]) + + data_time = np.asarray(X) + data_count = np.asarray(Y) + + + df = pd.DataFrame({'time': data_time, 'count': data_count}) + df.time = pd.to_datetime(df.time) + + + + + model = LorentzianModel() + params = model.guess(df['count'], x=df.time.values.astype(float)) + result = model.fit(df['count'], params, x=df.time.values.astype(float)) + result.plot_fit() + plt.show() + + + y = np.array(result.best_fit) + df['pre'] =y + df = df.set_index(data_time) + print(result.fit_report()) + out=df.to_json(orient='columns') + + y = json.loads(out) + + + + return jsonify(y['pre']) + + + + +@app.route('/p1',methods=['POST']) +def p1(): + + data=request.get_json() + + return model2(data) + +def model2(income_data): + + X=[] + Y=[] + + data_arr=json.dumps(income_data) + din=json.loads(data_arr) + for i in din: + X.append(i) + Y.append(din[i]) + + + + data_time = np.asarray(X) + data_count = np.asarray(Y) + + data = pd.DataFrame({'date': data_time, 'value': data_count}) + data.date = pd.to_datetime(data.date) + data.set_index(data_time) + + + csv_data = data.to_csv('test2.csv', index=False) + print(csv_data) + + data = pd.read_csv('test2.csv', parse_dates=['date'], index_col='date') + + # Seasonal - fit stepwise auto-ARIMA + smodel = pm.auto_arima(data, start_p=1, start_q=1, + test='adf', + max_p=3, max_q=3, m=12, + start_P=0, seasonal=True, + d=None, D=1, trace=True, + error_action='ignore', + suppress_warnings=True, + stepwise=True) + + smodel.summary() + + # Forecast + n_periods =5 + fitted, confint = smodel.predict(n_periods=n_periods, return_conf_int=True) + index_of_fc = pd.date_range(data.index[-1], periods = n_periods, freq='MS') + + # make series for plotting purpose + fitted_series = pd.Series(fitted, index=index_of_fc) + lower_series = pd.Series(confint[:, 0], index=index_of_fc) + upper_series = pd.Series(confint[:, 1], index=index_of_fc) + + + + dy = fitted_series.to_frame() + + + + + print(dy) + + out=dy.to_json(orient='index',date_format='iso') + + result = json.loads(out) + + print(out) + + return jsonify(result) + + + + +@app.route('/p2',methods=['POST']) +def p2(): + + data=request.get_json() + + return model2(data) + +def model2(income_data): + + X=[] + Y=[] + + data_arr=json.dumps(income_data) + din=json.loads(data_arr) + for i in din: + X.append(i) + Y.append(din[i]) + + + + data_time = np.asarray(X) + data_count = np.asarray(Y) + + data = pd.DataFrame({'date': data_time, 'value': data_count}) + data.date = pd.to_datetime(data.date) + data.set_index(data_time) + + + csv_data = data.to_csv('test2.csv', index=False) + print(csv_data) + + data = pd.read_csv('test2.csv', parse_dates=['date'], index_col='date') + + # Seasonal - fit stepwise auto-ARIMA + smodel = pm.auto_arima(data, start_p=1, start_q=1, + test='adf', + max_p=3, max_q=3, m=12, + start_P=0, seasonal=True, + d=None, D=1, trace=True, + error_action='ignore', + suppress_warnings=True, + stepwise=True) + + smodel.summary() + + # Forecast + n_periods =5 + fitted, confint = smodel.predict(n_periods=n_periods, return_conf_int=True) + index_of_fc = pd.date_range(data.index[-1], periods = n_periods, freq='MS') + + # make series for plotting purpose + fitted_series = pd.Series(fitted, index=index_of_fc) + lower_series = pd.Series(confint[:, 0], index=index_of_fc) + upper_series = pd.Series(confint[:, 1], index=index_of_fc) + + + + # Plot + plt.plot(data) + plt.plot(fitted_series, color='darkgreen') + plt.fill_between(lower_series.index, + lower_series, + upper_series, + color='k', alpha=.15) + + plt.title("Final Forecast") + plt.show() + + dy = fitted_series.to_frame() + + + + + print(dy) + + out=dy.to_json(orient='index',date_format='iso') + + result = json.loads(out) + + print(out) + + return jsonify(result) + + + +if __name__ == "__main__": + app.run() \ No newline at end of file diff --git a/API/run_bk.py b/API_bk/run_bk.py similarity index 100% rename from API/run_bk.py rename to API_bk/run_bk.py diff --git a/API/sample.csv b/API_bk/sample.csv similarity index 100% rename from API/sample.csv rename to API_bk/sample.csv diff --git a/API/stock.py b/API_bk/stock.py similarity index 100% rename from API/stock.py rename to API_bk/stock.py diff --git a/API/stock_data.csv b/API_bk/stock_data.csv similarity index 100% rename from API/stock_data.csv rename to API_bk/stock_data.csv diff --git a/API/test2.csv b/API_bk/test2.csv similarity index 100% rename from API/test2.csv rename to API_bk/test2.csv diff --git a/API/test_time.py b/API_bk/test_time.py similarity index 100% rename from API/test_time.py rename to API_bk/test_time.py diff --git a/API/views.py b/API_bk/views.py similarity index 100% rename from API/views.py rename to API_bk/views.py