{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f304a25f-fb7b-4bd1-9bdc-a04bb593fe58",
   "metadata": {
    "id": "f304a25f-fb7b-4bd1-9bdc-a04bb593fe58"
   },
   "source": [
    "# Lab 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d635a4b-b113-4de2-bc6f-3d0cfb76e618",
   "metadata": {},
   "source": [
    "## Vaiksmai su skaičiais\n",
    "\n",
    "Atlikite matematinius veiksmus su skaičiais, sugalvokite daugiau sudėtingesnių veiksmų."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "676a1e8c-6f4a-4370-94f5-8df04d859435",
   "metadata": {
    "id": "676a1e8c-6f4a-4370-94f5-8df04d859435",
    "outputId": "9fe71b1e-80ae-4417-d668-736e046b32ca"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4+5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "92a6c829-6a36-431f-aed5-b9f202005275",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "a=5\n",
    "b=6\n",
    "print(a*b)\n",
    "print(a^2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "625ffb6e-0f92-4e91-8c3c-083ce07d75b8",
   "metadata": {},
   "source": [
    "## Numpy bibliotekos naudojimas\n",
    "\n",
    "Peržvelkite dokumentaciją, atlikite kiek galite daugiau pavyzdžių:\n",
    "\n",
    "* https://numpy.org/doc/stable/user/index.html\n",
    "* https://numpy.org/doc/stable/user/absolute_beginners.html\n",
    "* https://numpy.org/doc/stable/user/quickstart.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3f5ffd90-8ef0-45f5-ad66-686c228a3f8b",
   "metadata": {
    "id": "3f5ffd90-8ef0-45f5-ad66-686c228a3f8b"
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2a30cd5-7d6d-42fb-844b-6db83e7aa3e2",
   "metadata": {},
   "source": [
    "## Skaliarinė sandauga\n",
    "\n",
    "Pamėginkite įsivesti naujus vektorius ir atlikti veiksmus su jais."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6c503661-6b4f-4424-a093-0a15464d84f7",
   "metadata": {
    "id": "6c503661-6b4f-4424-a093-0a15464d84f7"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c= 14\n",
      "d= [-9  2  3]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1, 0, 3])\n",
    "b = np.array([2, 3, 4])\n",
    "c = np.dot(a,b)\n",
    "d = np.cross(a,np.transpose(b))\n",
    "\n",
    "print (\"c=\", c)\n",
    "print (\"d=\", d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "108d84de-d3c6-4af6-80e9-fae4da6ea3a0",
   "metadata": {},
   "source": [
    "## Veiksmai su matricomis\n",
    "\n",
    "Pamėginkite įsivesti naujas matricas ir atlikti veiksmus su jomis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "29bf2c27-e278-423e-b658-d69a096d0e3b",
   "metadata": {
    "id": "29bf2c27-e278-423e-b658-d69a096d0e3b"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C= [[2 4]\n",
      " [6 8]]\n",
      "D= [[ 1  4]\n",
      " [ 9 16]]\n",
      "E= [[ 7 10]\n",
      " [15 22]]\n"
     ]
    }
   ],
   "source": [
    "A = np.array([[1, 2], [3, 4]])\n",
    "B = np.array([[1, 2], [3, 4]])\n",
    "\n",
    "C = A + B\n",
    "D = A * B\n",
    "E = np.matmul(A,B)\n",
    "\n",
    "print(\"C=\", C)\n",
    "print(\"D=\", D)\n",
    "print(\"E=\", E)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04127180-6fd8-4230-9b1d-98cc1b1326c6",
   "metadata": {},
   "source": [
    "## MSE skaičiavimas\n",
    "\n",
    "Išanalizuokite žemiau pateikta MSE skaičiavimo pavyzdį."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "415c4fe5-6968-45f1-b536-61514e813e9b",
   "metadata": {
    "id": "415c4fe5-6968-45f1-b536-61514e813e9b"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 34808 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MSE= 0.012540223468521546\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "x = np.arange(0, 5.0, 0.1)\n",
    "y = np.sin(x)\n",
    "y2 = y + np.random.normal(0,0.1,len(y))\n",
    "\n",
    "plt.plot(x, y)\n",
    "plt.plot(x, y2)\n",
    "plt.show()\n",
    "\n",
    "MSE = (1/len(y))*np.sum(np.power((y-y2),2))\n",
    "\n",
    "print(\"MSE=\", MSE)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96d8465c-d0f8-4d36-b4b0-f53e7cd6221c",
   "metadata": {},
   "source": [
    "## Atlikite veiksmus su paveikslėliais\n",
    "\n",
    "Pamėginkite pritaikyti įvarias numpy, ndimage ir matplotlib funkcijas paveikslėliams.\n",
    "\n",
    "* https://docs.scipy.org/doc/scipy/reference/ndimage.html\n",
    "* https://matplotlib.org/stable/tutorials/index.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9552ce03-15e7-4381-8166-e7afcbf97e6e",
   "metadata": {
    "id": "9552ce03-15e7-4381-8166-e7afcbf97e6e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[253 253 253 255]\n",
      "  [254 254 254 255]\n",
      "  [254 254 254 255]\n",
      "  ...\n",
      "  [254 254 254 255]\n",
      "  [254 254 254 255]\n",
      "  [253 253 253 255]]\n",
      "\n",
      " [[254 254 254 255]\n",
      "  [254 254 254 255]\n",
      "  [254 254 254 255]\n",
      "  ...\n",
      "  [254 254 254 255]\n",
      "  [254 254 254 255]\n",
      "  [254 254 254 255]]\n",
      "\n",
      " [[253 253 253 255]\n",
      "  [253 253 253 255]\n",
      "  [254 254 254 255]\n",
      "  ...\n",
      "  [254 254 254 255]\n",
      "  [253 253 253 255]\n",
      "  [253 253 253 255]]\n",
      "\n",
      " ...\n",
      "\n",
      " [[157 159 166 255]\n",
      "  [148 148 151 255]\n",
      "  [135 133 134 255]\n",
      "  ...\n",
      "  [ 98 127 164 255]\n",
      "  [106 134 173 255]\n",
      "  [105 135 173 255]]\n",
      "\n",
      " [[ 31  27  28 255]\n",
      "  [ 26  23  29 255]\n",
      "  [ 16  15  25 255]\n",
      "  ...\n",
      "  [ 98 127 163 255]\n",
      "  [107 135 174 255]\n",
      "  [106 135 172 255]]\n",
      "\n",
      " [[  1   3  35 255]\n",
      "  [  2   4  43 255]\n",
      "  [  3   9  52 255]\n",
      "  ...\n",
      "  [100 127 163 255]\n",
      "  [109 133 173 255]\n",
      "  [109 135 172 255]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f4a238b8d30>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 142928 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import imageio.v3 as imageio\n",
    "\n",
    "ktu = imageio.imread('ktu.png')\n",
    "\n",
    "print(ktu)\n",
    "\n",
    "plt.imshow(ktu)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a48fc16b-cd52-45e4-8658-3054eaa4f209",
   "metadata": {},
   "source": [
    "## Išvestinių skaičiavimas\n",
    "\n",
    "Pakeiskite žingsnio ir kitus parametrus."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "98193916-fbd8-4aab-8ee5-310d37b921c5",
   "metadata": {
    "id": "98193916-fbd8-4aab-8ee5-310d37b921c5",
    "outputId": "8df86f53-5fa0-40aa-e703-15511b1c09eb"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 43264 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "delta_x = 0.2\n",
    "\n",
    "x = np.arange(0, 5.0, delta_x)\n",
    "y = np.cos(x)+np.cos(6*x)\n",
    "\n",
    "dydx = np.diff(y)/delta_x\n",
    "\n",
    "plt.plot(x, y)\n",
    "plt.plot(x[1:]-delta_x/2, dydx)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d30dd171-0b0d-4055-ab57-dc79ed0f2b7b",
   "metadata": {},
   "source": [
    "## Išvestinės skaičiavimas su np.gradient\n",
    "\n",
    "* Kodėl dydx_numerical ir dydx_analytical reikšmės nesutampa?\n",
    "* Pakeiskite y=f(x) funkciją ir braižomus parametrus."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f40c9063-49db-4b8e-b178-bbf9f647b844",
   "metadata": {
    "id": "f40c9063-49db-4b8e-b178-bbf9f647b844",
    "outputId": "da0643a5-2adf-4ce0-c570-ef5e28d9be3a"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 56268 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.linspace(-5,5,20)\n",
    "dx = x[1]-x[0]\n",
    "y = np.sin(x)+np.cos(2*x)\n",
    "dydx_numerical = np.gradient(y, dx)\n",
    "dydx_analytical = np.cos(x)-2*np.sin(2*x)\n",
    "\n",
    "plt.plot(x, y)\n",
    "plt.plot(x, dydx_numerical)\n",
    "plt.plot(x, dydx_analytical)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c41f521b-3e8f-4c00-98a1-06aeed9b8bc2",
   "metadata": {},
   "source": [
    "## Gradientinio lauko radimas\n",
    "\n",
    "Išanalizuokite žemiau pateiktą kodą. Pamėginkite atvaizduoti kitas $z(x,y)$ funkcijas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "04c515ab-3961-4e5e-99cb-1570eb85cdb3",
   "metadata": {
    "id": "04c515ab-3961-4e5e-99cb-1570eb85cdb3",
    "outputId": "fb7d7a1a-03a1-452f-d23b-51d941117857"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.contour.QuadContourSet at 0x7f4a21ec3e20>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 237508 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "feature_x = np.arange(-2, 2, 0.1)\n",
    "feature_y = np.arange(-2, 2, 0.1)\n",
    "\n",
    "x, y = np.meshgrid(feature_x, feature_y)\n",
    "z = x * np.exp(-(x**2 + y**2))\n",
    "u = 1*np.exp(-(x**2 + y**2))+x*np.exp(-(x**2 + y**2))*(-2*x)\n",
    "v = x*np.exp(-(x**2 + y**2))*(-2*y)\n",
    "\n",
    "norm = np.linalg.norm(np.array((u, v)), axis=0)\n",
    "u = u / norm\n",
    "v = v / norm\n",
    "\n",
    "fig, ax = plt.subplots(1, 1)\n",
    "ax.set_aspect(1)\n",
    "ax.quiver(x, y, u, v, units='xy', scale=10, color='gray')\n",
    "ax.contour(x, y, z, 10, cmap='jet')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fb179d7-7295-4339-9b8e-0aaee69af5ac",
   "metadata": {},
   "source": [
    "## MNIST duomenų bazė\n",
    "\n",
    "Pamėginkite išsiaiškinti duomenų tipą ir juos atvaizduoti."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "acafd255-fe26-4de4-b2e4-77758d9d1aae",
   "metadata": {
    "id": "acafd255-fe26-4de4-b2e4-77758d9d1aae",
    "outputId": "563624f0-ca0b-4ef5-fa9c-75312eaa7c7c"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-06-25 18:47:35.461935: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F AVX512_VNNI FMA\n",
      "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TensorFlow version: 2.11.0\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "print(\"TensorFlow version:\", tf.__version__)\n",
    "\n",
    "mnist = tf.keras.datasets.mnist\n",
    "\n",
    "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
    "x_train, x_test = x_train / 255.0, x_test / 255.0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e61e99cf-7584-4bd2-8cbe-31b5738328d3",
   "metadata": {},
   "source": [
    "## Pandas biblioteka\n",
    "\n",
    "Išanalizuokite žemiau pateiktą kodą, pamėginkite rasti įvairių duomenų rinkinių internete ir jiems pritaikyti žemiau esantį kodą. Atlikite duomenų filtravimą, atvaizdavimą ir pritaikykite įvairiais Pandas bibliotekos funkcijas.\n",
    "\n",
    "* https://pandas.pydata.org/docs/user_guide/10min.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a68ba2e0-65e4-4496-9ab7-b2c5eec6e610",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 486,
     "status": "ok",
     "timestamp": 1687705797157,
     "user": {
      "displayName": "Paulius Palevičius",
      "userId": "12080217309158757175"
     },
     "user_tz": -180
    },
    "id": "a68ba2e0-65e4-4496-9ab7-b2c5eec6e610",
    "outputId": "ad4a316a-f189-4347-9abe-59e9a6efd8b1"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A          B    C  D      E    F\n",
      "0  1.0 2013-01-02  1.0  3   test  foo\n",
      "1  1.0 2013-01-02  1.0  3  train  foo\n",
      "2  1.0 2013-01-02  1.0  3   test  foo\n",
      "3  1.0 2013-01-02  1.0  3  train  foo\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df1 = pd.DataFrame({\n",
    "    \"A\": 1.0,\n",
    "    \"B\": pd.Timestamp(\"20130102\"),\n",
    "    \"C\": pd.Series(1, index=list(range(4)), dtype=\"float32\"),\n",
    "    \"D\": np.array([3] * 4, dtype=\"int32\"),\n",
    "    \"E\": pd.Categorical([\"test\", \"train\", \"test\", \"train\"]),\n",
    "    \"F\": \"foo\",\n",
    "})\n",
    "\n",
    "print(df1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "Pvp-0PSpGa0W",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 313
    },
    "executionInfo": {
     "elapsed": 333,
     "status": "error",
     "timestamp": 1687705801232,
     "user": {
      "displayName": "Paulius Palevičius",
      "userId": "12080217309158757175"
     },
     "user_tz": -180
    },
    "id": "Pvp-0PSpGa0W",
    "outputId": "d6a32d1c-ec3a-4b1c-b8c0-1acc39173c03"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21                 Lower De Boom (21st Amendment Brewery)\n",
      "32                    Hop Crisis (21st Amendment Brewery)\n",
      "70             London Balling (Against the Grain Brewery)\n",
      "134                Quakertown Stout (Armadillo Ale Works)\n",
      "171     Devils Tramping Ground Tripel (Aviator Brewing...\n",
      "                              ...                        \n",
      "2279    Lee Hill Series Vol. 4 - Manhattan Style Rye A...\n",
      "2281    Lee Hill Series Vol. 3 - Barrel Aged Imperial ...\n",
      "2286    Upslope Imperial India Pale Ale (Upslope Brewi...\n",
      "2312                   Voodoo Love Child (Voodoo Brewery)\n",
      "2315                            Gran Met (Voodoo Brewery)\n",
      "Name: label, Length: 84, dtype: object\n"
     ]
    }
   ],
   "source": [
    "df2 = pd.read_csv('beers.csv')\n",
    "\n",
    "print(df2[df2.abv>0.09].label)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "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.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
