machinelearning

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit cfc8c125e9850e1f70183273adb744aae1344f01
Author: Andrew <andrewlaack1@gmail.com>
Date:   Thu, 18 Apr 2024 17:46:44 -0500

Added code for gradient descent from notes

Diffstat:
AGradientDescentThirdDegreePolynomial.py | 47+++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+), 0 deletions(-)

diff --git a/GradientDescentThirdDegreePolynomial.py b/GradientDescentThirdDegreePolynomial.py @@ -0,0 +1,47 @@ +import random +print("ax^3 + bx^2 + cx + d") + +a = float(input("Enter a: ")) +b = float(input("Enter b: ")) +c = float(input("Enter c: ")) +d = float(input("Enter d: ")) + + +def calculateValue(x): + return x**3 * a + x**2 * b + x * c + d + + +def printResult(x, y): + print("x: " + str(x) + " y: " + str(y)) + + +# Find the x at the bottom +def descend(x, depth): + + if depth > 100: + return x + + midX = x + rightX = x + .00000001 + leftX = x - .00000001 + + midY = calculateValue(midX) + rightY = calculateValue(rightX) + leftY = calculateValue(leftX) + + printResult(leftX, leftY) + printResult(midX, midY) + printResult(rightX, rightY) + + depth += 1 + if rightY > leftY: + return descend(x - (1 / depth), depth) + else: + return descend(x + (1/depth), depth) + + +currSearch = random.random() * 10 +xVal = descend(currSearch, 0) + +printResult(xVal, calculateValue(xVal)) +