{ "cells": [ { "cell_type": "code", "execution_count": 130, "id": "9daadf20-caa6-4b25-901c-6cc3ef563f58", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(50, 9)\n", "(20, 4)\n", "(50, 12)\n", " id PREDICT_DATE CREAT_DATE MIN_PRICE MAX_PRICE HIGH_PRICE_x LOW_PRICE_x \\\n", "0 1 2024-11-26 2024-11-25 71.071556 76.006900 \n", "1 2 2024-11-27 2024-11-25 71.003624 75.580560 \n", "2 3 2024-11-28 2024-11-25 72.083850 76.204260 \n", "3 4 2024-11-29 2024-11-25 71.329730 75.703950 \n", "4 5 2024-12-02 2024-11-25 71.720825 76.264275 \n", "\n", " RIGHT_ROTE ds 序号 LOW_PRICE_y HIGH_PRICE_y \n", "0 2024-11-26 9.0 71.63 73.80 \n", "1 2024-11-27 8.0 71.71 72.85 \n", "2 2024-11-28 7.0 71.85 72.96 \n", "3 2024-11-29 6.0 71.75 73.34 \n", "4 2024-12-02 5.0 71.52 72.89 \n", " id PREDICT_DATE CREAT_DATE MIN_PRICE MAX_PRICE HIGH_PRICE_x \\\n", "45 46 2024-12-09 2024-12-06 68.894770 73.909450 \n", "46 47 2024-12-10 2024-12-06 68.845604 73.921120 \n", "47 48 2024-12-11 2024-12-06 69.181210 73.906260 \n", "48 49 2024-12-12 2024-12-06 69.075340 73.862946 \n", "49 50 2024-12-13 2024-12-06 69.076550 74.007740 \n", "\n", " LOW_PRICE_x RIGHT_ROTE ds 序号 LOW_PRICE_y HIGH_PRICE_y \n", "45 2024-12-09 NaN NaN NaN \n", "46 2024-12-10 NaN NaN NaN \n", "47 2024-12-11 NaN NaN NaN \n", "48 2024-12-12 NaN NaN NaN \n", "49 2024-12-13 NaN NaN NaN \n" ] } ], "source": [ "import sqlite3\n", "import os\n", "import pandas as pd\n", "\n", "# 预测价格数据\n", "dbfilename = os.path.join(r'D:\\code\\PriceForecast\\yuanyoudataset','jbsh_yuanyou.db')\n", "conn = sqlite3.connect(dbfilename)\n", "query = 'SELECT * FROM accuracy'\n", "df1 = pd.read_sql_query(query, conn)\n", "df1['ds'] = df1['PREDICT_DATE']\n", "conn.close()\n", "print(df1.shape)\n", "\n", "\n", "\n", "\n", "# 最高最低价\n", "xlsfilename = os.path.join(r'D:\\code\\PriceForecast\\yuanyoudataset','数据项下载.xls')\n", "df2 = pd.read_excel(xlsfilename)[5:]\n", "df2 = df2.rename(columns = {'数据项名称':'ds','布伦特最低价':'LOW_PRICE','布伦特最高价':'HIGH_PRICE'})\n", "print(df2.shape)\n", "\n", "\n", "\n", "df = pd.merge(df1,df2,on=['ds'],how='left')\n", "\n", "df['ds'] = pd.to_datetime(df['ds'])\n", "df['PREDICT_DATE'] = pd.to_datetime(df['PREDICT_DATE'])\n", "df = df.reindex()\n", "\n", "print(df.shape)\n", "\n", "df.to_csv(os.path.join(r'D:\\code\\PriceForecast\\yuanyoudataset','123.csv'))\n", "# df = df[['ds','min_within_quantile','max_within_quantile']]\n", "\n", "\n", "\n", "# 打印数据框的前几行\n", "print(df.head())\n", "print(df.tail())\n" ] }, { "cell_type": "code", "execution_count": 131, "id": "e51c3fd0-6bff-45de-b8b6-971e7986c7a7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " ds ACCURACY HIGH_PRICE_y LOW_PRICE_y MIN_PRICE MAX_PRICE\n", "0 2024-11-26 1.000000 73.80 71.63 71.071556 76.006900\n", "1 2024-11-27 1.000000 72.85 71.71 71.003624 75.580560\n", "2 2024-11-28 0.789324 72.96 71.85 72.083850 76.204260\n", "3 2024-11-29 1.000000 73.34 71.75 71.329730 75.703950\n", "4 2024-12-02 0.853412 72.89 71.52 71.720825 76.264275\n", "5 2024-11-27 0.606873 72.85 71.71 72.158165 76.173650\n", "6 2024-11-28 0.802144 72.96 71.85 72.069620 76.447000\n", "7 2024-11-29 0.608239 73.34 71.75 72.372900 76.082910\n", "8 2024-12-02 0.457007 72.89 71.52 72.263900 76.198685\n", "9 2024-12-03 0.758302 73.93 71.68 72.223820 76.041725\n", "10 2024-11-28 1.000000 72.96 71.85 70.709750 75.208030\n", "11 2024-11-29 1.000000 73.34 71.75 69.923110 75.423775\n", "12 2024-12-02 1.000000 72.89 71.52 70.810356 75.786860\n", "13 2024-12-03 1.000000 73.93 71.68 70.814430 75.791115\n", "14 2024-12-04 1.000000 74.28 72.25 70.476650 75.623730\n", "15 2024-11-29 1.000000 73.34 71.75 70.432760 75.480620\n", "16 2024-12-02 1.000000 72.89 71.52 69.651825 75.270940\n", "17 2024-12-03 1.000000 73.93 71.68 70.536285 75.826430\n", "18 2024-12-04 1.000000 74.28 72.25 70.568340 75.770290\n", "19 2024-12-05 1.000000 72.92 71.80 70.222640 76.113310\n", "20 2024-12-02 0.000000 72.89 71.52 81.179990 81.179990\n", "21 2024-12-03 0.000000 73.93 71.68 80.791550 80.791550\n", "22 2024-12-04 0.000000 74.28 72.25 80.650894 80.650894\n", "23 2024-12-05 0.000000 72.92 71.80 80.298000 80.298000\n", "24 2024-12-06 0.000000 72.19 70.85 75.508380 80.264275\n", "25 2024-12-03 1.000000 73.93 71.68 69.807790 75.837190\n", "26 2024-12-04 1.000000 74.28 72.25 71.281296 76.240510\n", "27 2024-12-05 1.000000 72.92 71.80 70.526230 75.955410\n", "28 2024-12-06 1.000000 72.19 70.85 70.719320 75.997680\n", "29 2024-12-09 0.000000 NaN NaN 71.642590 75.828636\n", "30 2024-12-04 0.943207 74.28 72.25 72.365290 75.890610\n", "31 2024-12-05 0.643920 72.92 71.80 72.198810 75.729620\n", "32 2024-12-06 0.040172 72.19 70.85 72.136170 75.977620\n", "33 2024-12-09 0.000000 NaN NaN 72.165790 75.880104\n", "34 2024-12-10 0.000000 NaN NaN 72.000620 75.906100\n", "35 2024-12-05 0.000000 72.92 71.80 82.405370 82.405370\n", "36 2024-12-06 0.000000 72.19 70.85 82.072700 82.072700\n", "37 2024-12-09 0.000000 NaN NaN 82.652700 82.652700\n", "38 2024-12-10 0.000000 NaN NaN 81.920910 81.920910\n", "39 2024-12-11 0.000000 NaN NaN 81.091270 81.091270\n", "40 2024-12-06 0.000000 72.19 70.85 83.367485 83.367485\n", "41 2024-12-09 0.000000 NaN NaN 82.869705 82.869705\n", "42 2024-12-10 0.000000 NaN NaN 82.580550 82.580550\n", "43 2024-12-11 0.000000 NaN NaN 82.442154 82.442154\n", "44 2024-12-12 0.000000 NaN NaN 82.276510 82.276510\n", "45 2024-12-09 0.000000 NaN NaN 68.894770 73.909450\n", "46 2024-12-10 0.000000 NaN NaN 68.845604 73.921120\n", "47 2024-12-11 0.000000 NaN NaN 69.181210 73.906260\n", "48 2024-12-12 0.000000 NaN NaN 69.075340 73.862946\n", "49 2024-12-13 0.000000 NaN NaN 69.076550 74.007740\n" ] } ], "source": [ "# 定义一个函数来计算准确率\n", "def calculate_accuracy(row):\n", " if row['HIGH_PRICE_y'] > row['MIN_PRICE']:\n", " sorted_prices = sorted([row['LOW_PRICE_y'], row['MIN_PRICE'], row['MAX_PRICE'], row['HIGH_PRICE_y']])\n", " middle_diff = sorted_prices[2] - sorted_prices[1]\n", " price_range = row['HIGH_PRICE_y'] - row['LOW_PRICE_y']\n", " accuracy = middle_diff / price_range\n", " return accuracy\n", " else:\n", " return 0\n", "\n", "# 使用 apply 函数来应用计算准确率的函数\n", "\n", "columns = ['HIGH_PRICE_y','LOW_PRICE_y','MIN_PRICE','MAX_PRICE']\n", "df[columns] = df[columns].astype(float)\n", "df['ACCURACY'] = df.apply(calculate_accuracy, axis=1)\n", "\n", "\n", "# 打印结果\n", "print(df[['ds','ACCURACY',]+columns])\n", "\n", "df = df[['ds','ACCURACY','PREDICT_DATE','CREAT_DATE']+columns]" ] }, { "cell_type": "code", "execution_count": 132, "id": "0f942c69", "metadata": {}, "outputs": [], "source": [ "import datetime\n", "# ds 按周取\n", "df['Ds_Week'] = df['ds'].apply(lambda x: x.strftime('%U'))\n", "df['Pre_Week'] = df['PREDICT_DATE'].apply(lambda x: x.strftime('%U'))" ] }, { "cell_type": "code", "execution_count": 133, "id": "a7b05510", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | ds | \n", "ACCURACY | \n", "PREDICT_DATE | \n", "CREAT_DATE | \n", "HIGH_PRICE_y | \n", "LOW_PRICE_y | \n", "MIN_PRICE | \n", "MAX_PRICE | \n", "Ds_Week | \n", "Pre_Week | \n", "
---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "2024-11-26 | \n", "1.000000 | \n", "2024-11-26 | \n", "2024-11-25 | \n", "73.80 | \n", "71.63 | \n", "71.071556 | \n", "76.006900 | \n", "47 | \n", "47 | \n", "
1 | \n", "2024-11-27 | \n", "1.000000 | \n", "2024-11-27 | \n", "2024-11-25 | \n", "72.85 | \n", "71.71 | \n", "71.003624 | \n", "75.580560 | \n", "47 | \n", "47 | \n", "
2 | \n", "2024-11-28 | \n", "0.789324 | \n", "2024-11-28 | \n", "2024-11-25 | \n", "72.96 | \n", "71.85 | \n", "72.083850 | \n", "76.204260 | \n", "47 | \n", "47 | \n", "
3 | \n", "2024-11-29 | \n", "1.000000 | \n", "2024-11-29 | \n", "2024-11-25 | \n", "73.34 | \n", "71.75 | \n", "71.329730 | \n", "75.703950 | \n", "47 | \n", "47 | \n", "
4 | \n", "2024-12-02 | \n", "0.853412 | \n", "2024-12-02 | \n", "2024-11-25 | \n", "72.89 | \n", "71.52 | \n", "71.720825 | \n", "76.264275 | \n", "48 | \n", "48 | \n", "
5 | \n", "2024-11-27 | \n", "0.606873 | \n", "2024-11-27 | \n", "2024-11-26 | \n", "72.85 | \n", "71.71 | \n", "72.158165 | \n", "76.173650 | \n", "47 | \n", "47 | \n", "
6 | \n", "2024-11-28 | \n", "0.802144 | \n", "2024-11-28 | \n", "2024-11-26 | \n", "72.96 | \n", "71.85 | \n", "72.069620 | \n", "76.447000 | \n", "47 | \n", "47 | \n", "
7 | \n", "2024-11-29 | \n", "0.608239 | \n", "2024-11-29 | \n", "2024-11-26 | \n", "73.34 | \n", "71.75 | \n", "72.372900 | \n", "76.082910 | \n", "47 | \n", "47 | \n", "
8 | \n", "2024-12-02 | \n", "0.457007 | \n", "2024-12-02 | \n", "2024-11-26 | \n", "72.89 | \n", "71.52 | \n", "72.263900 | \n", "76.198685 | \n", "48 | \n", "48 | \n", "
9 | \n", "2024-12-03 | \n", "0.758302 | \n", "2024-12-03 | \n", "2024-11-26 | \n", "73.93 | \n", "71.68 | \n", "72.223820 | \n", "76.041725 | \n", "48 | \n", "48 | \n", "
10 | \n", "2024-11-28 | \n", "1.000000 | \n", "2024-11-28 | \n", "2024-11-27 | \n", "72.96 | \n", "71.85 | \n", "70.709750 | \n", "75.208030 | \n", "47 | \n", "47 | \n", "
11 | \n", "2024-11-29 | \n", "1.000000 | \n", "2024-11-29 | \n", "2024-11-27 | \n", "73.34 | \n", "71.75 | \n", "69.923110 | \n", "75.423775 | \n", "47 | \n", "47 | \n", "
12 | \n", "2024-12-02 | \n", "1.000000 | \n", "2024-12-02 | \n", "2024-11-27 | \n", "72.89 | \n", "71.52 | \n", "70.810356 | \n", "75.786860 | \n", "48 | \n", "48 | \n", "
13 | \n", "2024-12-03 | \n", "1.000000 | \n", "2024-12-03 | \n", "2024-11-27 | \n", "73.93 | \n", "71.68 | \n", "70.814430 | \n", "75.791115 | \n", "48 | \n", "48 | \n", "
14 | \n", "2024-12-04 | \n", "1.000000 | \n", "2024-12-04 | \n", "2024-11-27 | \n", "74.28 | \n", "72.25 | \n", "70.476650 | \n", "75.623730 | \n", "48 | \n", "48 | \n", "
15 | \n", "2024-11-29 | \n", "1.000000 | \n", "2024-11-29 | \n", "2024-11-28 | \n", "73.34 | \n", "71.75 | \n", "70.432760 | \n", "75.480620 | \n", "47 | \n", "47 | \n", "
16 | \n", "2024-12-02 | \n", "1.000000 | \n", "2024-12-02 | \n", "2024-11-28 | \n", "72.89 | \n", "71.52 | \n", "69.651825 | \n", "75.270940 | \n", "48 | \n", "48 | \n", "
17 | \n", "2024-12-03 | \n", "1.000000 | \n", "2024-12-03 | \n", "2024-11-28 | \n", "73.93 | \n", "71.68 | \n", "70.536285 | \n", "75.826430 | \n", "48 | \n", "48 | \n", "
18 | \n", "2024-12-04 | \n", "1.000000 | \n", "2024-12-04 | \n", "2024-11-28 | \n", "74.28 | \n", "72.25 | \n", "70.568340 | \n", "75.770290 | \n", "48 | \n", "48 | \n", "
19 | \n", "2024-12-05 | \n", "1.000000 | \n", "2024-12-05 | \n", "2024-11-28 | \n", "72.92 | \n", "71.80 | \n", "70.222640 | \n", "76.113310 | \n", "48 | \n", "48 | \n", "
20 | \n", "2024-12-02 | \n", "0.000000 | \n", "2024-12-02 | \n", "2024-11-29 | \n", "72.89 | \n", "71.52 | \n", "81.179990 | \n", "81.179990 | \n", "48 | \n", "48 | \n", "
21 | \n", "2024-12-03 | \n", "0.000000 | \n", "2024-12-03 | \n", "2024-11-29 | \n", "73.93 | \n", "71.68 | \n", "80.791550 | \n", "80.791550 | \n", "48 | \n", "48 | \n", "
22 | \n", "2024-12-04 | \n", "0.000000 | \n", "2024-12-04 | \n", "2024-11-29 | \n", "74.28 | \n", "72.25 | \n", "80.650894 | \n", "80.650894 | \n", "48 | \n", "48 | \n", "
23 | \n", "2024-12-05 | \n", "0.000000 | \n", "2024-12-05 | \n", "2024-11-29 | \n", "72.92 | \n", "71.80 | \n", "80.298000 | \n", "80.298000 | \n", "48 | \n", "48 | \n", "
24 | \n", "2024-12-06 | \n", "0.000000 | \n", "2024-12-06 | \n", "2024-11-29 | \n", "72.19 | \n", "70.85 | \n", "75.508380 | \n", "80.264275 | \n", "48 | \n", "48 | \n", "
25 | \n", "2024-12-03 | \n", "1.000000 | \n", "2024-12-03 | \n", "2024-12-02 | \n", "73.93 | \n", "71.68 | \n", "69.807790 | \n", "75.837190 | \n", "48 | \n", "48 | \n", "
26 | \n", "2024-12-04 | \n", "1.000000 | \n", "2024-12-04 | \n", "2024-12-02 | \n", "74.28 | \n", "72.25 | \n", "71.281296 | \n", "76.240510 | \n", "48 | \n", "48 | \n", "
27 | \n", "2024-12-05 | \n", "1.000000 | \n", "2024-12-05 | \n", "2024-12-02 | \n", "72.92 | \n", "71.80 | \n", "70.526230 | \n", "75.955410 | \n", "48 | \n", "48 | \n", "
28 | \n", "2024-12-06 | \n", "1.000000 | \n", "2024-12-06 | \n", "2024-12-02 | \n", "72.19 | \n", "70.85 | \n", "70.719320 | \n", "75.997680 | \n", "48 | \n", "48 | \n", "
29 | \n", "2024-12-09 | \n", "0.000000 | \n", "2024-12-09 | \n", "2024-12-02 | \n", "NaN | \n", "NaN | \n", "71.642590 | \n", "75.828636 | \n", "49 | \n", "49 | \n", "
30 | \n", "2024-12-04 | \n", "0.943207 | \n", "2024-12-04 | \n", "2024-12-03 | \n", "74.28 | \n", "72.25 | \n", "72.365290 | \n", "75.890610 | \n", "48 | \n", "48 | \n", "
31 | \n", "2024-12-05 | \n", "0.643920 | \n", "2024-12-05 | \n", "2024-12-03 | \n", "72.92 | \n", "71.80 | \n", "72.198810 | \n", "75.729620 | \n", "48 | \n", "48 | \n", "
32 | \n", "2024-12-06 | \n", "0.040172 | \n", "2024-12-06 | \n", "2024-12-03 | \n", "72.19 | \n", "70.85 | \n", "72.136170 | \n", "75.977620 | \n", "48 | \n", "48 | \n", "
33 | \n", "2024-12-09 | \n", "0.000000 | \n", "2024-12-09 | \n", "2024-12-03 | \n", "NaN | \n", "NaN | \n", "72.165790 | \n", "75.880104 | \n", "49 | \n", "49 | \n", "
34 | \n", "2024-12-10 | \n", "0.000000 | \n", "2024-12-10 | \n", "2024-12-03 | \n", "NaN | \n", "NaN | \n", "72.000620 | \n", "75.906100 | \n", "49 | \n", "49 | \n", "
35 | \n", "2024-12-05 | \n", "0.000000 | \n", "2024-12-05 | \n", "2024-12-04 | \n", "72.92 | \n", "71.80 | \n", "82.405370 | \n", "82.405370 | \n", "48 | \n", "48 | \n", "
36 | \n", "2024-12-06 | \n", "0.000000 | \n", "2024-12-06 | \n", "2024-12-04 | \n", "72.19 | \n", "70.85 | \n", "82.072700 | \n", "82.072700 | \n", "48 | \n", "48 | \n", "
37 | \n", "2024-12-09 | \n", "0.000000 | \n", "2024-12-09 | \n", "2024-12-04 | \n", "NaN | \n", "NaN | \n", "82.652700 | \n", "82.652700 | \n", "49 | \n", "49 | \n", "
38 | \n", "2024-12-10 | \n", "0.000000 | \n", "2024-12-10 | \n", "2024-12-04 | \n", "NaN | \n", "NaN | \n", "81.920910 | \n", "81.920910 | \n", "49 | \n", "49 | \n", "
39 | \n", "2024-12-11 | \n", "0.000000 | \n", "2024-12-11 | \n", "2024-12-04 | \n", "NaN | \n", "NaN | \n", "81.091270 | \n", "81.091270 | \n", "49 | \n", "49 | \n", "
40 | \n", "2024-12-06 | \n", "0.000000 | \n", "2024-12-06 | \n", "2024-12-05 | \n", "72.19 | \n", "70.85 | \n", "83.367485 | \n", "83.367485 | \n", "48 | \n", "48 | \n", "
41 | \n", "2024-12-09 | \n", "0.000000 | \n", "2024-12-09 | \n", "2024-12-05 | \n", "NaN | \n", "NaN | \n", "82.869705 | \n", "82.869705 | \n", "49 | \n", "49 | \n", "
42 | \n", "2024-12-10 | \n", "0.000000 | \n", "2024-12-10 | \n", "2024-12-05 | \n", "NaN | \n", "NaN | \n", "82.580550 | \n", "82.580550 | \n", "49 | \n", "49 | \n", "
43 | \n", "2024-12-11 | \n", "0.000000 | \n", "2024-12-11 | \n", "2024-12-05 | \n", "NaN | \n", "NaN | \n", "82.442154 | \n", "82.442154 | \n", "49 | \n", "49 | \n", "
44 | \n", "2024-12-12 | \n", "0.000000 | \n", "2024-12-12 | \n", "2024-12-05 | \n", "NaN | \n", "NaN | \n", "82.276510 | \n", "82.276510 | \n", "49 | \n", "49 | \n", "
45 | \n", "2024-12-09 | \n", "0.000000 | \n", "2024-12-09 | \n", "2024-12-06 | \n", "NaN | \n", "NaN | \n", "68.894770 | \n", "73.909450 | \n", "49 | \n", "49 | \n", "
46 | \n", "2024-12-10 | \n", "0.000000 | \n", "2024-12-10 | \n", "2024-12-06 | \n", "NaN | \n", "NaN | \n", "68.845604 | \n", "73.921120 | \n", "49 | \n", "49 | \n", "
47 | \n", "2024-12-11 | \n", "0.000000 | \n", "2024-12-11 | \n", "2024-12-06 | \n", "NaN | \n", "NaN | \n", "69.181210 | \n", "73.906260 | \n", "49 | \n", "49 | \n", "
48 | \n", "2024-12-12 | \n", "0.000000 | \n", "2024-12-12 | \n", "2024-12-06 | \n", "NaN | \n", "NaN | \n", "69.075340 | \n", "73.862946 | \n", "49 | \n", "49 | \n", "
49 | \n", "2024-12-13 | \n", "0.000000 | \n", "2024-12-13 | \n", "2024-12-06 | \n", "NaN | \n", "NaN | \n", "69.076550 | \n", "74.007740 | \n", "49 | \n", "49 | \n", "