石油苯,液化气线上bug调试

This commit is contained in:
workpc 2025-05-08 13:44:11 +08:00
parent 91abe4cfb7
commit 035b74a617
10 changed files with 492 additions and 83 deletions

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@ -10,12 +10,12 @@
"text/html": [
" <script type=\"text/javascript\">\n",
" window.PlotlyConfig = {MathJaxConfig: 'local'};\n",
" if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n",
" if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n",
" if (typeof require !== 'undefined') {\n",
" require.undef(\"plotly\");\n",
" requirejs.config({\n",
" paths: {\n",
" 'plotly': ['https://cdn.plot.ly/plotly-2.2.0.min']\n",
" 'plotly': ['https://cdn.plot.ly/plotly-2.12.1.min']\n",
" }\n",
" });\n",
" require(['plotly'], function(Plotly) {\n",
@ -196,36 +196,19 @@
" \"funcOperation\": \"新增\",\n",
" \"data\": [\n",
" {\"dataItemNo\": \"250855713|Forecast_Price|ACN\",\n",
" \"dataDate\": date,\n",
" \"dataDate\": getNow(date=date)[0],\n",
" \"dataStatus\": \"add\",\n",
" \"dataValue\": forecast_price()\n",
" }\n",
"\n",
" ]\n",
" }\n",
" # headers = {\"Authorization\": token_push}\n",
" # res = requests.post(url=upload_url, headers=headers, json=data, timeout=(3, 5))\n",
" # print(res.text)\n",
" headers = {\"Authorization\": token_push}\n",
" res = requests.post(url=upload_url, headers=headers, json=data, timeout=(3, 5))\n",
" print(res.text)\n",
" print('预测值:',data['data'][0]['dataValue'])\n",
"\n",
" \n",
"# def upload_data_to_system(token):\n",
"# data = {\n",
"# \"funcModule\": \"数据表信息列表\",\n",
"# \"funcOperation\": \"新增\",\n",
"# \"data\": [\n",
"# {\"dataItemNo\": \"C01100036|Forecast_ Price|ACN\",\n",
"# \"dataDate\": '20230706',\n",
"# \"dataStatus\": \"add\",\n",
"# \"dataValue\": 3780.0\n",
"# }\n",
"\n",
"# ]\n",
"# }\n",
"# headers = {\"Authorization\": token}\n",
"# res = requests.post(url=upload_url, headers=headers, json=data, timeout=(3, 5))\n",
"# print(res.text)\n",
"\n",
"price_list = []\n",
" \n",
"def forecast_price():\n",
@ -698,7 +681,7 @@
" # 更新当日数据\n",
" start(date)\n",
" # 训练模型\n",
" optimize_Model()\n",
" # optimize_Model()\n",
" # 预测&上传预测结果\n",
" upload_data_to_system(token_push,start_date)\n",
"\n"
@ -717,15 +700,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"运行中ing...\n"
]
}
],
"outputs": [],
"source": [
"\n",
"if __name__ == \"__main__\":\n",
@ -734,16 +709,17 @@
" while True:\n",
" # 获取当前时间\n",
" current_time = time.strftime(\"%H:%M:%S\", time.localtime())\n",
" try:\n",
" # try:\n",
" # 判断当前时间是否为执行任务的时间点\n",
" if current_time == \"09:15:00\":\n",
" print(\"执行定时任务\")\n",
" main()\n",
" elif current_time == \"20:00:00\":\n",
" start_1()\n",
" time.sleep(1)\n",
" except:\n",
" print(f\"{current_time}执行失败\")\n",
" if current_time == \"09:15:00\":\n",
" # if current_time == \"17:12:00\":\n",
" print(\"执行定时任务\")\n",
" main()\n",
" elif current_time == \"20:00:00\":\n",
" start_1()\n",
" time.sleep(1)\n",
" # except:\n",
" # print(f\"{current_time}执行失败\")\n",
"\n",
" # 检测数据准确性, 需要检测放开\n",
" # check_data(\"100028098|LISTING_PRICE\")\n",
@ -754,10 +730,93 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"获取的token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhcGlfZGV2IiwidGgiOiI4YTQ1NzdkYmQ5MTk2NzU3NThkNTc5OTlhMWU4OTFmZSIsImx0IjoiYXBpIiwiaXNzIjoiIiwidG0iOiJQQyIsImV4cCI6MTc0NjY0NTcwMCwianRpIjoiODRhODFmYmZjYTUxNGM4ZGIzOGFiZTVlM2U0ZWFhYmUifQ.Vu5gbh0s1TMd3NE6T89l0h67TQaf3r_TDHvO_lZsIcs\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\EDY\\AppData\\Local\\Temp\\ipykernel_2516\\268695125.py:602: DeprecationWarning:\n",
"\n",
"The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"当月数据更新完成\n",
"20250507\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\EDY\\AppData\\Local\\Temp\\ipykernel_2516\\268695125.py:602: DeprecationWarning:\n",
"\n",
"The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"当月数据更新完成\n",
"获取到的数据项ID['YHQMXBB|C01100008|STRIKE_PRICE', 'C01100008|CORTED_VALUE', 'C01100008|AUCTION_MAX_PRICE', 'C01100008|AMOUNT', 'C01100008|YEDAY_AMOUNT', '100028046|LISTING_PRICE', 'ICE_CL0_LAST_YEDAY_PRICE', '91370200163576944B|C01100008|STRIKE_PRICE', '9137078672073757X8|C01100008|STRIKE_PRICE', '91370500674526498A|C01100008|STRIKE_PRICE', '91370305773165341A|C01100008|STRIKE_PRICE', '91370521164880008P|C01100008|STRIKE_PRICE', '91370321164425136B|C01100008|STRIKE_PRICE', 'SD|GC|ZDW|LIST_PRICE', '370500|ISOBUTANE|LIST_PRICE', 'SD|YT|SG|LIST_PRICE', '91110000710926094P|C01100008|SUPPLY_MERE', '91110000710932515R|C01100008|SUPPLY_MERE', '91370500674526498A|C01100008|SUPPLY_MERE', '91370321164425136B|C01100008|SUPPLY_MERE', 'C01100008|OTHER|SUPPLY_MERE', 'SD|WJH|DEMANDS', 'C01100008|SUY_DED_DAP', 'C01100008|EFFECTIVE_STOCK', '912102117169477344|C01100008|STRIKE_PRICE', '91110304102767480H|C01100008|STRIKE_PRICE', '91130193670310403L|C01100008|STRIKE_PRICE', 'HD|LPG|IMPORT_PRICE', 'SD|WJH|SALES_PRICE']\n",
"获取的token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhcGlfZGV2IiwidGgiOiI4YTQ1NzdkYmQ5MTk2NzU3NThkNTc5OTlhMWU4OTFmZSIsImx0IjoiYXBpIiwiaXNzIjoiIiwidG0iOiJQQyIsImV4cCI6MTc0NjY0NTcwOCwianRpIjoiNDFjODI0ZjQ0MDIzNDZlZmI5NjY5NTcwZGE3MmZiYmYifQ.OUIHg0JlPstctzFKJ__8Qqo7oZuxedssnRJlUYXujtc\n",
"获取20250507数据\n",
"数据项查询参数search_data\n",
"{'data': {'date': '20250507', 'dataItemNoList': ['YHQMXBB|C01100008|STRIKE_PRICE', 'C01100008|CORTED_VALUE', 'C01100008|AUCTION_MAX_PRICE', 'C01100008|AMOUNT', 'C01100008|YEDAY_AMOUNT', '100028046|LISTING_PRICE', 'ICE_CL0_LAST_YEDAY_PRICE', '91370200163576944B|C01100008|STRIKE_PRICE', '9137078672073757X8|C01100008|STRIKE_PRICE', '91370500674526498A|C01100008|STRIKE_PRICE', '91370305773165341A|C01100008|STRIKE_PRICE', '91370521164880008P|C01100008|STRIKE_PRICE', '91370321164425136B|C01100008|STRIKE_PRICE', 'SD|GC|ZDW|LIST_PRICE', '370500|ISOBUTANE|LIST_PRICE', 'SD|YT|SG|LIST_PRICE', '91110000710926094P|C01100008|SUPPLY_MERE', '91110000710932515R|C01100008|SUPPLY_MERE', '91370500674526498A|C01100008|SUPPLY_MERE', '91370321164425136B|C01100008|SUPPLY_MERE', 'C01100008|OTHER|SUPPLY_MERE', 'SD|WJH|DEMANDS', 'C01100008|SUY_DED_DAP', 'C01100008|EFFECTIVE_STOCK', '912102117169477344|C01100008|STRIKE_PRICE', '91110304102767480H|C01100008|STRIKE_PRICE', '91130193670310403L|C01100008|STRIKE_PRICE', 'HD|LPG|IMPORT_PRICE', 'SD|WJH|SALES_PRICE']}, 'funcModule': '数据项', 'funcOperation': '查询'}\n",
"数据项查询结果search_res\n",
"{\"confirmFlg\":false,\"data\":[{\"dataDate\":\"20250507\",\"dataItemNo\":\"91110000710926094P|C01100008|SUPPLY_MERE\",\"dataValue\":1300.00000000},{\"dataDate\":\"20250507\",\"dataItemNo\":\"91110000710932515R|C01100008|SUPPLY_MERE\"},{\"dataDate\":\"20250507\",\"dataItemNo\":\"91370321164425136B|C01100008|STRIKE_PRICE\",\"dataValue\":4750.00000000},{\"dataDate\":\"20250507\",\"dataItemNo\":\"91370321164425136B|C01100008|SUPPLY_MERE\",\"dataValue\":300.00000000},{\"dataDate\":\"20250507\",\"dataItemNo\":\"91370500674526498A|C01100008|SUPPLY_MERE\",\"dataValue\":175.00000000},{\"dataDate\":\"20250507\",\"dataItemNo\":\"91370521164880008P|C01100008|STRIKE_PRICE\",\"dataValue\":4805.00000000},{\"dataDate\":\"20250507\",\"dataItemNo\":\"C01100008|AUCTION_MAX_PRICE\",\"dataValue\":4770.00000000},{\"dataDate\":\"20250507\",\"dataItemNo\":\"C01100008|CORTED_VALUE\",\"dataValue\":4770.00000000},{\"dataDate\":\"20250507\",\"dataItemNo\":\"C01100008|OTHER|SUPPLY_MERE\",\"dataValue\":5000.00000000},{\"dataDate\":\"20250507\",\"dataItemNo\":\"C01100008|SUY_DED_DAP\",\"dataValue\":-50.00000000},{\"dataDate\":\"20250507\",\"dataItemNo\":\"ICE_CL0_LAST_YEDAY_PRICE\",\"dataValue\":62.04000000},{\"dataDate\":\"20250507\",\"dataItemNo\":\"SD|WJH|DEMANDS\",\"dataValue\":8500.00000000},{\"dataDate\":\"20250507\",\"dataItemNo\":\"SD|WJH|SALES_PRICE\",\"dataValue\":7830.00000000},{\"dataDate\":\"20250507\",\"dataItemNo\":\"YHQMXBB|C01100008|STRIKE_PRICE\",\"dataValue\":4770.00000000}],\"status\":true}\n",
"数据项查询结果: [{'dataDate': '20250507', 'dataItemNo': '91110000710926094P|C01100008|SUPPLY_MERE', 'dataValue': 1300.0}, {'dataDate': '20250507', 'dataItemNo': '91110000710932515R|C01100008|SUPPLY_MERE'}, {'dataDate': '20250507', 'dataItemNo': '91370321164425136B|C01100008|STRIKE_PRICE', 'dataValue': 4750.0}, {'dataDate': '20250507', 'dataItemNo': '91370321164425136B|C01100008|SUPPLY_MERE', 'dataValue': 300.0}, {'dataDate': '20250507', 'dataItemNo': '91370500674526498A|C01100008|SUPPLY_MERE', 'dataValue': 175.0}, {'dataDate': '20250507', 'dataItemNo': '91370521164880008P|C01100008|STRIKE_PRICE', 'dataValue': 4805.0}, {'dataDate': '20250507', 'dataItemNo': 'C01100008|AUCTION_MAX_PRICE', 'dataValue': 4770.0}, {'dataDate': '20250507', 'dataItemNo': 'C01100008|CORTED_VALUE', 'dataValue': 4770.0}, {'dataDate': '20250507', 'dataItemNo': 'C01100008|OTHER|SUPPLY_MERE', 'dataValue': 5000.0}, {'dataDate': '20250507', 'dataItemNo': 'C01100008|SUY_DED_DAP', 'dataValue': -50.0}, {'dataDate': '20250507', 'dataItemNo': 'ICE_CL0_LAST_YEDAY_PRICE', 'dataValue': 62.04}, {'dataDate': '20250507', 'dataItemNo': 'SD|WJH|DEMANDS', 'dataValue': 8500.0}, {'dataDate': '20250507', 'dataItemNo': 'SD|WJH|SALES_PRICE', 'dataValue': 7830.0}, {'dataDate': '20250507', 'dataItemNo': 'YHQMXBB|C01100008|STRIKE_PRICE', 'dataValue': 4770.0}]\n",
"{'dataDate': '20250507', 'dataItemNo': '91110000710932515R|C01100008|SUPPLY_MERE'}\n",
"添加的行: ['2025-05-07', 4770.0, 4770.0, 4770.0, '', '', '', 62.04, '', '', '', '', 4805.0, 4750.0, '', '', '', 1300.0, '', 175.0, 300.0, 5000.0, 8500.0, -50.0, '', '', '', '', '', 7830.0]\n",
"更新 2025-05-07 数据\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\EDY\\AppData\\Local\\Temp\\ipykernel_2516\\268695125.py:186: UserWarning:\n",
"\n",
"The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.\n",
"\n",
"C:\\Users\\EDY\\AppData\\Local\\Temp\\ipykernel_2516\\268695125.py:188: UserWarning:\n",
"\n",
"The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.\n",
"\n",
"C:\\Users\\EDY\\AppData\\Local\\Temp\\ipykernel_2516\\268695125.py:220: FutureWarning:\n",
"\n",
"Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Date\n",
"2025-05-07 4769.817871\n",
"Name: 日度预测价格, dtype: float32\n",
"{\"confirmFlg\":false,\"status\":true}\n",
"预测值: 4769.82\n"
]
}
],
"source": [
"# start_date = datetime(2025, 4, 1)\n",
"# end_date = datetime(2025, 5, 7)\n",
"# start_date = datetime(2025, 5, 7)\n",
"# end_date = datetime(2025, 5, 8)\n",
"# token = get_head_auth()\n",
"\n",
"# while start_date < end_date:\n",
@ -783,7 +842,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "base",
"language": "python",
"name": "python3"
},
@ -797,7 +856,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
"version": "3.11.7"
}
},
"nbformat": 4,

