How to Calculate Residual Sum of Squares in Python Last Updated : 07 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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:Method 1: Using Its Base FormulaIn 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.4514802304Method 2: Using statsmodel.apiIn this approach, we import the statsmodel.api. After reading the datasets, similar to the previous approach we separate independent and dependent features. We fit them in sm.OLS() regression model. This model has a summary method that gives the summary of all metrics and regression results. model.ssr gives us the value of the residual sum of squares(RSS). We can see that the value we derived from the previous approach is the same as model.ssr value. To view and download the dataset used click here. Python # import packages import pandas as pd import numpy as np import statsmodels.api as sm # 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)'] # adding constant x = sm.add_constant(x) #fit linear regression model model = sm.OLS(y, x).fit() #display model summary print(model.summary()) # residual sum of squares print(model.ssr) Output:583207.4514802304 Comment More infoAdvertise with us Next Article How to Calculate Residual Sum of Squares in Python S sarahjane3102 Follow Improve Article Tags : Machine Learning AI-ML-DS Python-numpy Python-pandas python +1 More Practice Tags : Machine Learningpython Similar Reads How to Calculate Studentized Residuals in Python? Studentized residual is a statistical term and it is defined as the quotient obtained by dividing a residual by its estimated standard deviation. This is a crucial technique used in the detection of outlines. Practically, one can claim that any type of observation in a dataset having a studentized r 4 min read How to calculate the sum of squares? The number system includes different types of numbers for example prime numbers, odd numbers, even numbers, rational numbers, whole numbers, etc. These numbers can be expressed in the form of figures as well as words accordingly. For example, numbers like 40 and 65 expressed in the form of figures c 5 min read How to Calculate SMAPE in Python? In this article, we will see how to compute one of the methods to determine forecast accuracy called the Symmetric Mean Absolute Percentage Error (or simply SMAPE) in Python. The SMAPE is one of the alternatives to overcome the limitations with MAPE forecast error measurement. In contrast to the me 3 min read Residual Sum of Squares Residual Sum of Squares is essentially the sum of the squared differences between the actual values of the dependent variable and the values predicted by the model. This metric provides a numerical representation of how well the model fits the data, with smaller values indicating a better fit and la 6 min read Get the Least squares fit of Hermite series to data in Python In this article, we will discuss how to find the Least-squares fit of the Hermite series to data in Python and NumPy. NumPy.polynomials.hermite.hermfit method The Hermite series is an orthogonal polynomial sequence that has its applications in physics, wave theory, numerical analysis, and signal pro 4 min read Like