machinelearning

Machine learning code
git clone git://git.laack.co/machinelearning.git
Log | Files | Refs

GradientDescentLinearRegression.ipynb (1751B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "code",
      5    "execution_count": 2,
      6    "metadata": {},
      7    "outputs": [],
      8    "source": [
      9     "# Custom implementation of gradient descent for use with linear regression.\n",
     10     "# This would be used in the case of lots of features (100,000+)\n",
     11     "# or lots of samples such that they don't all fit in memory.\n",
     12     "import numpy as np\n",
     13     "from sklearn.preprocessing import add_dummy_feature\n",
     14     "\n",
     15     "# init\n",
     16     "m = 100\n",
     17     "X = 2*np.random.rand(m,1)\n",
     18     "y = 4+3 * X + np.random.randn(m,1)\n",
     19     "X_b = add_dummy_feature(X)\n",
     20     "\n",
     21     "\n",
     22     "# set learning rate and iterations\n",
     23     "lr = .1\n",
     24     "itrs = 1000\n",
     25     "\n",
     26     "# Calculate and move\n",
     27     "m = len(X_b)\n",
     28     "np.random.seed(42)\n",
     29     "theta = np.random.randn(2,1)\n",
     30     "\n",
     31     "for itr in range(itrs):\n",
     32     "    gradients = 2 / m * X_b.T @ (X_b @ theta - y)\n",
     33     "    theta = theta - lr * gradients"
     34    ]
     35   },
     36   {
     37    "cell_type": "code",
     38    "execution_count": 3,
     39    "metadata": {},
     40    "outputs": [
     41     {
     42      "data": {
     43       "text/plain": [
     44        "array([[4.20831857],\n",
     45        "       [2.79226572]])"
     46       ]
     47      },
     48      "execution_count": 3,
     49      "metadata": {},
     50      "output_type": "execute_result"
     51     }
     52    ],
     53    "source": [
     54     "theta"
     55    ]
     56   }
     57  ],
     58  "metadata": {
     59   "kernelspec": {
     60    "display_name": "notebook",
     61    "language": "python",
     62    "name": "notebook"
     63   },
     64   "language_info": {
     65    "codemirror_mode": {
     66     "name": "ipython",
     67     "version": 3
     68    },
     69    "file_extension": ".py",
     70    "mimetype": "text/x-python",
     71    "name": "python",
     72    "nbconvert_exporter": "python",
     73    "pygments_lexer": "ipython3",
     74    "version": "3.11.2"
     75   }
     76  },
     77  "nbformat": 4,
     78  "nbformat_minor": 2
     79 }