How to use Its Base Formula In Python

In this approach, we divide the datasets into independent variables and dependent variables. we import sklearn.linear_model.LinearRegression(). we fit the data in it and then carry out predictions using predict() method. as the dataset only contains 100 rows train test split is not necessary. 

To view and download the dataset used click here

Python




# import packages
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
  
  
# reading csv file as pandas dataframe
data = pd.read_csv('headbrain2.csv')
  
# independent variable
X = data[['Head Size(cm^3)']]
  
# output variable (dependent)
y = data['Brain Weight(grams)']
  
# using the linear regression model
model = LinearRegression()
  
# fitting the data
model.fit(X, y)
  
# predicting values
y_pred = model.predict(X)
df = pd.DataFrame({'Actual': y, 'Predicted':
y_pred})
  
print(' residual sum of squares is : '+ str(np.sum(np.square(df['Predicted'] - df['Actual']))))


Output:

 residual sum of squares is : 583207.4514802304

How to Calculate Residual Sum of Squares in Python

The residual sum of squares (RSS) calculates the degree of variance in a regression model. It estimates the level of error in the model’s prediction. The smaller the residual sum of squares, the better your model fits your data; the larger the residual sum of squares, the worse. It is the sum of squares of the observed data minus the predicted data. 

Formula:

Similar Reads

Method 1: Using Its Base Formula

In this approach, we divide the datasets into independent variables and dependent variables. we import sklearn.linear_model.LinearRegression(). we fit the data in it and then carry out predictions using predict() method. as the dataset only contains 100 rows train test split is not necessary....

Method 2: Using statsmodel.api

...