This commit is contained in:
Sasza Stanczew 2025-05-21 10:35:44 +02:00
parent f7c8c1badd
commit 411baadbaa
3 changed files with 53118 additions and 5 deletions

View file

@ -5,20 +5,18 @@ name = "pypi"
[packages]
jupyterlab = "*"
jupyterlab-vim = "*"
notebook-intelligence = "*"
pandas = "*"
numpy = "*"
scikit-learn = "*"
sklearn-pandas = "*"
matplotlib = "*"
<<<<<<< HEAD
notebook-intelligence = "*"
jupyterlab-vim = "*"
=======
tensorflow = "*"
torch = "*"
torchvision = "*"
ultralytics = "*"
>>>>>>> a6f27a9 (123:x)
statsmodels = "*"
[dev-packages]

View file

@ -0,0 +1,506 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "f3f45407-987d-4d79-9650-2b67f875cc4a",
"metadata": {},
"source": [
"# Dzień 7"
]
},
{
"cell_type": "markdown",
"id": "84f23b82-ae45-46bd-a10b-019f7695a1db",
"metadata": {},
"source": [
"# forecasting"
]
},
{
"cell_type": "markdown",
"id": "5d2c3c47-4772-4114-ad39-6d4ad0026a99",
"metadata": {},
"source": [
"## Solar"
]
},
{
"cell_type": "markdown",
"id": "c5bec457-55e9-4113-98c4-4c66bf0c6d40",
"metadata": {},
"source": [
"### Biblioteki "
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "0de8e17c-93ee-4b39-ad7c-cf688e838182",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "fb9246bc-a45c-4c8c-b156-038fce834f91",
"metadata": {},
"source": [
"### Wczytanie danych"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "bc48fb44-a144-40e4-a879-37d73a49c9d1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Datetime\n",
"2007-10-01 00:00:00 0.0\n",
"2007-10-01 01:00:00 0.0\n",
"2007-10-01 02:00:00 0.0\n",
"2007-10-01 03:00:00 0.0\n",
"2007-10-01 04:00:00 0.0\n",
"Name: Incoming Solar, dtype: float64"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"series = pd.read_csv(\"time_series_solar.csv\", parse_dates=['Datetime'], index_col=\"Datetime\")[\"Incoming Solar\"]\n",
"\n",
"series.head()"
]
},
{
"cell_type": "markdown",
"id": "9b7f3349-eb5a-460b-bc9d-92041da86fbc",
"metadata": {
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"### ARIMA"
]
},
{
"cell_type": "markdown",
"id": "3009aaeb-f2dc-4baf-8729-a5311cacb2d9",
"metadata": {
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"#### Tworzenie i uczenie"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "ba96714b-41b5-4114-9d8f-d5b0717125da",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/sasza/.local/share/virtualenvs/JupyterLab-9JRWupKp/lib/python3.12/site-packages/statsmodels/tsa/base/tsa_model.py:473: ValueWarning: No frequency information was provided, so inferred frequency h will be used.\n",
" self._init_dates(dates, freq)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" SARIMAX Results \n",
"==============================================================================\n",
"Dep. Variable: Incoming Solar No. Observations: 52608\n",
"Model: ARIMA(3, 1, 1) Log Likelihood -311811.362\n",
"Date: Wed, 21 May 2025 AIC 623632.724\n",
"Time: 09:56:19 BIC 623677.077\n",
"Sample: 10-01-2007 HQIC 623646.585\n",
" - 09-30-2013 \n",
"Covariance Type: opg \n",
"==============================================================================\n",
" coef std err z P>|z| [0.025 0.975]\n",
"------------------------------------------------------------------------------\n",
"ar.L1 1.2602 0.002 512.194 0.000 1.255 1.265\n",
"ar.L2 -0.2351 0.004 -65.863 0.000 -0.242 -0.228\n",
"ar.L3 -0.2033 0.003 -75.840 0.000 -0.209 -0.198\n",
"ma.L1 -0.9970 0.000 -2652.955 0.000 -0.998 -0.996\n",
"sigma2 8237.1776 24.360 338.144 0.000 8189.433 8284.922\n",
"===================================================================================\n",
"Ljung-Box (L1) (Q): 33.68 Jarque-Bera (JB): 187848.52\n",
"Prob(Q): 0.00 Prob(JB): 0.00\n",
"Heteroskedasticity (H): 1.04 Skew: 0.51\n",
"Prob(H) (two-sided): 0.01 Kurtosis: 12.20\n",
"===================================================================================\n",
"\n",
"Warnings:\n",
"[1] Covariance matrix calculated using the outer product of gradients (complex-step).\n"
]
}
],
"source": [
"from statsmodels.tsa.arima.model import ARIMA\n",
"\n",
"model = ARIMA(series, order=(3, 1, 1), freq='h')\n",
"model_fit = model.fit()\n",
"print(model_fit.summary())"
]
},
{
"cell_type": "markdown",
"id": "eb1af070-d18d-4e01-8886-b42e1368ac30",
"metadata": {
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"#### Test"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "a09b2c6f-98d6-43bb-be39-0f39694a430f",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/sasza/.local/share/virtualenvs/JupyterLab-9JRWupKp/lib/python3.12/site-packages/statsmodels/tsa/statespace/representation.py:374: FutureWarning: Unknown keyword arguments: dict_keys(['typ']).Passing unknown keyword arguments will raise a TypeError beginning in version 0.15.\n",
" warnings.warn(msg, FutureWarning)\n"
]
},
{
"data": {
"text/plain": [
"2013-10-01 00:00:00 24.388517\n",
"2013-10-01 01:00:00 55.123963\n",
"2013-10-01 02:00:00 88.124071\n",
"2013-10-01 03:00:00 117.527535\n",
"2013-10-01 04:00:00 140.575513\n",
"2013-10-01 05:00:00 155.999219\n",
"2013-10-01 06:00:00 164.039971\n",
"2013-10-01 07:00:00 165.861096\n",
"2013-10-01 08:00:00 163.129890\n",
"2013-10-01 09:00:00 157.624974\n",
"2013-10-01 10:00:00 150.959328\n",
"2013-10-01 11:00:00 144.408545\n",
"2013-10-01 12:00:00 138.839343\n",
"2013-10-01 13:00:00 134.716152\n",
"2013-10-01 14:00:00 132.161153\n",
"2013-10-01 15:00:00 131.042918\n",
"2013-10-01 16:00:00 131.072666\n",
"2013-10-01 17:00:00 131.892522\n",
"2013-10-01 18:00:00 133.146095\n",
"2013-10-01 19:00:00 134.527095\n",
"2013-10-01 20:00:00 135.806076\n",
"2013-10-01 21:00:00 136.838350\n",
"2013-10-01 22:00:00 137.557791\n",
"2013-10-01 23:00:00 137.961733\n",
"2013-10-02 00:00:00 138.091778\n",
"2013-10-02 01:00:00 138.014425\n",
"2013-10-02 02:00:00 137.804240\n",
"2013-10-02 03:00:00 137.531104\n",
"2013-10-02 04:00:00 137.252029\n",
"2013-10-02 05:00:00 137.007278\n",
"2013-10-02 06:00:00 136.819976\n",
"2013-10-02 07:00:00 136.698214\n",
"2013-10-02 08:00:00 136.638560\n",
"2013-10-02 09:00:00 136.630090\n",
"2013-10-02 10:00:00 136.658197\n",
"2013-10-02 11:00:00 136.707738\n",
"2013-10-02 12:00:00 136.765285\n",
"2013-10-02 13:00:00 136.820447\n",
"2013-10-02 14:00:00 136.866362\n",
"2013-10-02 15:00:00 136.899556\n",
"2013-10-02 16:00:00 136.919380\n",
"2013-10-02 17:00:00 136.927223\n",
"2013-10-02 18:00:00 136.925697\n",
"2013-10-02 19:00:00 136.917900\n",
"2013-10-02 20:00:00 136.906839\n",
"2013-10-02 21:00:00 136.895041\n",
"2013-10-02 22:00:00 136.884360\n",
"2013-10-02 23:00:00 136.875921\n",
"2013-10-03 00:00:00 136.870197\n",
"2013-10-03 01:00:00 136.867138\n",
"2013-10-03 02:00:00 136.866344\n",
"Freq: h, Name: predicted_mean, dtype: float64"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pred = model_fit.predict(start=len(series),end=len(series)+50, typ=\"levels\")\n",
"pred"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4add26f7-0995-4a06-a495-3d71339827bc",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "f4048b90-85a9-4ca8-b9c9-675a65c66a94",
"metadata": {
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"### LSTM"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "9d306814-eedc-499c-8ab2-adda96a69b77",
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.preprocessing import MinMaxScaler"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "b5e5d70e-9cb9-4cd8-9951-6896adaa05db",
"metadata": {},
"outputs": [],
"source": [
"def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):\n",
" n_vars = 1 if len(data.shape) == 1 else data.shape[1]\n",
" df = pd.DataFrame(data)\n",
" cols, names = list(), list()\n",
" # input sequence (t-n, ... t-1)\n",
" for i in range(n_in, 0, -1):\n",
" cols.append(df.shift(i))\n",
" names += [(\"var%d(t-%d)\" % (j + 1, i)) for j in range(n_vars)]\n",
" # forecast sequence (t, t+1, ... t+n)\n",
" for i in range(0, n_out):\n",
" cols.append(df.shift(-i))\n",
" if i == 0:\n",
" names += [(\"var%d(t)\" % (j + 1)) for j in range(n_vars)]\n",
" else:\n",
" names += [(\"var%d(t+%d)\" % (j + 1, i)) for j in range(n_vars)]\n",
" # put it all together\n",
" agg = pd.concat(cols, axis=1)\n",
" agg.columns = names\n",
" # drop rows with NaN values\n",
" if dropnan:\n",
" agg.dropna(inplace=True)\n",
" return agg"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "3dc00ddd-6107-41a1-8de0-c777e5b1f555",
"metadata": {},
"outputs": [],
"source": [
"data = series_to_supervised(series, n_in=1, n_out=1)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "ae53c621-39ec-4251-be6f-df746a7dffa4",
"metadata": {},
"outputs": [],
"source": [
"series = series.resample('D').sum()"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "053c8a96-c055-4caa-9f58-6a49ac1fafd8",
"metadata": {},
"outputs": [],
"source": [
"scaler = MinMaxScaler(feature_range=(-1, 1))\n",
"train,test = train_test_split(data, test_size=0.2, shuffle=False)\n",
"train = scaler.fit_transform(train)\n",
"test = scaler.transform(test)\n"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "7ca18f24-8faf-4675-8205-986940a77ff7",
"metadata": {},
"outputs": [],
"source": [
"X_train, y_train = train[:, :-1], train[:, -1]\n",
"X_test, y_test = test[:, :-1], test[:, -1]\n",
"\n",
"X_train = torch.from_numpy(X_train).type(torch.Tensor)\n",
"X_test = torch.from_numpy(X_test).type(torch.Tensor)\n",
"y_train = torch.from_numpy(y_train).type(torch.Tensor).view(-1)\n",
"y_test = torch.from_numpy(y_test).type(torch.Tensor).view(-1)\n",
"\n",
"X_train = X_train.view([X_train.shape[0], X_train.shape[1], 1])\n",
"X_test = X_test.view([X_test.shape[0], X_test.shape[1], 1])"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "34dcd6c9-ffa2-4d5e-8ae7-65619f490156",
"metadata": {},
"outputs": [],
"source": [
"class LSTM(nn.Module):\n",
" def __init__(self, input_dim, hidden_dim, num_layers, output_dim):\n",
" super(LSTM, self).__init__()\n",
" self.hidden_dim = hidden_dim\n",
" self.num_layers = num_layers\n",
" self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True)\n",
" self.fc = nn.Linear(hidden_dim, output_dim)\n",
"\n",
" def forward(self, x):\n",
" h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).requires_grad_()\n",
" c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).requires_grad_()\n",
" out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))\n",
" out = self.fc(out[:, -1, :])\n",
"\n",
" return out"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "c73a7f15-bd10-460f-b317-813b217460ff",
"metadata": {},
"outputs": [],
"source": [
"model = LSTM(input_dim=1,\n",
" hidden_dim=32,\n",
" output_dim=1,\n",
" num_layers=1)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "ae9959b1-39dc-4088-92fb-d8860e65238c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch: 0, Loss: 0.7092826962471008\n",
"Epoch: 10, Loss: 0.6484428644180298\n",
"Epoch: 20, Loss: 0.5884607434272766\n",
"Epoch: 30, Loss: 0.5275981426239014\n",
"Epoch: 40, Loss: 0.46536171436309814\n",
"Epoch: 50, Loss: 0.4024186432361603\n",
"Epoch: 60, Loss: 0.34060969948768616\n",
"Epoch: 70, Loss: 0.2827403247356415\n",
"Epoch: 80, Loss: 0.23206999897956848\n",
"Epoch: 90, Loss: 0.19147342443466187\n",
"Epoch: 100, Loss: 0.16242049634456635\n",
"Epoch: 110, Loss: 0.1442137509584427\n",
"Epoch: 120, Loss: 0.1341131627559662\n",
"Epoch: 130, Loss: 0.1285659521818161\n",
"Epoch: 140, Loss: 0.12475237250328064\n",
"Epoch: 150, Loss: 0.12126223742961884\n",
"Epoch: 160, Loss: 0.11771411448717117\n",
"Epoch: 170, Loss: 0.11410810053348541\n",
"Epoch: 180, Loss: 0.11048132181167603\n",
"Epoch: 190, Loss: 0.10684189945459366\n",
"Test Loss: 0.11549811065196991\n"
]
}
],
"source": [
"loss_fn = nn.MSELoss()\n",
"optimizer = torch.optim.Adam(model.parameters(), lr=0.001)\n",
"\n",
"epochs = 200\n",
"\n",
"for epoch in range(epochs):\n",
" model.train()\n",
" optimizer.zero_grad()\n",
"\n",
" out = model(X_train).reshape(-1, )\n",
" loss = loss_fn(out, y_train)\n",
" loss.backward()\n",
" optimizer.step()\n",
"\n",
" if epoch % 10 == 0:\n",
" print(f\"Epoch: {epoch}, Loss: {loss.item()}\")\n",
"\n",
"model.eval()\n",
"y_pred = model(X_test).reshape(-1, )\n",
"test_loss = loss_fn(y_pred, y_test)\n",
"print(f\"Test Loss: {test_loss.item()}\")"
]
},
{
"cell_type": "markdown",
"id": "f4989fd8-9578-4d77-92f0-1fd26e206359",
"metadata": {},
"source": [
"### 123"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68f0a136-a2ed-4905-b469-345b28329a2e",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0dd52bb4-fa30-487f-819a-8a633a79cdb0",
"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.12.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because it is too large Load diff