|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Linear Algebra In Supervised Learning: \n", |
| 8 | + "\n", |
| 9 | + "Linear Algebra is the basis of building models in machine learning. \n", |
| 10 | + "A very simple example: \n", |
| 11 | + "\n", |
| 12 | + "Predict House Price from its size:\n", |
| 13 | + "\n", |
| 14 | + "Create a math function called **Hypothesis** to predict the price of the house.\n", |
| 15 | + "\n", |
| 16 | + "\\begin{align}\n", |
| 17 | + "\\Large\n", |
| 18 | + "y^i = h_\\theta(x^i) & = \\theta_0 + \\theta_1 x^i \\\\\n", |
| 19 | + "\\end{align}\n", |
| 20 | + "\n", |
| 21 | + "\n", |
| 22 | + "Linear algebra is using as the basis of any math in any machine learning algorithm. \n", |
| 23 | + "\n", |
| 24 | + "The above formula for one input variable. \n", |
| 25 | + "But what if we have multi input variables? \n", |
| 26 | + "\n", |
| 27 | + "\\begin{align}\n", |
| 28 | + "\\Large\n", |
| 29 | + "y^i = h_\\theta(x_1^i, x_2^i, ...) & = \\theta_0 + \\theta_1 x_1^i + \\theta_2 x_2^i + ... \\\\\n", |
| 30 | + "\\end{align}\n" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "## Practical Example: Use Matrix calculation in Supervised Learning\n", |
| 38 | + "\n", |
| 39 | + "\n", |
| 40 | + "\n" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "markdown", |
| 45 | + "metadata": {}, |
| 46 | + "source": [ |
| 47 | + "### Iris dataset in Scikit-learn module:\n", |
| 48 | + "scikit-learn comes with a few standard datasets, and iris is one of them. \n", |
| 49 | + "It is formatted in a way to be used by machine learning. " |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 1, |
| 55 | + "metadata": {}, |
| 56 | + "outputs": [ |
| 57 | + { |
| 58 | + "name": "stdout", |
| 59 | + "output_type": "stream", |
| 60 | + "text": [ |
| 61 | + "type of iris: <class 'sklearn.utils.Bunch'>\n", |
| 62 | + "dict keys: dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names', 'filename'])\n", |
| 63 | + "type of iris data: <class 'numpy.ndarray'>\n", |
| 64 | + "iris column names: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']\n", |
| 65 | + "the target: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", |
| 66 | + " 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", |
| 67 | + " 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2\n", |
| 68 | + " 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\n", |
| 69 | + " 2 2]\n" |
| 70 | + ] |
| 71 | + } |
| 72 | + ], |
| 73 | + "source": [ |
| 74 | + "from sklearn import datasets\n", |
| 75 | + "iris_sk = datasets.load_iris()\n", |
| 76 | + "print ('type of iris: ', type(iris_sk))\n", |
| 77 | + "print ('dict keys: ' , iris_sk.keys())\n", |
| 78 | + "print ('type of iris data: ', type(iris_sk.data))\n", |
| 79 | + "print ('iris column names: ' , iris_sk.feature_names)\n", |
| 80 | + "print ('the target: ', iris_sk.target)" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": 2, |
| 86 | + "metadata": {}, |
| 87 | + "outputs": [], |
| 88 | + "source": [ |
| 89 | + "import numpy as np\n", |
| 90 | + "\n", |
| 91 | + "X = np.asmatrix(iris_sk.data)\n", |
| 92 | + "\n", |
| 93 | + "x = np.transpose(X)\n", |
| 94 | + "\n", |
| 95 | + "T = x.dot(X)\n", |
| 96 | + "\n", |
| 97 | + "inv = np.linalg.inv(T)\n", |
| 98 | + "\n", |
| 99 | + "theta = (inv.dot(X.T)).dot(iris_sk.target)" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": 3, |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [ |
| 107 | + { |
| 108 | + "data": { |
| 109 | + "text/plain": [ |
| 110 | + "matrix([[-0.0844926 , -0.02356211, 0.22487123, 0.59972247]])" |
| 111 | + ] |
| 112 | + }, |
| 113 | + "execution_count": 3, |
| 114 | + "metadata": {}, |
| 115 | + "output_type": "execute_result" |
| 116 | + } |
| 117 | + ], |
| 118 | + "source": [ |
| 119 | + "theta" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": null, |
| 125 | + "metadata": {}, |
| 126 | + "outputs": [], |
| 127 | + "source": [] |
| 128 | + } |
| 129 | + ], |
| 130 | + "metadata": { |
| 131 | + "kernelspec": { |
| 132 | + "display_name": "Python 3", |
| 133 | + "language": "python", |
| 134 | + "name": "python3" |
| 135 | + }, |
| 136 | + "language_info": { |
| 137 | + "codemirror_mode": { |
| 138 | + "name": "ipython", |
| 139 | + "version": 3 |
| 140 | + }, |
| 141 | + "file_extension": ".py", |
| 142 | + "mimetype": "text/x-python", |
| 143 | + "name": "python", |
| 144 | + "nbconvert_exporter": "python", |
| 145 | + "pygments_lexer": "ipython3", |
| 146 | + "version": "3.6.8" |
| 147 | + } |
| 148 | + }, |
| 149 | + "nbformat": 4, |
| 150 | + "nbformat_minor": 2 |
| 151 | +} |
0 commit comments