View File

@ -5,27 +5,17 @@
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\statsmodels\\compat\\pandas.py:49: FutureWarning:\n",
"\n",
"The Panel class is removed from pandas. Accessing it from the top-level namespace will also be removed in the next version\n",
"\n"
]
},
{
"data": {
"text/html": [
" <script type=\"text/javascript\">\n",
" window.PlotlyConfig = {MathJaxConfig: 'local'};\n",
" if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n",
" if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n",
" if (typeof require !== 'undefined') {\n",
" require.undef(\"plotly\");\n",
" requirejs.config({\n",
" paths: {\n",
" 'plotly': ['https://cdn.plot.ly/plotly-2.2.0.min']\n",
" 'plotly': ['https://cdn.plot.ly/plotly-2.12.1.min']\n",
" }\n",
" });\n",
" require(['plotly'], function(Plotly) {\n",
@ -43,7 +33,106 @@
"name": "stdout",
"output_type": "stream",
"text": [
"运行中ing\n"
"运行中ing\n",
"执行定时任务\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\EDY\\AppData\\Local\\Temp\\ipykernel_22664\\2912906777.py:771: DeprecationWarning:\n",
"\n",
"The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"获取到的数据项ID['数据项编码', 'C01100047|STRIKE_PRICE', 'Brentspj', '913716251671540959|EXCHANGE_RATE', 'C01100010|LIST_PRICE01', '250326561|STRIKE_PRICE', 'C01100047|LIST_PRICE', 'C01100047|LIST_PRICE-1', 'C01100047|LIST_PRICE-01', 'OIL_CHEM|guonei|6097|PRICE', '91370500674526498A|C01100008|STRIKE_PRICE', '91370783724809024G|BEN|PRICE', '91370500737223620X|BEN|PRICE', '91370503706169019D|BEN|PRICE', '91370503164840647R|BEN|PRICE', 'C01100047|TURNOVER', '913705221649223519|C01100047|EXW', 'C01100047|CAPACITY']\n",
"获取到的数据项ID['C01100047|STRIKE_PRICE', 'Brentspj', '913716251671540959|EXCHANGE_RATE', 'C01100010|LIST_PRICE01', '250326561|STRIKE_PRICE', 'C01100047|LIST_PRICE', 'C01100047|LIST_PRICE-1', 'C01100047|LIST_PRICE-01', 'OIL_CHEM|guonei|6097|PRICE', '91370500674526498A|C01100008|STRIKE_PRICE', '91370783724809024G|BEN|PRICE', '91370500737223620X|BEN|PRICE', '91370503706169019D|BEN|PRICE', '91370503164840647R|BEN|PRICE', 'C01100047|TURNOVER', '913705221649223519|C01100047|EXW', 'C01100047|CAPACITY']\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\EDY\\AppData\\Local\\Temp\\ipykernel_22664\\2912906777.py:771: DeprecationWarning:\n",
"\n",
"The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
"\n",
"C:\\Users\\EDY\\AppData\\Local\\Temp\\ipykernel_22664\\2912906777.py:320: UserWarning:\n",
"\n",
"The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.\n",
"\n",
"d:\\ProgramData\\anaconda3\\Lib\\site-packages\\sklearn\\linear_model\\_coordinate_descent.py:631: ConvergenceWarning:\n",
"\n",
"Objective did not converge. You might want to increase the number of iterations, check the scale of the features or consider increasing regularisation. Duality gap: 6.681e+05, tolerance: 4.960e+04\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using matplotlib backend: QtAgg\n",
"%pylab is deprecated, use %matplotlib inline and import the required libraries.\n",
"Populating the interactive namespace from numpy and matplotlib\n",
"Fitting 3 folds for each of 180 candidates, totalling 540 fits\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\ProgramData\\anaconda3\\Lib\\site-packages\\IPython\\core\\magics\\pylab.py:162: UserWarning:\n",
"\n",
"pylab import has clobbered these variables: ['__version__', 'datetime', 'plot', 'random']\n",
"`%matplotlib` prevents importing * from pylab and numpy\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Date\n",
"2025-05-07 5427.172363\n",
"Name: 日度预测价格, dtype: float32\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\EDY\\AppData\\Local\\Temp\\ipykernel_22664\\2912906777.py:273: UserWarning:\n",
"\n",
"The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.\n",
"\n",
"C:\\Users\\EDY\\AppData\\Local\\Temp\\ipykernel_22664\\2912906777.py:303: FutureWarning:\n",
"\n",
"Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'funcModule': '数据表信息列表', 'funcOperation': '新增', 'data': [{'dataItemNo': 'C01100047|FORECAST_PRICE', 'dataDate': '20250507', 'dataStatus': 'add', 'dataValue': 5427.17}]}\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[3], line 834\u001b[0m\n\u001b[0;32m 832\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m更新数据\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 833\u001b[0m start_1()\n\u001b[1;32m--> 834\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m1\u001b[39m)\n",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
]
}
],
@ -246,6 +335,9 @@
"def get_cur_time(date=''):\n",
" if date == '':\n",
" now = datetime.now()\n",
" # 如果是字符串,尝试解析日期\n",
" elif isinstance(date, str):\n",
" now = datetime.strptime(date, '%Y-%m-%d')\n",
" else:\n",
" now = date\n",
" year = now.year\n",
@ -280,7 +372,7 @@
" \"funcOperation\": \"新增\",\n",
" \"data\": [\n",
" {\"dataItemNo\": \"C01100047|FORECAST_PRICE\",\n",
" \"dataDate\": get_cur_time(date)[0],\n",
" \"dataDate\": date,\n",
" \"dataStatus\": \"add\",\n",
" \"dataValue\": datavalue\n",
" }\n",
@ -292,25 +384,7 @@
" res = requests.post(url=upload_url, headers=headers, json=data, timeout=(3, 5))\n",
" print(res.text)\n",
"\n",
" \n",
"# def upload_data_to_system(token):\n",
"# data = {\n",
"# \"funcModule\": \"数据表信息列表\",\n",
"# \"funcOperation\": \"新增\",\n",
"# \"data\": [\n",
"# {\"dataItemNo\": \"C01100036|Forecast_ Price|ACN\",\n",
"# \"dataDate\": '20230706',\n",
"# \"dataStatus\": \"add\",\n",
"# \"dataValue\": 3780.0\n",
"# }\n",
"\n",
"# ]\n",
"# }\n",
"# headers = {\"Authorization\": token}\n",
"# res = requests.post(url=upload_url, headers=headers, json=data, timeout=(3, 5))\n",
"# print(res.text)\n",
"\n",
" \n",
" \n",
" \n",
"def forecast_price():\n",
" # df_test = pd.read_csv('定价模型数据收集0212.csv')\n",
@ -916,7 +990,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "base",
"language": "python",
"name": "python3"
},
@ -930,7 +1004,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
"version": "3.11.7"
}
},
"nbformat": 4,

