Skip to main content
Practice

SciPy vs NumPy


While NumPy and SciPy are closely related, the best way to understand them is to see them in action.
This lesson will walk you through using NumPy for basic computation and then SciPy for an advanced task.


Installing and Importing SciPy

If you’re running this in a Jupyter Lite environment, make sure to install SciPy before importing:

Install SciPy in Jupyter Lite
import piplite
await piplite.install('scipy')

Once installed:

Import NumPy and SciPy
import numpy as np
from scipy import stats

Example: Mean and t-Test

Let’s say you have two datasets and want to compare their means.

Using NumPy for the mean:

Mean with NumPy
data1 = [5.1, 5.5, 5.8, 6.0, 6.2]
data2 = [5.0, 5.1, 5.4, 5.6, 5.9]

mean1 = np.mean(data1)
mean2 = np.mean(data2)

print("Mean of data1:", mean1)
print("Mean of data2:", mean2)

Using SciPy for a statistical test:

Independent t-Test with SciPy
t_stat, p_value = stats.ttest_ind(data1, data2)

print("t-statistic:", t_stat)
print("p-value:", p_value)

With NumPy, you can compute the means. With SciPy, you can directly test whether those means are statistically different — without writing the math yourself.


Key Takeaway

NumPy handles the core math and array operations, while SciPy gives you ready-made scientific and statistical tools to solve complex problems faster.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.