from flask import Flask, request, jsonify import numpy as np import pandas as pd import statsmodels.api as sm from datetime import datetime app = Flask(name) @app.route('/time_series_analysis', methods=['POST']) def time_series_analysis(): # 解析请求体中的参数 arr0 = ['2019/1', '2019/2', '2019/3', '2019/4', '2019/5', '2019/6', '2019/7', '2019/8', '2019/9', '2019/10', '2019/11', '2019/12', '2020/1', '2020/2', '2020/3', '2020/4', '2020/5', '2020/6', '2020/7', '2020/8', '2020/9', '2020/10', '2020/11', '2020/12'] date_arr = [] for date_str in arr0: date_obj = datetime.strptime(date_str, '%Y/%m') date_arr.append(date_obj.timestamp()) arr1 = np.array(request.json['data'], dtype=float) data_array = np.vstack((date_arr, arr1)).T.astype(float) df = pd.DataFrame(data_array, columns=['x', 'y']) df = df.dropna() acf, q, p = sm.tsa.acf(df['y'], nlags=20, qstat=True) if (p < 0.05).any(): short_term_dependency = True else: short_term_dependency = False acf, q, p = sm.tsa.acf(df['y'], nlags=20, fft=True, qstat=True) if (p < 0.05).any(): periodicity = True else: periodicity = False adf_result = sm.tsa.stattools.adfuller(df['y']) if adf_result[1] < 0.05: stationary = True else: stationary = False res = sm.tsa.seasonal_decompose(df['y'], model='additive', period=12) if np.isnan(res.seasonal).any(): seasonality = False else: seasonality = True # 返回分析结果 result = { 'short_term_dependency': short_term_dependency, 'periodicity': periodicity, 'stationary': stationary, 'seasonality': seasonality, 'recommendations': 'arima擅长处理平稳数据,其他数据处理效果也很好\nlightGBM擅长处理短期依赖型,非周期性的数据\nLSTM擅长处理长期依赖的数据\nTNC擅长处理没有明显的周期性或季节性变化,但是可能存在趋势和周期性的波动的数据\nRNN由于具有记忆能力,可以处理所有数据' } return jsonify(result) if name == 'main': app.run(),做修改能显示出实际的接口网站
时间: 2023-11-24 09:07:00 浏览: 106
基于Flask的Python全国招聘岗位就业可视化系统源码+项目说明.zip
你需要将name改为__name__,并修改app.run()为app.run(debug=True)。这样可以启动调试模式,方便查看错误信息。同时,你需要确保已经安装了Flask和其他依赖库。最后,在运行程序之前,确保端口没有被占用。以下是修改后的代码:
```
from flask import Flask, request, jsonify
import numpy as np
import pandas as pd
import statsmodels.api as sm
from datetime import datetime
app = Flask(__name__)
@app.route('/time_series_analysis', methods=['POST'])
def time_series_analysis():
# 解析请求体中的参数
arr0 = ['2019/1', '2019/2', '2019/3', '2019/4', '2019/5', '2019/6', '2019/7', '2019/8', '2019/9', '2019/10', '2019/11', '2019/12', '2020/1', '2020/2', '2020/3', '2020/4', '2020/5', '2020/6', '2020/7', '2020/8', '2020/9', '2020/10', '2020/11', '2020/12']
date_arr = []
for date_str in arr0:
date_obj = datetime.strptime(date_str, '%Y/%m')
date_arr.append(date_obj.timestamp())
arr1 = np.array(request.json['data'], dtype=float)
data_array = np.vstack((date_arr, arr1)).T.astype(float)
df = pd.DataFrame(data_array, columns=['x', 'y'])
df = df.dropna()
acf, q, p = sm.tsa.acf(df['y'], nlags=20, qstat=True)
if (p < 0.05).any():
short_term_dependency = True
else:
short_term_dependency = False
acf, q, p = sm.tsa.acf(df['y'], nlags=20, fft=True, qstat=True)
if (p < 0.05).any():
periodicity = True
else:
periodicity = False
adf_result = sm.tsa.stattools.adfuller(df['y'])
if adf_result[1] < 0.05:
stationary = True
else:
stationary = False
res = sm.tsa.seasonal_decompose(df['y'], model='additive', period=12)
if np.isnan(res.seasonal).any():
seasonality = False
else:
seasonality = True
# 返回分析结果
result = {
'short_term_dependency': short_term_dependency,
'periodicity': periodicity,
'stationary': stationary,
'seasonality': seasonality,
'recommendations': 'arima擅长处理平稳数据,其他数据处理效果也很好\nlightGBM擅长处理短期依赖型,非周期性的数据\nLSTM擅长处理长期依赖的数据\nTNC擅长处理没有明显的周期性或季节性变化,但是可能存在趋势和周期性的波动的数据\nRNN由于具有记忆能力,可以处理所有数据'
}
return jsonify(result)
if __name__ == '__main__':
app.run(debug=True)
```
阅读全文