236
models/xgboostmodels.py Normal file
View File

@ -0,0 +1,236 @@
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.feature_selection import SelectFromModel
from sklearn.preprocessing import OrdinalEncoder
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
import pandas as pd
from sklearn.linear_model import Lasso
from xgboost import XGBRegressor
import statsmodels.api as sm
import plotly.express as px
import plotly.graph_objects as go
import xgboost as xgb
from xgboost import plot_importance, plot_tree
from sklearn.metrics import mean_absolute_error
from statsmodels.tools.eval_measures import mse, rmse
from sklearn.model_selection import GridSearchCV
import warnings
import pickle
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
import numpy as np
from matplotlib import pyplot as plt
import os
from matplotlib import rcParams
rcParams['font.sans-serif'] = ['SimHei'] # Windows系统自带的中文字体
rcParams['axes.unicode_minus'] = False # 解决负号显示问题
pd.set_option('display.max_rows', 40)
pd.set_option('display.max_columns', 40)
class xgboostmodels():
def __init__(self, df, steps=3):
self.df = df
self.steps = steps
self.features = []
def optimize_Model(self, df):
# 生成滞后特征示例使用3期滞后
steps = self.steps
for i in range(1, steps+1):
df[f'lag_{i}'] = df['y'].shift(i)
# 生成多步预测目标示例预测未来3步
forecast_horizon = steps
for i in range(forecast_horizon):
df[f'target_{i+1}'] = df['y'].shift(-(i+1))
df = df.dropna() # 删除包含NaN的行
# 更新特征和目标变量
features = [col for col in df.columns if col.startswith('lag')]
targets = [col for col in df.columns if col.startswith('target')]
X = df[features]
y = df[targets]
# 按时间顺序分割数据集(时间序列不能随机分割)
split = int(0.8 * len(df))
X_train, x_test = X.iloc[:split], X.iloc[split:]
y_train, y_test = y.iloc[:split], y.iloc[split:]
# 修改模型为多输出回归
from sklearn.multioutput import MultiOutputRegressor
base_model = XGBRegressor(random_state=0)
estimator = MultiOutputRegressor(base_model) # 保持多输出包装器
# 模型参数网格需要调整为适用于多输出回归器
parameters = {
'estimator__max_depth': range(2, 11, 2), # 注意参数前缀
'estimator__n_estimators': range(50, 101, 10),
'estimator__learning_rate': [0.01, 0.03, 0.1, 0.3, 0.5, 1]
}
grid_search_XGB = GridSearchCV(
estimator=estimator,
param_grid=parameters,
cv=3,
verbose=True,
scoring='neg_mean_squared_error' # 使用更适合时序的评估指标
)
grid_search_XGB.fit(X_train, y_train)
# 优化后的结果展示
print("最佳参数组合:")
best_params = {}
for param, value in grid_search_XGB.best_params_.items():
best_params[param.split('__')[1]] = value
print(f"{param}: {value}")
best_params_df = pd.DataFrame(
[best_params], index=["最佳参数组合"])
y_pred = grid_search_XGB.predict(x_test)
# 多步预测评估指标
evaluation_results = []
for step in range(forecast_horizon):
mse = mean_squared_error(y_test.iloc[:, step], y_pred[:, step])
rmse = np.sqrt(mse)
r2 = r2_score(y_test.iloc[:, step], y_pred[:, step])
evaluation_results.append({
'预测步长': step+1,
'均方根误差(RMSE)': rmse,
'R²分数': r2
})
# 生成多步预测结果对比表
results_df = pd.DataFrame(evaluation_results).set_index('预测步长')
print("\n多步预测性能评估:")
print(results_df)
# 保存完整模型(包含多输出包装器)
Pkl_Filename = "report\日度价格预测_最佳模型.pkl"
with open(Pkl_Filename, 'wb') as file:
pickle.dump(grid_search_XGB.best_estimator_, file)
return results_df, best_params_df
def forecast_price(self, df):
steps = self.steps
import joblib
Best_model_DalyLGPrice = joblib.load(
"report\日度价格预测_最佳模型.pkl")
df = df.copy()
df.set_index('ds', inplace=True)
# 生成滞后特征(与训练时保持一致)
for i in range(1, steps+1):
df[f'lag_{i}'] = df['y'].shift(i)
# 获取特征列(保持与训练时相同的特征顺序)
features = [f'lag_{i}' for i in range(1, steps+1)]
# 初始化预测序列保留最后3个已知值作为初始输入
current_values = df[features][-1:].values # 初始形状 (1,3)
predictions = Best_model_DalyLGPrice.predict(current_values)[0]
# 生成带日期的预测结果
last_date = df.index[-1]
date_range = pd.date_range(
start=last_date + pd.Timedelta(days=1),
periods=steps
)
# 保存预测结果到DataFrame
forecast_series = pd.Series(
predictions, index=date_range.strftime('%Y-%m-%d'), name='预测值')
# 拼接原始数据和预测结果
df = pd.concat([df[['y']], forecast_series], axis=0)
# 绘制预测结果
plt.figure(figsize=(12, 6))
n_points = min(len(df), 30) # 自动适配数据量
plt.plot(df.index[-n_points:], df['y'].iloc[-n_points:],
label=f'原始数据(近{n_points}天)', marker='o', markersize=4)
plt.plot(forecast_series.index, forecast_series.values,
label=f'预测值 (未来{steps}天)', linestyle='--', marker='x')
plt.axvline(x=last_date, color='red', linestyle=':',
label='当前日期分界线', alpha=0.7)
plt.title('价格预测趋势')
plt.xlabel('日期')
plt.ylabel('价格')
plt.legend()
plt.grid(True)
# 自动旋转日期标签
plt.gcf().autofmt_xdate()
# 确保保存目录存在
os.makedirs("report", exist_ok=True)
plt.savefig("report/价格预测.png", dpi=300, bbox_inches='tight')
plt.close()
return forecast_series
def generate_xgboost_report(self, model_results, best_params_df, forecast_series,
save_path="report/xgboost_report.md"):
"""
生成XGBoost模型报告
参数:
model_results: 模型对比结果DataFrame
evaluation_df: 多步评估结果DataFrame
forecast_series: 预测结果Series
save_path: 报告保存路径
"""
# 构建Markdown内容
md_content = f"""
# XGBoost价格预测分析报告 <span style="color:gray; font-size:0.8em">{pd.Timestamp.now().strftime('%Y-%m-%d')}</span>
## 一、模型实现
### 1. 特征工程
- 使用{len(model_results)}期历史滞后特征
- 预测未来{len(model_results)}个时间步长
- 数据集分割比例80% 训练集 / 20% 测试集
### 2. 模型配置
{best_params_df.to_markdown()}
## 二、性能评估
### 多步预测误差分析
{model_results.to_markdown()}
## 三、预测结果
### 未来{len(forecast_series)}日价格预测
![预测结果](价格预测.png)
{forecast_series.to_markdown()}
"""
# 保存报告
os.makedirs(os.path.dirname(save_path), exist_ok=True)
with open(save_path, 'w', encoding='utf-8') as f:
f.write(md_content)
print(f"报告已生成至:{os.path.abspath(save_path)}")
if __name__ == '__main__':
df = pd.read_csv(
"D:\code\PriceForecast-svn\yuanyoudataset\指标数据.csv")
df['ds'] = pd.to_datetime(df['ds'])
xg = xgboostmodels(df=df, steps=5)
model_results, best_params_df = xg.optimize_Model(df=df)
forecast_series = xg.forecast_price(df)
xg.generate_xgboost_report(
model_results=model_results,
best_params_df=best_params_df,
forecast_series=forecast_series
)

