|
2威望
侄子学校选修作业,遇到难题,求大神帮忙
要实现8训练1验证1测试最大的问题是输出结果是张图片,没法直接调用生成的函数。
{
"cells": [
{
"cell_type": "code",
"execution_count": 110,
"id": "fd97c8f0",
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"from torch import nn\n",
"import numpy as np\n",
"from torch.utils import data\n",
"import pandas as pd\n",
"import statsmodels.api as sm #导包\n",
"\n",
"\n",
"sales = pd.read_csv('C:\\\\Users\\Administrator\\Desktop\\sales.csv') #导入数据\n",
"sales['date_time'] = pd.to_datetime(sales['date_time']) #将日期转化为标准格式\n",
"sales['date_time'] = sales['date_time'].apply(lambda x : x.strftime(\"%Y%m%d\")).apply(lambda x:int(x))#将日期转化为整数类型\n",
"sales[\"day\"] = sales[\"day\"].map({\"Monday\": 1, \"Tuesday\": 2,\"Wednesday\":3, \"Thursday\": 4,\"Friday\": 5,\"Saturday\":6,\"Sunday\":7})\n",
"#将day强制转化为整数类型\n",
"\n",
"\n",
"w = sales.iloc[:,1]\n",
"l = len(w)\n",
"w = np.array(w.values)\n",
"w = torch.from_numpy(w).reshape(l,1) #第几周\n",
"\n",
"dt = sales.iloc[:,2]\n",
"dt = np.array(dt.values)\n",
"dt = torch.from_numpy(dt).reshape(l,1) #具体日期\n",
"\n",
"dy = sales.iloc[:,3]\n",
"dy = np.array(dy.values)\n",
"dy = torch.from_numpy(dy).reshape(l,1) #周几\n",
"\n",
"y = sales.iloc[:,4]\n",
"y = np.array(y.values)\n",
"y = torch.from_numpy(y).reshape(l,1) #日销量\n",
"\n",
"'''\n",
"xd1第几周(适用于“日”),xw1第几周(适用于“周”),x2星期几,x3上周平均,x4第几月,x5上月平均,x6是否为高发期(是1否0),\n",
"y1日总数,y2周平均,y3月平均\n",
"'''\n",
"xd1, xw1, x2, x3, x4, x5, x6, y1, y2, y3 =[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]\n",
"xd1 = torch.tensor(xd1)\n",
"xw1 = torch.tensor(xw1)\n",
"x2 = torch.tensor(x2)\n",
"x3 = torch.tensor(x3)\n",
"x4 = torch.tensor(x4)\n",
"x5 = torch.tensor(x5)\n",
"x6 = torch.tensor(x6)\n",
"y1 = torch.tensor(y1)\n",
"y2 = torch.tensor(y2)\n",
"y3 = torch.tensor(y3)\n",
"\n",
"#日\n",
"x2 = dy\n",
"y1 = y\n",
"v = 1\n",
"for i in range(l-1):\n",
" if w[i] == w[i+1]:\n",
" b = torch.tensor(v)\n",
" b = np.expand_dims(b,0)\n",
" b = torch.from_numpy(b)\n",
" xd1 = torch.cat((xd1,b), dim=0)\n",
" else:\n",
" v = v + 1\n",
" b = torch.tensor(v)\n",
" b = np.expand_dims(b,0)\n",
" b = torch.from_numpy(b)\n",
" xd1 = torch.cat((xd1,b), dim=0)\n",
"xd1 = xd1[0:len(xd1)].reshape(len(xd1),1)\n",
"\n",
"#周\n",
"v = 0\n",
"for i in range(l-1):\n",
" if w[i] != w[i+1]:\n",
" b = w[i]\n",
" xw1 = torch.cat((xw1,b), dim=0)\n",
" c = y[v:i+1].sum()/(i+1-v)\n",
" c = np.expand_dims(c,0)\n",
" c = torch.from_numpy(c)\n",
" y2 = torch.cat((y2,c), dim=0)\n",
" v = i+1 \n",
"xw1 = torch.arange(len(xw1)).reshape(len(xw1),1)\n",
"xw1 = xw1[2:len(xw1)]\n",
"\n",
"x3 = y2[1:len(y2)-1].reshape(len(y2)-2,1)\n",
"y2 = y2[2:len(y2)].reshape(len(y2)-2,1)\n",
"\n",
"#月\n",
"temp1,temp2 = [100],[1]\n",
"temp1,temp2 = torch.tensor(temp1),torch.tensor(temp2)\n",
"\n",
"v = 0\n",
"b = 0\n",
"for i in range(l-1):\n",
" if dt[i+1]-dt[i]>=30:\n",
" b = b + 1\n",
" c = y[v:i+1].sum()/(i+1-v)\n",
" c = np.expand_dims(c,0)\n",
" c = torch.from_numpy(c)\n",
" y3 = torch.cat((y3,c), dim=0)\n",
" v = i+1 \n",
"\n",
"x4 = torch.arange(b+2)\n",
"x4 = x4[2:b+2].reshape(b,1)\n",
"\n",
"x5 = y3[1:b+1].reshape(b,1)\n",
"y3 = y3[1:b+2].reshape(b,1)\n",
"\n",
"for i in range(len(x4)):\n",
" if x4[i]%12>=5 and x4[i]%12<=11:\n",
" t = torch.tensor(0)\n",
" t = np.expand_dims(t,0)\n",
" t = torch.from_numpy(t)\n",
" x6 = torch.cat((x6,t), dim=0)\n",
" else:\n",
" t = torch.tensor(1)\n",
" t = np.expand_dims(t,0)\n",
" t = torch.from_numpy(t)\n",
" x6 = torch.cat((x6,t), dim=0)\n",
"x6 = x6[1:b+2].reshape(b,1) #每年11月到次年五月为流感多发期"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "7d7ab9a8",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table class=\"simpletable\">\n",
"<caption>OLS Regression Results</caption>\n",
"<tr>\n",
" <th>Dep. Variable:</th> <td>y</td> <th> R-squared (uncentered):</th> <td> 0.564</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Model:</th> <td>OLS</td> <th> Adj. R-squared (uncentered):</th> <td> 0.564</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Method:</th> <td>Least Squares</td> <th> F-statistic: </th> <td> 907.9</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Date:</th> <td>Thu, 14 Jul 2022</td> <th> Prob (F-statistic):</th> <td>1.18e-253</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Time:</th> <td>16:51:09</td> <th> Log-Likelihood: </th> <td> -16966.</td> \n",
"</tr>\n",
"<tr>\n",
" <th>No. Observati**:</th> <td> 1404</td> <th> AIC: </th> <td>3.394e+04</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Df Residuals:</th> <td> 1402</td> <th> BIC: </th> <td>3.395e+04</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Df Model:</th> <td> 2</td> <th> </th> <td> </td> \n",
"</tr>\n",
"<tr>\n",
" <th>Covariance Type:</th> <td>nonrobust</td> <th> </th> <td> </td> \n",
"</tr>\n",
"</table>\n",
"<table class=\"simpletable\">\n",
"<tr>\n",
" <td></td> <th>coef</th> <th>std err</th> <th>t</th> <th>P>|t|</th> <th>[0.025</th> <th>0.975]</th> \n",
"</tr>\n",
"<tr>\n",
" <th>x1</th> <td> 202.8443</td> <td> 10.948</td> <td> 18.528</td> <td> 0.000</td> <td> 181.368</td> <td> 224.321</td>\n",
"</tr>\n",
"<tr>\n",
" <th>x2</th> <td> 4874.4095</td> <td> 530.290</td> <td> 9.192</td> <td> 0.000</td> <td> 3834.162</td> <td> 5914.658</td>\n",
"</tr>\n",
"</table>\n",
"<table class=\"simpletable\">\n",
"<tr>\n",
" <th>Omnibus:</th> <td>50.157</td> <th> Durbin-Watson: </th> <td> 1.844</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Prob(Omnibus):</th> <td> 0.000</td> <th> Jarque-Bera (JB): </th> <td> 54.866</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Skew:</th> <td> 0.462</td> <th> Prob(JB): </th> <td>1.22e-12</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Kurtosis:</th> <td> 3.288</td> <th> Cond. No. </th> <td> 78.3</td>\n",
"</tr>\n",
"</table><br/><br/>Notes:<br/>[1] R² is computed without centering (uncentered) since the model does not contain a c**tant.<br/>[2] Standard Errors assume that the covariance matrix of the errors is correctly specified."
],
"text/plain": [
"<class 'statsmodels.iolib.summary.Summary'>\n",
"\"\"\"\n",
" OLS Regression Results \n",
"=======================================================================================\n",
"Dep. Variable: y R-squared (uncentered): 0.564\n",
"Model: OLS Adj. R-squared (uncentered): 0.564\n",
"Method: Least Squares F-statistic: 907.9\n",
"Date: Thu, 14 Jul 2022 Prob (F-statistic): 1.18e-253\n",
"Time: 16:51:09 Log-Likelihood: -16966.\n",
"No. Observati**: 1404 AIC: 3.394e+04\n",
"Df Residuals: 1402 BIC: 3.395e+04\n",
"Df Model: 2 \n",
"Covariance Type: nonrobust \n",
"==============================================================================\n",
" coef std err t P>|t| [0.025 0.975]\n",
"------------------------------------------------------------------------------\n",
"x1 202.8443 10.948 18.528 0.000 181.368 224.321\n",
"x2 4874.4095 530.290 9.192 0.000 3834.162 5914.658\n",
"==============================================================================\n",
"Omnibus: 50.157 Durbin-Watson: 1.844\n",
"Prob(Omnibus): 0.000 Jarque-Bera (JB): 54.866\n",
"Skew: 0.462 Prob(JB): 1.22e-12\n",
"Kurtosis: 3.288 Cond. No. 78.3\n",
"==============================================================================\n",
"\n",
"Notes:\n",
"[1] R² is computed without centering (uncentered) since the model does not contain a c**tant.\n",
"[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
"\"\"\""
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = torch.cat((xd1,x2),dim = 1)\n",
"y = y1\n",
"x = x.numpy()\n",
"y = y.numpy()\n",
"model = sm.OLS(y, x) #生成模型 \n",
"result = model.fit() #模型拟合 \n",
"result.summary() #模型描述 "
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "8076c5da",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table class=\"simpletable\">\n",
"<caption>OLS Regression Results</caption>\n",
"<tr>\n",
" <th>Dep. Variable:</th> <td>y</td> <th> R-squared (uncentered):</th> <td> 0.898</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Model:</th> <td>OLS</td> <th> Adj. R-squared (uncentered):</th> <td> 0.897</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Method:</th> <td>Least Squares</td> <th> F-statistic: </th> <td> 1254.</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Date:</th> <td>Thu, 14 Jul 2022</td> <th> Prob (F-statistic):</th> <td>5.82e-142</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Time:</th> <td>16:57:27</td> <th> Log-Likelihood: </th> <td> -3220.5</td> \n",
"</tr>\n",
"<tr>\n",
" <th>No. Observati**:</th> <td> 287</td> <th> AIC: </th> <td> 6445.</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Df Residuals:</th> <td> 285</td> <th> BIC: </th> <td> 6452.</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Df Model:</th> <td> 2</td> <th> </th> <td> </td> \n",
"</tr>\n",
"<tr>\n",
" <th>Covariance Type:</th> <td>nonrobust</td> <th> </th> <td> </td> \n",
"</tr>\n",
"</table>\n",
"<table class=\"simpletable\">\n",
"<tr>\n",
" <td></td> <th>coef</th> <th>std err</th> <th>t</th> <th>P>|t|</th> <th>[0.025</th> <th>0.975]</th> \n",
"</tr>\n",
"<tr>\n",
" <th>x1</th> <td> 56.4788</td> <td> 12.012</td> <td> 4.702</td> <td> 0.000</td> <td> 32.836</td> <td> 80.122</td>\n",
"</tr>\n",
"<tr>\n",
" <th>x2</th> <td> 0.8010</td> <td> 0.035</td> <td> 22.636</td> <td> 0.000</td> <td> 0.731</td> <td> 0.871</td>\n",
"</tr>\n",
"</table>\n",
"<table class=\"simpletable\">\n",
"<tr>\n",
" <th>Omnibus:</th> <td>14.918</td> <th> Durbin-Watson: </th> <td> 2.708</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Prob(Omnibus):</th> <td> 0.001</td> <th> Jarque-Bera (JB): </th> <td> 23.148</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Skew:</th> <td>-0.345</td> <th> Prob(JB): </th> <td>9.41e-06</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Kurtosis:</th> <td> 4.208</td> <th> Cond. No. </th> <td> 636.</td>\n",
"</tr>\n",
"</table><br/><br/>Notes:<br/>[1] R² is computed without centering (uncentered) since the model does not contain a c**tant.<br/>[2] Standard Errors assume that the covariance matrix of the errors is correctly specified."
],
"text/plain": [
"<class 'statsmodels.iolib.summary.Summary'>\n",
"\"\"\"\n",
" OLS Regression Results \n",
"=======================================================================================\n",
"Dep. Variable: y R-squared (uncentered): 0.898\n",
"Model: OLS Adj. R-squared (uncentered): 0.897\n",
"Method: Least Squares F-statistic: 1254.\n",
"Date: Thu, 14 Jul 2022 Prob (F-statistic): 5.82e-142\n",
"Time: 16:57:27 Log-Likelihood: -3220.5\n",
"No. Observati**: 287 AIC: 6445.\n",
"Df Residuals: 285 BIC: 6452.\n",
"Df Model: 2 \n",
"Covariance Type: nonrobust \n",
"==============================================================================\n",
" coef std err t P>|t| [0.025 0.975]\n",
"------------------------------------------------------------------------------\n",
"x1 56.4788 12.012 4.702 0.000 32.836 80.122\n",
"x2 0.8010 0.035 22.636 0.000 0.731 0.871\n",
"==============================================================================\n",
"Omnibus: 14.918 Durbin-Watson: 2.708\n",
"Prob(Omnibus): 0.001 Jarque-Bera (JB): 23.148\n",
"Skew: -0.345 Prob(JB): 9.41e-06\n",
"Kurtosis: 4.208 Cond. No. 636.\n",
"==============================================================================\n",
"\n",
"Notes:\n",
"[1] R² is computed without centering (uncentered) since the model does not contain a c**tant.\n",
"[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
"\"\"\""
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = torch.cat((xw1,x3),dim = 1)\n",
"y = y2\n",
"x = x.numpy()\n",
"y = y.numpy()\n",
"\n",
"model = sm.OLS(y, x) #生成模型 \n",
"result = model.fit() #模型拟合 \n",
"result.summary() #模型描述 \n"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "5e766f2d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table class=\"simpletable\">\n",
"<caption>OLS Regression Results</caption>\n",
"<tr>\n",
" <th>Dep. Variable:</th> <td>y</td> <th> R-squared (uncentered):</th> <td> 1.000</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Model:</th> <td>OLS</td> <th> Adj. R-squared (uncentered):</th> <td> 1.000</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Method:</th> <td>Least Squares</td> <th> F-statistic: </th> <td>3.477e+15</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Date:</th> <td>Thu, 14 Jul 2022</td> <th> Prob (F-statistic):</th> <td> 0.00</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Time:</th> <td>20:47:09</td> <th> Log-Likelihood: </th> <td> 265.57</td> \n",
"</tr>\n",
"<tr>\n",
" <th>No. Observati**:</th> <td> 66</td> <th> AIC: </th> <td> -525.1</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Df Residuals:</th> <td> 63</td> <th> BIC: </th> <td> -518.6</td> \n",
"</tr>\n",
"<tr>\n",
" <th>Df Model:</th> <td> 3</td> <th> </th> <td> </td> \n",
"</tr>\n",
"<tr>\n",
" <th>Covariance Type:</th> <td>nonrobust</td> <th> </th> <td> </td> \n",
"</tr>\n",
"</table>\n",
"<table class=\"simpletable\">\n",
"<tr>\n",
" <td></td> <th>coef</th> <th>std err</th> <th>t</th> <th>P>|t|</th> <th>[0.025</th> <th>0.975]</th> \n",
"</tr>\n",
"<tr>\n",
" <th>x1</th> <td>-1.526e-05</td> <td> 2.77e-05</td> <td> -0.550</td> <td> 0.584</td> <td>-7.07e-05</td> <td> 4.02e-05</td>\n",
"</tr>\n",
"<tr>\n",
" <th>x2</th> <td> 1.0000</td> <td> 2.17e-08</td> <td> 4.6e+07</td> <td> 0.000</td> <td> 1.000</td> <td> 1.000</td>\n",
"</tr>\n",
"<tr>\n",
" <th>x3</th> <td> 0.0010</td> <td> 0.001</td> <td> 0.869</td> <td> 0.388</td> <td> -0.001</td> <td> 0.003</td>\n",
"</tr>\n",
"</table>\n",
"<table class=\"simpletable\">\n",
"<tr>\n",
" <th>Omnibus:</th> <td>65.730</td> <th> Durbin-Watson: </th> <td> 0.099</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Prob(Omnibus):</th> <td> 0.000</td> <th> Jarque-Bera (JB): </th> <td> 301.727</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Skew:</th> <td> 3.207</td> <th> Prob(JB): </th> <td>3.03e-66</td>\n",
"</tr>\n",
"<tr>\n",
" <th>Kurtosis:</th> <td>11.282</td> <th> Cond. No. </th> <td>1.15e+05</td>\n",
"</tr>\n",
"</table><br/><br/>Notes:<br/>[1] R² is computed without centering (uncentered) since the model does not contain a c**tant.<br/>[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.<br/>[3] The condition number is large, 1.15e+05. This might indicate that there are<br/>strong multicollinearity or other numerical problems."
],
"text/plain": [
"<class 'statsmodels.iolib.summary.Summary'>\n",
"\"\"\"\n",
" OLS Regression Results \n",
"=======================================================================================\n",
"Dep. Variable: y R-squared (uncentered): 1.000\n",
"Model: OLS Adj. R-squared (uncentered): 1.000\n",
"Method: Least Squares F-statistic: 3.477e+15\n",
"Date: Thu, 14 Jul 2022 Prob (F-statistic): 0.00\n",
"Time: 20:47:09 Log-Likelihood: 265.57\n",
"No. Observati**: 66 AIC: -525.1\n",
"Df Residuals: 63 BIC: -518.6\n",
"Df Model: 3 \n",
"Covariance Type: nonrobust \n",
"==============================================================================\n",
" coef std err t P>|t| [0.025 0.975]\n",
"------------------------------------------------------------------------------\n",
"x1 -1.526e-05 2.77e-05 -0.550 0.584 -7.07e-05 4.02e-05\n",
"x2 1.0000 2.17e-08 4.6e+07 0.000 1.000 1.000\n",
"x3 0.0010 0.001 0.869 0.388 -0.001 0.003\n",
"==============================================================================\n",
"Omnibus: 65.730 Durbin-Watson: 0.099\n",
"Prob(Omnibus): 0.000 Jarque-Bera (JB): 301.727\n",
"Skew: 3.207 Prob(JB): 3.03e-66\n",
"Kurtosis: 11.282 Cond. No. 1.15e+05\n",
"==============================================================================\n",
"\n",
"Notes:\n",
"[1] R² is computed without centering (uncentered) since the model does not contain a c**tant.\n",
"[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
"[3] The condition number is large, 1.15e+05. This might indicate that there are\n",
"strong multicollinearity or other numerical problems.\n",
"\"\"\""
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = torch.cat((x4,x5,x6),dim = 1)\n",
"y = y3\n",
"x = x.numpy()\n",
"y = y.numpy()\n",
"\n",
"model = sm.OLS(y, x) #生成模型 \n",
"result = model.fit() #模型拟合 \n",
"result.summary() #模型描述 "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e112d822",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|
|