You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2415 lines
75 KiB
Plaintext

2 years ago
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# %pip install sklearn"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.preprocessing import MinMaxScaler"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(40, 2)\n"
]
}
],
"source": [
"# 加载数据集\n",
"data = pd.read_csv('translated_speed_strain_useOriginSpeed.csv')\n",
"data=data.head(200)\n",
"# 提取特征\n",
"# features = data[['焊接电压', '焊接电流', '焊接速度', 'x', 'y', 'z', '3', '4', '5','strain']]\n",
"\n",
"\n",
"features=pd.DataFrame()\n",
"features['UIV']=data['焊接电压']*data['焊接电流']/data['焊接速度']\n",
"features[\"Q\"]=(data['焊接电压']*data['焊接电流']/data['焊接速度'])*0.75\n",
"mu,sigma=0,5\n",
"noise= np.random.normal(mu, sigma, len(features[\"Q\"]))\n",
"features[\"Q\"]=features[\"Q\"]+noise\n",
"\n",
"\n",
"features=features.to_numpy()\n",
"\n",
"train_size = int(0.8*len(features))\n",
"train_sequences = features[:train_size]\n",
"test_sequences = features[train_size:]\n",
"\n",
"\n",
"print(test_sequences.shape)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"class LSTM(nn.Module):\n",
" def __init__(self, input_size, hidden_size, num_layers, output_size):\n",
" super(LSTM, self).__init__()\n",
" # self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)\n",
" # self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)\n",
" # self.lstm=nn.RNN(input_size, hidden_size, num_layers, batch_first=True)\n",
" self.fc0=nn.Linear(input_size,hidden_size,bias=False)\n",
" self.relu = nn.ReLU()\n",
" self.fc = nn.Linear(hidden_size, output_size)\n",
" \n",
" nn.init.constant_(self.fc0.weight,0.78)\n",
" nn.init.xavier_uniform_(self.fc.weight)\n",
" nn.init.zeros_(self.fc.bias)\n",
"\n",
" def forward(self, x):\n",
" # out, _ = self.lstm(x)\n",
" out0=self.fc0(x)\n",
" # out1=self.relu(out0)\n",
" # print(f\"out{out[0]}\")\n",
" # print(f\"out{out[-1]}\")\n",
" # out2 = self.fc(out1)\n",
" return out0"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"input_size = 1\n",
"hidden_size = 1\n",
"num_layers = 2\n",
"output_size = 1\n",
"model = LSTM(input_size, hidden_size, num_layers, output_size)\n",
"\n",
"# 定义优化器和损失函数\n",
"learning_rate = 0.01\n",
"# learning_rate=1\n",
"criterion = nn.MSELoss()\n",
"# criterion=nn.L1Loss()\n",
"optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/50, Loss: 26.1416\n",
"[[0.7336207032203674]]\n",
"Epoch 2/50, Loss: 20.0536\n",
"[[0.7503769993782043]]\n",
"Epoch 3/50, Loss: 18.3776\n",
"[[0.7569617033004761]]\n",
"Epoch 4/50, Loss: 20.1773\n",
"[[0.7479583024978638]]\n",
"Epoch 5/50, Loss: 19.8454\n",
"[[0.7500355839729309]]\n",
"Epoch 6/50, Loss: 19.2315\n",
"[[0.7522401809692383]]\n",
"Epoch 7/50, Loss: 19.7850\n",
"[[0.7499220967292786]]\n",
"Epoch 8/50, Loss: 19.5623\n",
"[[0.7510177493095398]]\n",
"Epoch 9/50, Loss: 19.5571\n",
"[[0.7509536743164062]]\n",
"Epoch 10/50, Loss: 19.6496\n",
"[[0.7506616711616516]]\n",
"Epoch 11/50, Loss: 19.5702\n",
"[[0.7510122060775757]]\n",
"Epoch 12/50, Loss: 19.6270\n",
"[[0.7507977485656738]]\n",
"Epoch 13/50, Loss: 19.6025\n",
"[[0.7509378790855408]]\n",
"Epoch 14/50, Loss: 19.6167\n",
"[[0.7508964538574219]]\n",
"Epoch 15/50, Loss: 19.6162\n",
"[[0.7509273290634155]]\n",
"Epoch 16/50, Loss: 19.6183\n",
"[[0.7509388327598572]]\n",
"Epoch 17/50, Loss: 19.6221\n",
"[[0.7509452700614929]]\n",
"Epoch 18/50, Loss: 19.6225\n",
"[[0.7509629726409912]]\n",
"Epoch 19/50, Loss: 19.6260\n",
"[[0.7509673833847046]]\n",
"Epoch 20/50, Loss: 19.6267\n",
"[[0.7509824633598328]]\n",
"Epoch 21/50, Loss: 19.6294\n",
"[[0.7509878873825073]]\n",
"Epoch 22/50, Loss: 19.6303\n",
"[[0.7510002255439758]]\n",
"Epoch 23/50, Loss: 19.6325\n",
"[[0.7510063052177429]]\n",
"Epoch 24/50, Loss: 19.6334\n",
"[[0.7510166168212891]]\n",
"Epoch 25/50, Loss: 19.6352\n",
"[[0.7510228157043457]]\n",
"Epoch 26/50, Loss: 19.6362\n",
"[[0.7510318756103516]]\n",
"Epoch 27/50, Loss: 19.6378\n",
"[[0.7510377168655396]]\n",
"Epoch 28/50, Loss: 19.6386\n",
"[[0.7510459423065186]]\n",
"Epoch 29/50, Loss: 19.6401\n",
"[[0.7510514259338379]]\n",
"Epoch 30/50, Loss: 19.6409\n",
"[[0.7510588765144348]]\n",
"Epoch 31/50, Loss: 19.6421\n",
"[[0.7510640621185303]]\n",
"Epoch 32/50, Loss: 19.6429\n",
"[[0.7510709166526794]]\n",
"Epoch 33/50, Loss: 19.6440\n",
"[[0.7510756850242615]]\n",
"Epoch 34/50, Loss: 19.6447\n",
"[[0.7510820627212524]]\n",
"Epoch 35/50, Loss: 19.6458\n",
"[[0.7510864734649658]]\n",
"Epoch 36/50, Loss: 19.6464\n",
"[[0.7510923743247986]]\n",
"Epoch 37/50, Loss: 19.6473\n",
"[[0.7510964870452881]]\n",
"Epoch 38/50, Loss: 19.6479\n",
"[[0.7511020302772522]]\n",
"Epoch 39/50, Loss: 19.6488\n",
"[[0.751105785369873]]\n",
"Epoch 40/50, Loss: 19.6493\n",
"[[0.7511110901832581]]\n",
"Epoch 41/50, Loss: 19.6502\n",
"[[0.7511143684387207]]\n",
"Epoch 42/50, Loss: 19.6505\n",
"[[0.7511195540428162]]\n",
"Epoch 43/50, Loss: 19.6515\n",
"[[0.7511224150657654]]\n",
"Epoch 44/50, Loss: 19.6517\n",
"[[0.7511274814605713]]\n",
"Epoch 45/50, Loss: 19.6527\n",
"[[0.7511299252510071]]\n",
"Epoch 46/50, Loss: 19.6528\n",
"[[0.751134991645813]]\n",
"Epoch 47/50, Loss: 19.6537\n",
"[[0.7511370182037354]]\n",
"Epoch 48/50, Loss: 19.6538\n",
"[[0.7511419653892517]]\n",
"Epoch 49/50, Loss: 19.6548\n",
"[[0.7511436939239502]]\n",
"Epoch 50/50, Loss: 19.6548\n",
"[[0.7511485815048218]]\n"
]
}
],
"source": [
"# 训练模型\n",
"num_epochs = 50\n",
"batch_size = 16\n",
"for epoch in range(num_epochs):\n",
" for i in range(0, len(train_sequences), batch_size):\n",
" batch_sequences = train_sequences[i:i+batch_size]\n",
" # print(batch_sequences.shape)\n",
" inputs = torch.tensor(batch_sequences[:, :-1], dtype=torch.float32)\n",
" \n",
" targets = torch.tensor(batch_sequences[:,-1], dtype=torch.float32)\n",
" if len(inputs) != batch_size:\n",
" continue\n",
" \n",
" # 前向传播\n",
" outputs = model(inputs)\n",
" # print(f\"targets {targets}\")\n",
" \n",
" # print(f\"input:{inputs}\")\n",
" \n",
" # print(f\"outputs {outputs}\")\n",
" \n",
" outputs=outputs.reshape(targets.shape)\n",
" # print(outputs.shape)\n",
" # print(f\"output:{outputs.shape}\")\n",
" # print(f\"target:{targets.shape}\")\n",
" loss = criterion(outputs, targets)\n",
" # print(f\"loss:{loss}\")\n",
" # loss=loss*1000\n",
" # 反向传播和优化\n",
" loss.backward()\n",
" optimizer.step()\n",
" optimizer.zero_grad()\n",
" \n",
" \n",
" \n",
" # if epoch>996:\n",
" if True:\n",
" print(f'Epoch {epoch+1}/{num_epochs}, Loss: {loss.item():.4f}')\n",
" \n",
" print(model.fc0.weight.tolist())\n",
" # print(model.fc.weight.tolist())\n",
" # print(model.fc.bias)\n",
" # print(model.fc.weight[0][0])\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mnotebook controller is DISPOSED. \n",
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
]
}
],
"source": [
"# num_epochs = 30\n",
"# batch_size = 32\n",
"# learning_rate=0.05\n",
"# for k in range(8):\n",
"# learning_rate*=0.5\n",
"# print(f\"learning_rate:{learning_rate}\")\n",
"# for epoch in range(num_epochs):\n",
"# for i in range(0, len(train_sequences), batch_size):\n",
"# batch_sequences = train_sequences[i:i+batch_size]\n",
"# inputs = torch.tensor(batch_sequences[:, :-1,:-1], dtype=torch.float32)\n",
"# targets = torch.tensor(batch_sequences[:, -1, -1], dtype=torch.float32)\n",
"# if len(inputs) != batch_size:\n",
"# continue\n",
" \n",
"# # 前向传播\n",
"# # print(inputs.shape)\n",
"# outputs = model(inputs)\n",
"# loss = criterion(outputs, targets)\n",
" \n",
"# # 反向传播和优化\n",
"# optimizer.zero_grad()\n",
"# loss.backward()\n",
"# optimizer.step()\n",
" \n",
"# print(f'Epoch {epoch+1}/{num_epochs}, Loss: {loss.item():.4f}')"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"test_inputs_shapetorch.Size([40, 1])\n",
"Test Loss: 21.8248\n"
]
}
],
"source": [
"# 在测试集上进行评估\n",
"model.eval()\n",
"test_inputs = torch.tensor(test_sequences[ :,:-1], dtype=torch.float32)\n",
"test_targets = torch.tensor(test_sequences[ :, -1], dtype=torch.float32)\n",
"print(f\"test_inputs_shape{test_inputs.shape}\")\n",
"test_outputs = model(test_inputs)\n",
"test_loss = criterion(test_outputs, test_targets)\n",
"# print(test_outputs)\n",
"# print(test_targets)\n",
"print(f'Test Loss: {test_loss.item():.4f}')"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Parameter containing:\n",
"tensor([[0.7608]], requires_grad=True)\n",
"Parameter containing:\n",
"tensor([-1.3928], requires_grad=True)\n",
"tensor([6.2156], grad_fn=<AddBackward0>)\n"
]
}
],
"source": [
"print(model.fc0.weight)\n",
"\n",
"one_point=torch.tensor([10],dtype=torch.float32)\n",
"print(model(one_point))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"读取点修正模型"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.7511485815048218\n"
]
}
],
"source": [
"model.fc0.weight.tolist()[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" <script type=\"text/javascript\">\n",
" window.PlotlyConfig = {MathJaxConfig: 'local'};\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.18.2.min']\n",
" }\n",
" });\n",
" require(['plotly'], function(Plotly) {\n",
" window._Plotly = Plotly;\n",
" });\n",
" }\n",
" </script>\n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"linkText": "Export to plot.ly",
"plotlyServerURL": "https://plot.ly",
"showLink": false
},
"data": [
{
"mode": "lines+markers",
"type": "scatter",
"x": [
1,
2,
3,
4,
5
],
"y": [
1,
3,
2,
4,
3
]
}
],
"layout": {
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
}
}
},
"text/html": [
"<div> <div id=\"aa0c0fd1-4f98-4b9a-9119-51b6010d1d5b\" class=\"plotly-graph-div\" style=\"height:525px; width:100%;\"></div> <script type=\"text/javascript\"> require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {}; if (document.getElementById(\"aa0c0fd1-4f98-4b9a-9119-51b6010d1d5b\")) { Plotly.newPlot( \"aa0c0fd1-4f98-4b9a-9119-51b6010d1d5b\", [{\"mode\":\"lines+markers\",\"x\":[1,2,3,4,5],\"y\":[1,3,2,4,3],\"type\":\"scatter\"}], {\"template\":{\"data\":{\"barpolar\":[{\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"barpolar\"}],\"bar\":[{\"error_x\":{\"color\":\"#2a3f5f\"},\"error_y\":{\"color\":\"#2a3f5f\"},\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"bar\"}],\"carpet\":[{\"aaxis\":{\"endlinecolor\":\"#2a3f5f\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"#2a3f5f\"},\"baxis\":{\"endlinecolor\":\"#2a3f5f\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"#2a3f5f\"},\"type\":\"carpet\"}],\"choropleth\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"type\":\"choropleth\"}],\"contourcarpet\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"type\":\"contourcarpet\"}],\"contour\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"contour\"}],\"heatmapgl\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"heatmapgl\"}],\"heatmap\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"heatmap\"}],\"histogram2dcontour\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"histogram2dcontour\"}],\"histogram2d\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"histogram2d\"}],\"histogram\":[{\"marker\":{\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"histogram\"}],\"mesh3d\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"type\":\"mesh3d\"}],\"parcoords\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"parcoords\"}],\"pie\":[{\"automargin\":true,\"type\":\"pie\"}],\"scatter3d\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"mark
" \n",
"var gd = document.getElementById('aa0c0fd1-4f98-4b9a-9119-51b6010d1d5b');\n",
"var x = new MutationObserver(function (mutations, observer) {{\n",
" var display = window.getComputedStyle(gd).display;\n",
" if (!display || display === 'none') {{\n",
" console.log([gd, 'removed!']);\n",
" Plotly.purge(gd);\n",
" observer.disconnect();\n",
" }}\n",
"}});\n",
"\n",
"// Listen for the removal of the full notebook cells\n",
"var notebookContainer = gd.closest('#notebook-container');\n",
"if (notebookContainer) {{\n",
" x.observe(notebookContainer, {childList: true});\n",
"}}\n",
"\n",
"// Listen for the clearing of the current output cell\n",
"var outputEl = gd.closest('.output');\n",
"if (outputEl) {{\n",
" x.observe(outputEl, {childList: true});\n",
"}}\n",
"\n",
" }) }; }); </script> </div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import plotly.graph_objs as go\n",
"from plotly.offline import iplot, init_notebook_mode\n",
"\n",
"# 初始化Plotly的Notebook模式\n",
"init_notebook_mode(connected=True)\n",
"\n",
"# 创建初始的折线图数据\n",
"x = [1, 2, 3, 4, 5]\n",
"y = [1, 3, 2, 4, 3]\n",
"\n",
"trace = go.Scatter(x=x, y=y, mode='lines+markers')\n",
"\n",
"data = [trace]\n",
"\n",
"# 绘制初始的折线图\n",
"fig = go.Figure(data=data)\n",
"\n",
"iplot(fig)\n",
"\n",
"# 动态添加新的点\n",
"new_x = [6]\n",
"new_y = [2]\n",
"fig.add_trace(go.Scatter(x=new_x, y=new_y, mode='markers'))\n",
"\n",
"# 更新折线图数据\n",
"fig.update_traces(x=[list(fig.data[0].x) + new_x],\n",
" y=[list(fig.data[0].y) + new_y])\n",
"\n",
"\n",
"# iplot(fig)"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"mode": "lines+markers",
"type": "scatter",
"x": [
[
1,
2,
3,
4,
5,
6
]
],
"y": [
[
1,
3,
2,
4,
3,
2
]
]
},
{
"mode": "markers",
"type": "scatter",
"x": [
[
1,
2,
3,
4,
5,
6
]
],
"y": [
[
1,
3,
2,
4,
3,
2
]
]
}
],
"layout": {
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
}
}
},
"text/html": [
"<div> <div id=\"f1e976b7-a562-4af8-8830-7e27ba5546de\" class=\"plotly-graph-div\" style=\"height:525px; width:100%;\"></div> <script type=\"text/javascript\"> require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {}; if (document.getElementById(\"f1e976b7-a562-4af8-8830-7e27ba5546de\")) { Plotly.newPlot( \"f1e976b7-a562-4af8-8830-7e27ba5546de\", [{\"mode\":\"lines+markers\",\"x\":[[1,2,3,4,5,6]],\"y\":[[1,3,2,4,3,2]],\"type\":\"scatter\"},{\"mode\":\"markers\",\"x\":[[1,2,3,4,5,6]],\"y\":[[1,3,2,4,3,2]],\"type\":\"scatter\"}], {\"template\":{\"data\":{\"histogram2dcontour\":[{\"type\":\"histogram2dcontour\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"choropleth\":[{\"type\":\"choropleth\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}],\"histogram2d\":[{\"type\":\"histogram2d\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"heatmap\":[{\"type\":\"heatmap\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"heatmapgl\":[{\"type\":\"heatmapgl\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"contourcarpet\":[{\"type\":\"contourcarpet\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}],\"contour\":[{\"type\":\"contour\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"surface\":[{\"type\":\"surface\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"mesh3d\":[{\"type\":\"mesh3d\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}],\"scatter\":[{\"fillpattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2},\"type\":\"scatter\"}],\"parcoords\":[{\"type\":\"parcoords\",\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scatterpolargl\":[{\"type\":\"scatterpolargl\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"bar\":[{\"error_x\":{\"color\":\"#2a3f5f\"},\"error_y\":{\"color\":\"#2a3f5f\"},\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"bar\"}],\"scattergeo\":[{\"type\":\"scat
" \n",
"var gd = document.getElementById('f1e976b7-a562-4af8-8830-7e27ba5546de');\n",
"var x = new MutationObserver(function (mutations, observer) {{\n",
" var display = window.getComputedStyle(gd).display;\n",
" if (!display || display === 'none') {{\n",
" console.log([gd, 'removed!']);\n",
" Plotly.purge(gd);\n",
" observer.disconnect();\n",
" }}\n",
"}});\n",
"\n",
"// Listen for the removal of the full notebook cells\n",
"var notebookContainer = gd.closest('#notebook-container');\n",
"if (notebookContainer) {{\n",
" x.observe(notebookContainer, {childList: true});\n",
"}}\n",
"\n",
"// Listen for the clearing of the current output cell\n",
"var outputEl = gd.closest('.output');\n",
"if (outputEl) {{\n",
" x.observe(outputEl, {childList: true});\n",
"}}\n",
"\n",
" }) }; }); </script> </div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"# for epoch in range(num_epochs):\n",
"# for i in range(0, len(train_sequences), batch_size):\n",
"# batch_sequences = train_sequences[i:i+batch_size]\n",
"# # print(batch_sequences.shape)\n",
"# inputs = torch.tensor(batch_sequences[:, :-1], dtype=torch.float32)\n",
" \n",
"# targets = torch.tensor(batch_sequences[:,-1], dtype=torch.float32)\n",
"# if len(inputs) != batch_size:\n",
"# continue\n",
" \n",
"# # 前向传播\n",
"# outputs = model(inputs)\n",
"# # print(f\"targets {targets}\")\n",
" \n",
"# # print(f\"input:{inputs}\")\n",
" \n",
"# # print(f\"outputs {outputs}\")\n",
" \n",
"# outputs=outputs.reshape(targets.shape)\n",
"# # print(outputs.shape)\n",
"# # print(f\"output:{outputs.shape}\")\n",
"# # print(f\"target:{targets.shape}\")\n",
"# loss = criterion(outputs, targets)\n",
"# # print(f\"loss:{loss}\")\n",
"# # loss=loss*1000\n",
"# # 反向传播和优化\n",
"# loss.backward()\n",
"# optimizer.step()\n",
"# optimizer.zero_grad()\n",
" \n",
" \n",
" \n",
"# # if epoch>996:\n",
"# if True:\n",
"# print(f'Epoch {epoch+1}/{num_epochs}, Loss: {loss.item():.4f}')\n",
" \n",
"# print(model.fc0.weight.tolist())\n",
"# print(model.fc0.bias)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
"torch.save(model.state_dict(), 'lstm_model.pt')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"读取"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mnotebook controller is DISPOSED. \n",
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
]
}
],
"source": [
"model = LSTM(input_size, hidden_size, num_layers, output_size)\n",
"\n",
"model.load_state_dict(torch.load('lstm_model.pt'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"下面是尝试反缩放归一化的数据并在测试集测试,发现误差很大,所以决定不再缩放"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mnotebook controller is DISPOSED. \n",
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
]
}
],
"source": [
"\n",
"model.eval()\n",
"test_inputs = torch.tensor(test_sequences[:, :-1,:-1], dtype=torch.float32)\n",
"test_targets = torch.tensor(test_sequences[:, -1, -1], dtype=torch.float32)\n",
"test_outputs = model(test_inputs)\n",
"\n",
"print(test_outputs.shape)\n",
"print(test_outputs.size())\n",
"t=np.zeros((test_outputs.shape[0],1),dtype=float)\n",
"\n",
"t=torch.Tensor(t)\n",
"print(t.shape)\n",
"test_outputs=torch.cat([t, test_outputs], dim=1)\n",
"print(test_outputs.shape)\n",
"\n",
"test_outputs = scaler.inverse_transform(test_outputs.detach().numpy() )\n",
"test_targets = scaler.inverse_transform(torch.tensor(test_sequences[:, -1, :]).detach().numpy())\n",
"test_outputs=torch.tensor(test_outputs[:,-1], dtype=torch.float32)\n",
"test_targets=torch.tensor(test_targets[:,-1], dtype=torch.float32)\n",
"print(test_outputs)\n",
"print(test_targets)\n",
"# print(test_outputs.shape)\n",
"# print(test_targets.shape)\n",
"\n",
"test_loss = criterion(test_outputs, test_targets)\n",
"print(f'Test Loss: {test_loss:.4f}')\n",
"# a=test_outputs>0.00007\n",
"# print(torch.sum(a))\n",
"# b=test_targets>0.00007\n",
"# print(torch.sum(b))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.16 ('torch_py3.9')",
"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.16"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "2c93e8e09754cf1cd40b55c7c8fefeaff6ac58d6a85dd394fdad2eae466f5690"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}