40
report/xgboost_report.md Normal file
View File

@ -0,0 +1,40 @@
# XGBoost 价格预测分析报告 <span style="color:gray; font-size:0.8em">2025-05-08</span>
## 一、模型实现
### 1. 特征工程
- 使用 5 期历史滞后特征
- 预测未来 10 个时间步长
- 数据集分割比例80% 训练集 / 20% 测试集
### 2. 模型配置
| | learning_rate | max_depth | n_estimators |
| :----------- | ------------: | --------: | -----------: |
| 最佳参数组合 | 1 | 4 | 100 |
## 二、性能评估
### 多步预测误差分析
| 预测步长 | 均方根误差(RMSE) | R² 分数 |
| -------: | ---------------: | -------: |
| 1 | 2.30044 | 0.828193 |
| 2 | 2.74815 | 0.756595 |
| 3 | 3.16346 | 0.679378 |
| 4 | 3.62202 | 0.58126 |
| 5 | 3.77657 | 0.546517 |
## 三、预测结果
### 未来 5 日价格预测
![预测结果](价格预测.png)
| | 预测值 |
|:--------------------|---------:|
| 2025-03-14 00:00:00 | 68.8125 |
| 2025-03-15 00:00:00 | 68.6643 |
| 2025-03-16 00:00:00 | 69.283 |
| 2025-03-17 00:00:00 | 71.7288 |
| 2025-03-18 00:00:00 | 68.8356 |

Binary file not shown.

Binary file not shown.