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(input("请输入连续24个月的配件销售数据,元素之间用空格隔开:").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(): print('时间序列具有短期依赖性') else: print('时间序列没有短期依赖性') acf, q, p = sm.tsa.acf(df['y'], nlags=20, fft=True, qstat=True) if (p < 0.05).any(): print('时间序列具有周期性') else: print('时间序列没有周期性') adf_result = sm.tsa.stattools.adfuller(df['y']) if adf_result[1] < 0.05: print('时间序列是平稳的') else: print('时间序列不是平稳的') res = sm.tsa.seasonal_decompose(df['y'], model='additive', period=12) if np.isnan(res.seasonal).any(): print('时间序列没有明显的季节性变化') else: print('时间序列存在季节性变化') pass,python def函数用web接口进行封装
时间: 2023-08-08 11:06:10 浏览: 185
你可以使用 Flask 框架将该函数封装成 Web 接口。具体步骤如下:
1. 安装 Flask:
```
pip install flask
```
2. 导入必要的库:
```
from flask import Flask, request, jsonify
import numpy as np
import pandas as pd
import statsmodels.api as sm
from datetime import datetime
```
3. 创建 Flask 应用:
```
app = Flask(__name__)
```
4. 定义接口:
```
@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 = 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():
has_periodicity = True
else:
has_periodicity = False
adf_result = sm.tsa.stattools.adfuller(df['y'])
if adf_result[1] < 0.05:
is_stationary = True
else:
is_stationary = False
res = sm.tsa.seasonal_decompose(df['y'], model='additive', period=12)
if np.isnan(res.seasonal).any():
has_seasonality = False
else:
has_seasonality = True
result = {
'short_term_dependency': short_term_dependency,
'has_periodicity': has_periodicity,
'is_stationary': is_stationary,
'has_seasonality': has_seasonality
}
return jsonify(result)
```
5. 启动应用:
```
if __name__ == '__main__':
app.run()
```
6. 调用接口:
使用 POST 方法向 `http://127.0.0.1:5000/option0` 发送数据,数据格式如下:
```
data=1.0 2.0 3.0 ... 24.0
```
其中,`1.0 2.0 3.0 ... 24.0` 表示 24 个月的销售数据,以空格分隔。服务端会返回一个 JSON 对象,包含判断结果。
注意,这里只是一个简单的示例,实际应用需要根据业务需求进行修改和完善。
阅读全文