from flask import Flask, jsonify, request import pandas as pd import numpy as np import statsmodels.api as sm from datetime import datetime app = Flask(__name__) @app.route('/option0', methods=['POST']) def option0(): 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.form['data'].split(), 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 = '时间序列具有短期依赖性' else: short_term_dependency = '时间序列没有短期依赖性' acf, q, p = sm.tsa.acf(df['y'], nlags=20, fft=True, qstat=True) if (p < 0.05).any(): has_periodicity = '时间序列具有周期性' else: has_periodicity = '时间序列没有周期性' adf_result = sm.tsa.stattools.adfuller(df['y']) if adf_result[1] < 0.05: is_stationary = '时间序列是平稳的' else: is_stationary = '时间序列不是平稳的' res = sm.tsa.seasonal_decompose(df['y'], model='additive', period=12) if np.isnan(res.seasonal).any(): has_seasonality = '时间序列没有明显的季节性变化' else: has_seasonality = '时间序列存在季节性变化' result = { print(short_term_dependency, has_periodicity, is_stationary, has_seasonality) } return jsonify(result),如何修改才能正常运行
时间: 2023-11-24 12:06:31 浏览: 97
有几个问题需要修改:
1. `print` 函数不应该出现在字典中,应该直接将结果保存在字典中。
2. 在 `jsonify` 函数中,字典的键和值之间应该是冒号 `:`,而不是逗号 `,`。
3. `if (p < 0.05).any():` 这一行代码中,括号的位置应该调整为 `if (p < 0.05).any():`,否则会出现语法错误。
修改后的代码如下所示:
```
from flask import Flask, jsonify, request
import pandas as pd
import numpy as np
import statsmodels.api as sm
from datetime import datetime
app = Flask(__name__)
@app.route('/option0', methods=['POST'])
def option0():
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.form['data'].split(), 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 = '时间序列具有短期依赖性'
else:
short_term_dependency = '时间序列没有短期依赖性'
acf, q, p = sm.tsa.acf(df['y'], nlags=20, fft=True, qstat=True)
if (p < 0.05).any():
has_periodicity = '时间序列具有周期性'
else:
has_periodicity = '时间序列没有周期性'
adf_result = sm.tsa.stattools.adfuller(df['y'])
if adf_result[1] < 0.05:
is_stationary = '时间序列是平稳的'
else:
is_stationary = '时间序列不是平稳的'
res = sm.tsa.seasonal_decompose(df['y'], model='additive', period=12)
if np.isnan(res.seasonal).any():
has_seasonality = '时间序列没有明显的季节性变化'
else:
has_seasonality = '时间序列存在季节性变化'
result = {
'short_term_dependency': short_term_dependency,
'has_periodicity': has_periodicity,
'is_stationary': is_stationary,
'has_seasonality': has_seasonality
}
return jsonify(result)
```
阅读全文