{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from sympy import *\n", "init_printing()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "X = Matrix([[1, 10],\n", " [1, 8],\n", " [1, 13],\n", " [1, 9],\n", " [1, 11],\n", " [1, 14],\n", " [1, 6],\n", " [1, 4],\n", " [1, 12],\n", " [1, 7],\n", " [1, 5]])\n", "\n", "y = Matrix([[8.04],\n", " [6.95],\n", " [7.58],\n", " [8.81],\n", " [8.33],\n", " [9.96],\n", " [7.24],\n", " [4.26],\n", " [10.84],\n", " [4.82],\n", " [5.68]])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left[\\begin{matrix}1 & 10\\\\1 & 8\\\\1 & 13\\\\1 & 9\\\\1 & 11\\\\1 & 14\\\\1 & 6\\\\1 & 4\\\\1 & 12\\\\1 & 7\\\\1 & 5\\end{matrix}\\right]$$" ], "text/plain": [ "⎡1 10⎤\n", "⎢ ⎥\n", "⎢1 8 ⎥\n", "⎢ ⎥\n", "⎢1 13⎥\n", "⎢ ⎥\n", "⎢1 9 ⎥\n", "⎢ ⎥\n", "⎢1 11⎥\n", "⎢ ⎥\n", "⎢1 14⎥\n", "⎢ ⎥\n", "⎢1 6 ⎥\n", "⎢ ⎥\n", "⎢1 4 ⎥\n", "⎢ ⎥\n", "⎢1 12⎥\n", "⎢ ⎥\n", "⎢1 7 ⎥\n", "⎢ ⎥\n", "⎣1 5 ⎦" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left[\\begin{matrix}8.04\\\\6.95\\\\7.58\\\\8.81\\\\8.33\\\\9.96\\\\7.24\\\\4.26\\\\10.84\\\\4.82\\\\5.68\\end{matrix}\\right]$$" ], "text/plain": [ "⎡8.04 ⎤\n", "⎢ ⎥\n", "⎢6.95 ⎥\n", "⎢ ⎥\n", "⎢7.58 ⎥\n", "⎢ ⎥\n", "⎢8.81 ⎥\n", "⎢ ⎥\n", "⎢8.33 ⎥\n", "⎢ ⎥\n", "⎢9.96 ⎥\n", "⎢ ⎥\n", "⎢7.24 ⎥\n", "⎢ ⎥\n", "⎢4.26 ⎥\n", "⎢ ⎥\n", "⎢10.84⎥\n", "⎢ ⎥\n", "⎢4.82 ⎥\n", "⎢ ⎥\n", "⎣5.68 ⎦" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "v = Inverse(X.T * X) * X.T * y" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left[\\begin{matrix}3.00009090909091\\\\0.500090909090909\\end{matrix}\\right]$$" ], "text/plain": [ "⎡3.00009090909091 ⎤\n", "⎢ ⎥\n", "⎣0.500090909090909⎦" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "#v = (b, m), or (intercept, slope)\n", "#So, the best fit line is given by (approximately) y = 0.5x + 3" ] } ], "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.6.7" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 2 }