Skip to content

Instantly share code, notes, and snippets.

@adusak
Created June 2, 2015 18:36
Show Gist options
  • Save adusak/e4afdaf5fa286d85c430 to your computer and use it in GitHub Desktop.
Save adusak/e4afdaf5fa286d85c430 to your computer and use it in GitHub Desktop.
Regression
import matplotlib.pyplot as plt
import numpy as np
def generate_points(a, b, number_points=500):
x_points = np.random.normal(0, np.random.randint(0, number_points / 10), number_points)
y_points = (a * x_points + b) + np.random.normal(0, number_points / 2, number_points)
return np.array(x_points, dtype=float), np.array(y_points, dtype=float)
def linear_regression_analytical(x, y, plot=False):
n = len(x)
numer = n * sum(x * y) - sum(x) * sum(y)
denom = n * sum(x ** 2) - sum(x) ** 2
a = numer / denom
b = sum(y) / n - a * (sum(x) / n)
print("y = " + str(a) + "x + " + str(b))
if plot:
plt.scatter(x, y)
plt.plot(x, a * x + b, color="green")
plt.savefig("analytical.png")
plt.show()
return a, b
def gradient_descent(start_a, start_b, x, y, iterations, step, plot=False):
number_points = len(x)
result_a, result_b = start_a, start_b
for i in range(iterations):
if i % 500 == 0 and plot:
plt.plot(x, result_a * x + result_a, color="red")
a_gradient = sum(-(2 / number_points) * x * (y - (result_a * x + result_b)))
b_gradient = sum(-(2 / number_points) * (y - (result_a * x + result_b)))
result_a = result_a - (step * a_gradient)
result_b = result_b - (step * b_gradient)
print("Gradient descent => y = " + str(result_a) + "x + " + str(result_b))
if plot:
plt.scatter(x, y)
plt.plot(x, result_a * x + result_a, color="green")
plt.savefig("gradient_descent.png")
plt.show()
def test_linreg():
a = 2.5
b = 1.4
points = generate_points(a, b, 100)
print("Original a:", a, "b:", b)
plt.plot(points[0], a * points[0] + b, color="red")
print("Calculated a,b:", linear_regression_analytical(points[0], points[1], plot=True))
def load_file(name):
file = open(name)
read_file = file.read()
lines = read_file.split("\n")
x = []
y = []
for line in lines:
xy = line.split(" ")
x.append(float(xy[0]))
y.append(float(xy[1]))
return np.array(x, dtype=float), np.array(y, dtype=float)
# x, y = load_file("exfiles/oldfailthful.txt")
# #linear_regression_analytical(x, y, True)
#
# a = 2.5
# b = 1.4
# o, p = generate_points(a, b, 100)
#
# gradient_descent(5, 0.5, o, p, 6000, 0.0001, plot=True)
#
# #test_linreg()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment