<jupyter_start><jupyter_text>Linear Regression<jupyter_code>%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (20.0,10.0)

#reading data
data = pd.read_csv('Desktop\headbrain.csv')
print(data.shape)
data.head()
#collecting X and Y
X = data['Head Size(cm^3)'].values
Y = data['Brain Weight(grams)'].values
#mean of X,Y
mean_x = np.mean(X)
mean_y = np.mean(Y)
n = len(X)

# calculating b1 and b2 , y = b1*x + b2

num = 0
den = 0

for i in range(n):
    num += (X[i]-mean_x) * (Y[i]-mean_y)
    den += (X[i]-mean_x) ** 2
b1 = num/den
b2 = mean_y - (b1*mean_x)
print(b1,b2)
#ploting the values
max_x = np.max(X) + 100
min_x = np.min(X) - 100

x = np.linspace(max_x,min_x,1000)
y = b2 + b1*x

#ploting line
plt.plot(x,y,color='red',label='Regression Line')

#ploting scatter points
plt.scatter(X,Y,c='blue', label='Scatter Plot')

plt.xlabel('Head size in cm3')
plt.ylabel('Brain Weight in grams')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Linear Regression using Scikit Learn<jupyter_code>from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

X = X.reshape((n,1))

#creating model
reg = LinearRegression()

#fitting training data
reg = reg.fit(X,Y)

#prediction
Y_pred = reg.predict(X)

#calculating R2 score
r2_score = reg.score(X,Y)

print(r2_score)<jupyter_output>0.639311719957
StarcoderdataJupyterStructuredCleanDedup