Sunday, April 5, 2020

linear regression in matplotlib in python

Linear regression

This is the topic related to Linear regression using python
In [4]:
from scipy.stats import norm
import matplotlib.pyplot as plt
import numpy as np


page_speeds = np.random.normal(3.0,1.0,1000)
purchase_amount = 100 - (page_speeds + np.random.normal(0,0.1, 1000))*3

plt.scatter(page_speeds,purchase_amount)
Out[4]:
<matplotlib.collections.PathCollection at 0x277ffa83348>
In [5]:
from scipy import stats 

slope, intercept, r_value, p_value, std_err = stats.linregress(page_speeds,purchase_amount)
In [6]:
r_value**2
Out[6]:
0.9904525550737715
In [7]:
import matplotlib.pyplot as plt

def predict(x):
    return slope * x + intercept

fit_line = predict(page_speeds)
plt.scatter(page_speeds,purchase_amount)
plt.plot(page_speeds,fit_line, c='r')
plt.show()

python class topic video