Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import numpy as np
- import matplotlib.pyplot as plt
- x = []
- y = []
- # np.random.normal(loc(mean),scale(STDEV))
- for i in range(100):
- x.append(random.uniform(0, 2))
- y.append(4+3*x[i])
- y[i] += np.random.normal(y[i], 1)
- # plot x and y using blue circle markers
- plt.plot(x,y,'bo')
- plt.xlabel("x-axis")
- plt.ylabel("y-axis")
- plt.title("Y = 4 + 3X + randn with linear regression")
- # Stack arrays in sequence vertically and transpose matrix
- Xb = np.vstack([x, np.ones(len(x))]).T
- # method of least squares to get fitted line
- b2, b1 = np.linalg.lstsq(Xb, y)[0]
- X = np.array(x)
- # red line
- plt.plot(X, b2*X + b1, 'r')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement