Skip to content

Instantly share code, notes, and snippets.

@ritwikraha
Created January 18, 2024 17:54
Show Gist options
  • Save ritwikraha/0e0b3a044a18c03716d96608f38348e5 to your computer and use it in GitHub Desktop.
Save ritwikraha/0e0b3a044a18c03716d96608f38348e5 to your computer and use it in GitHub Desktop.
gradient_descent_script.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMMQS+enI4zpKy9fN3rR7uX",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/ritwikraha/0e0b3a044a18c03716d96608f38348e5/gradient_descent_script.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "iqNLMchoxUUm"
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"def gradient_descent(x, y, theta, num_iterations):\n",
"\n",
" m = len(y) # Number of data points\n",
"\n",
" for i in range(num_iterations):\n",
" # Calculate predictions\n",
" predictions = np.dot(x, theta)\n",
"\n",
" # Calculate errors (residuals)\n",
" errors = predictions - y\n",
"\n",
" # Calculate gradients\n",
" gradients = np.dot(x.T, errors) / m\n",
"\n",
" # Update parameters\n",
" theta = theta * gradients\n",
"\n",
" return theta\n"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment