diff --git a/README.md b/README.md new file mode 100644 index 0000000..0445aa2 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Mathematics and statistics for data science diff --git a/matrix.ipynb b/matrix.ipynb new file mode 100644 index 0000000..682243f --- /dev/null +++ b/matrix.ipynb @@ -0,0 +1,491 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Linear algebra" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(array([1, 2, 3]), array([4, 5, 6]))" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = np.array([1,2,3])\n", + "b = np.array([4,5,6])\n", + "a,b" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(3,)" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Element-wise(also known as the Hadamard product) multiplication using loop" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 4. 10. 18.]\n" + ] + } + ], + "source": [ + "n = a.shape[0]\n", + "res = np.zeros(shape=n)\n", + "for i in range(n):\n", + " res[i] = a[i] * b[i]\n", + "print(res)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Element-wise multiplication using opeartor" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 4, 10, 18])" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a * b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dot Product" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Calculate dot product using loop" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "32\n" + ] + } + ], + "source": [ + "n = a.shape[0]\n", + "res = 0\n", + "for i in range(n):\n", + " res += a[i] * b[i]\n", + "print(res)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Calculate dot product using in-built function" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "32" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.dot(a,b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using @ opeartor" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "32" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a @ b" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(array([1, 2, 3]), array([4, 5, 6]))" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a,b" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(array([[1, 2, 3],\n", + " [4, 5, 6]]),\n", + " array([[1, 2],\n", + " [3, 4],\n", + " [5, 6]]))" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = np.array([[1,2,3],[4,5,6]])\n", + "b = np.array([[1,2],[3,4],[5,6]])\n", + "a,b" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((2, 3), (3, 2))" + ] + }, + "execution_count": 90, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.shape,b.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[22, 28],\n", + " [49, 64]])" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.matmul(a,b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Transpose" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 4],\n", + " [2, 5],\n", + " [3, 6]])" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.T" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 4],\n", + " [2, 5],\n", + " [3, 6]])" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.transpose(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 4],\n", + " [2, 5],\n", + " [3, 6]])" + ] + }, + "execution_count": 104, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r,c = a.shape\n", + "a_t = np.zeros(shape=(c,r),dtype=int)\n", + "row,col = 0,0\n", + "for i in range(c):\n", + " col = 0\n", + " for j in range(r):\n", + " a_t[row][col] = a[j][i]\n", + " col +=1\n", + " row +=1\n", + "a_t" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## EigenValue" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A - (lambda * I) = 0" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2],\n", + " [3, 4]])" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c = np.array([[1,2],[3,4]])\n", + "c" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": {}, + "outputs": [], + "source": [ + "I = np.array([[1,0],[0,1]])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.11.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/practical.ipynb b/practical.ipynb new file mode 100644 index 0000000..9df84d2 --- /dev/null +++ b/practical.ipynb @@ -0,0 +1,156 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You have a dataset with three features (columns) and four data points (rows). Construct a \n", + "4×3 matrix to represent this data. How would adding a bias term modify this matrix?" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original Matrix:\n", + " [[1 2 3]\n", + " [4 5 6]\n", + " [7 8 9]]\n", + "Modified array after adding bias term:\n", + " [[ 3 4 5]\n", + " [ 6 7 8]\n", + " [ 9 10 11]]\n" + ] + } + ], + "source": [ + "a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n", + "print(f\"Original Matrix:\\n {a}\")\n", + "print(f\"Modified array after adding bias term:\\n {a+2}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q3: Construct two matrices,A and 𝐵 of shapes 3×2 and 2×3 respectively. Compute the product AB and describe the result.\n", + "Can you compute BA? Why or why not?\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(array([[1, 2],\n", + " [3, 4],\n", + " [5, 6]]),\n", + " array([[1, 2, 3],\n", + " [4, 5, 6]]))" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = np.array([[1, 2], [3, 4], [5, 6]])\n", + "b = np.array([[1, 2, 3], [4, 5, 6]])\n", + "a, b" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[9, 12, 15], [19, 26, 33], [29, 40, 51]]" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "rows_a = a.shape[0]\n", + "cols_a = a.shape[1]\n", + "cols_b = b.shape[1]\n", + "\n", + "result = [[0 for _ in range(cols_b)] for _ in range(rows_a)]\n", + "\n", + "for i in range(rows_a):\n", + " for j in range(cols_b):\n", + " for k in range(cols_a):\n", + " result[i][j] += a[i][k] * b[k][j]\n", + "result" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 9, 12, 15],\n", + " [19, 26, 33],\n", + " [29, 40, 51]])" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.dot(a, b)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.11.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}