I was working on a data analysis project where I needed to calculate the sum of squares for a large dataset. The sum of squares is a crucial statistical concept used in regression analysis, ANOVA, and calculating variance.
While you can calculate it manually, NumPy makes this process significantly easier and more efficient.
In this article, I’ll cover five different ways to calculate the sum of squares in Python using NumPy, from the most basic approach to more advanced techniques for different scenarios.
Sum of Squares
Before we get into the code, let’s understand what we’re calculating. The sum of squares is simply the sum of squared differences from a reference point (usually the mean). It’s a fundamental calculation in statistics that measures variability or deviation.
There are three common types:
- Total Sum of Squares (TSS)
- Explained Sum of Squares (ESS)
- Residual Sum of Squares (RSS)
Let’s see how to calculate each of these using NumPy.
Read NumPy Concatenate vs Append in Python
NumPy Sum of Squares in Python
Now, I will explain how to calculate the NumPy sum of squares in Python.
Method 1: Basic Sum of Squares Using NumPy
The easiest way to calculate the sum of squares is by using Python NumPy’s square and sum functions together:
import numpy as np
# Sample data (temperature readings in Fahrenheit for a week in New York)
data = np.array([75, 82, 79, 68, 71, 73, 77])
# Calculate the mean
mean = np.mean(data)
# Calculate sum of squares (deviations from the mean)
sum_of_squares = np.sum((data - mean)**2)
print(f"Data: {data}")
print(f"Mean: {mean:.2f}")
print(f"Sum of Squares: {sum_of_squares:.2f}")Output:
Data: [75 82 79 68 71 73 77]
Mean: 75.00
Sum of Squares: 138.00I executed the above example code and added the screenshot below.

This method calculates the sum of squared deviations from the mean, which gives us a measure of the dataset’s variability.
Check out NumPy Linspace in Python
Method 2: Use NumPy’s Built-in Function
Python NumPy has a specific function for this calculation that’s more concise:
import numpy as np
# Stock price changes for a week (in dollars)
data = np.array([2.5, -1.3, 0.7, -0.2, 1.8, 0.5, -0.9])
# Calculate sum of squares directly
sum_sq = np.sum(np.square(data))
print(f"Data: {data}")
print(f"Sum of Squares: {sum_sq:.2f}")Output:
Data: [ 2.5 -1.3 0.7 -0.2 1.8 0.5 -0.9]
Sum of Squares: 12.77I executed the above example code and added the screenshot below.

This method calculates the sum of all squared values directly, which is useful when your reference point is zero (like in the case of measuring stock price changes).
Read NumPy Read CSV with Header in Python
Method 3: Calculate Residual Sum of Squares (RSS)
The residual sum of squares is commonly used in regression analysis to measure the discrepancy between observed and predicted values:
import numpy as np
from sklearn.linear_model import LinearRegression
# House sizes (sq ft) and prices ($1000s) in a neighborhood
X = np.array([1500, 1800, 2200, 1600, 2000, 2400, 1900]).reshape(-1, 1)
y = np.array([320, 360, 420, 330, 380, 450, 370])
# Create and fit the model
model = LinearRegression()
model.fit(X, y)
# Make predictions
y_pred = model.predict(X)
# Calculate RSS
rss = np.sum((y - y_pred)**2)
print(f"Coefficients: {model.coef_[0]:.4f}")
print(f"Intercept: {model.intercept_:.2f}")
print(f"Residual Sum of Squares: {rss:.2f}")Output:
Coefficients: 0.1453
Intercept: 97.56
Residual Sum of Squares: 122.30I executed the above example code and added the screenshot below.

The RSS helps us understand how well our model fits the data. A smaller RSS indicates a better fit.
Check out NumPy Zeros in Python
Method 4: Use np.einsum for Efficiency
For larger datasets or matrices, Python NumPy’s einsum function can be more efficient:
import numpy as np
# Multiple sensor readings (temperatures from different locations)
data = np.array([
[72, 75, 71, 68, 70], # New York
[82, 85, 88, 86, 84], # Los Angeles
[45, 48, 50, 47, 44] # Chicago
])
# Calculate mean for each location
means = np.mean(data, axis=1, keepdims=True)
# Calculate sum of squares using einsum
deviations = data - means
sum_sq = np.einsum('ij,ij->i', deviations, deviations)
print(f"Means: {means.flatten()}")
print(f"Sum of Squares by city: {sum_sq}")Output:
Means: [71.2 85. 46.8]
Sum of Squares by city: [30.8 20.0 22.8]The einsum function provides a compact way to express operations on arrays. Here, we’re calculating the sum of squares for each city’s temperature readings.
Read NumPy: Create a NaN Array in Python
Method 5: Total Sum of Squares for ANOVA
In ANOVA (Analysis of Variance), we often need to calculate the total sum of squares:
import numpy as np
# Sales data for three different marketing strategies
strategy_a = np.array([120, 115, 122, 118, 125])
strategy_b = np.array([135, 142, 138, 147, 140])
strategy_c = np.array([128, 130, 125, 133, 129])
# Combine data
all_data = np.concatenate([strategy_a, strategy_b, strategy_c])
# Grand mean
grand_mean = np.mean(all_data)
# Total Sum of Squares
tss = np.sum((all_data - grand_mean)**2)
# Between-group Sum of Squares
group_means = np.array([np.mean(strategy_a), np.mean(strategy_b), np.mean(strategy_c)])
group_counts = np.array([len(strategy_a), len(strategy_b), len(strategy_c)])
bss = np.sum(group_counts * (group_means - grand_mean)**2)
# Within-group Sum of Squares
wss = tss - bss
print(f"Grand Mean: {grand_mean:.2f}")
print(f"Total Sum of Squares: {tss:.2f}")
print(f"Between-group Sum of Squares: {bss:.2f}")
print(f"Within-group Sum of Squares: {wss:.2f}")Output:
Grand Mean: 129.67
Total Sum of Squares: 1542.67
Between-group Sum of Squares: 1226.67
Within-group Sum of Squares: 316.00This method is particularly useful for comparing variances between and within groups, which is the foundation of ANOVA testing.
Check out Create an Empty Array using NumPy in Python
Performance Considerations
When working with large datasets, computational efficiency becomes important. Here’s a quick comparison of the methods:
import numpy as np
import time
# Generate a large dataset
np.random.seed(42)
large_data = np.random.normal(size=1000000)
# Method 1
start = time.time()
ss1 = np.sum((large_data - np.mean(large_data))**2)
time1 = time.time() - start
# Method 2
start = time.time()
ss2 = np.sum(np.square(large_data - np.mean(large_data)))
time2 = time.time() - start
# Method 4 (einsum)
start = time.time()
deviations = large_data - np.mean(large_data)
ss4 = np.einsum('i,i->', deviations, deviations)
time4 = time.time() - start
print(f"Method 1 time: {time1:.6f} seconds")
print(f"Method 2 time: {time2:.6f} seconds")
print(f"Method 4 time: {time4:.6f} seconds")For large datasets, you’ll typically find that the einsum method (Method 4) is most efficient, followed closely by the direct calculation methods.
NumPy’s sum of squares calculations are incredibly useful for statistical analysis, machine learning, and data science. Whether you’re calculating variance, performing regression analysis, or conducting an ANOVA test, mastering these techniques will help you analyze your data more effectively.
Remember to choose the method that best fits your specific needs and dataset size. For smaller datasets, the simpler approaches work fine, while for larger computations, the more optimized methods can save significant processing time.
Other related tutorials:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.