Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- def estimate_coef(x, y):
- # number of observations/points
- n = np.size(x)
- # mean of x and y vector
- m_x = np.mean(x)
- m_y = np.mean(y)
- # calculating cross-deviation and deviation about x
- SS_xy = np.sum(y * x) - n * m_y * m_x
- SS_xx = np.sum(x * x) - n * m_x * m_x
- # calculating regression coefficients
- b_1 = SS_xy / SS_xx
- b_0 = m_y - b_1 * m_x
- print("b_0", b_0)
- print("b_1", b_1)
- return (b_0, b_1)
- def plot_regression_line(x, y, b):
- # plotting the actual points as scatter plot
- plt.scatter(x, y, color="m", marker="o", s=30)
- # predicted response vector
- y_pred = b[0] + b[1] * x
- # plotting the regression line
- plt.plot(x, y_pred, color="g")
- # putting labels
- plt.xlabel('x')
- plt.ylabel('y')
- # observations / data
- x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
- y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
- # estimating coefficients
- b = estimate_coef(x, y)
- plot_regression_line(x, y, b)
- output:
- https://ibb.co/xmMGCbm
- # Predict the speed of a 10-year old car.
- import matplotlib.pyplot as plt
- from scipy import stats
- x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
- y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
- slope, intercept, r, p, std_err = stats.linregress(x, y)
- def myfunc(x):
- return slope * x + intercept
- mymodel = list(map(myfunc, x))
- plt.scatter(x, y)
- plt.plot(x, mymodel)
- plt.show()
- speed = myfunc(10)
- print(speed)
- # Example of bad-fit
- import matplotlib.pyplot as plt
- from scipy import stats
- x = [89,43,36,36,95,10,66,34,38,20,26,29,48,
- 64,6 ,5 ,36 ,66 ,72 ,40]
- y = [21 ,46 ,3 ,35 ,67 ,95 ,53 ,72 ,58 ,10 ,
- 26 ,34 ,90 ,33 ,38 ,20 ,56 ,2 ,47 ,15]
- slope, intercept,r,p,std_err = stats.linregress(x,y)
- def myfunc(x):
- return slope * x + intercept
- mymodel = list(map(myfunc,x))
- plt.scatter(x,y)
- plt.plot(x,mymodel)
- plt.show()
- print(r)
- output:
- https://ibb.co/3F6nXwZ
- https://ibb.co/k3jv8xM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement