Tuesday, September 25, 2012

[Python] Ordinary Linear Regression

import numpy as np
import mlpy
import matplotlib.pyplot as plt # required for plotting'
np.random.seed(0)
mean, cov, n = [1, 5], [[1,1],[1,2]], 200     # specify the distribution parameters
d = np.random.multivariate_normal(mean, cov, n) # generate a sequence of vectors
x, y = d[:, 0].reshape(-1, 1), d[:, 1]  # reshape() Gives a new shape to an array without changing its data
ols=mlpy.OLS()
ols.learn(x,y)
xx = np.arange(np.min(x), np.max(x), 0.01).reshape(-1, 1)
yy=ols.pred(xx)
fig=plt.figure(1)
plot=plt.plot(x,y,'o',xx,yy,'--k')  #plot with fitted line added, '--' means dashed line and 'k' means color to be black
plt.show()

No comments:

Post a Comment