Skip to content

Instantly share code, notes, and snippets.

@Linusp
Created September 8, 2014 02:14
Show Gist options
  • Save Linusp/7495f2cec34a6195f47d to your computer and use it in GitHub Desktop.
Save Linusp/7495f2cec34a6195f47d to your computer and use it in GitHub Desktop.
linear regression
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Author : Linusp
Date : 2014/08/30
Description: Linear Regression
"""
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
data = np.loadtxt("linear_regression.txt")
x = np.hstack([np.ones((len(data), 1)), data])
theta=np.array([0.0, 0.0])
alpha = 0.001
m = len(x)
def update():
global x, theta, alpha, m
theta = theta - alpha * np.dot(x[:,0:2].T, np.dot(x[:, 0:2], theta)
- x[:,2]) / m
print "theta is :(%f %f)" %(theta[0], theta[1])
def plot_all_points():
global x, ax1
for point in x:
ax1.plot(point[1], point[2], 'ro')
def animate(i):
global x, ax1, theta
update()
ax1.clear()
plot_all_points()
x_range = np.arange(0, 30, 0.025)
y_range = np.arange(0, 30, 0.025)
X, Y = np.meshgrid(x_range, y_range)
f = theta[0] + theta[1] * X - Y
ax1.contour(X, Y, f, [0], colors=('blue'))
ax1.set_title("Linear regression")
plt.xlim([0, 30])
plt.ylim([0, 30])
if __name__ == '__main__':
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment