NumPy helps us work with numbers and data fast and easily. It is the base for many tools that do science and math with computers.
0
0
NumPy and scientific computing ecosystem
Introduction
When you want to handle large lists of numbers quickly.
When you need to do math on whole groups of numbers at once.
When you want to use other tools that need NumPy to work.
When you want to create graphs or do statistics with data.
When you want to solve science or engineering problems using code.
Syntax
NumPy
import numpy as np # Create an array arr = np.array([1, 2, 3]) # Use functions from NumPy mean = np.mean(arr) sum_ = np.sum(arr)
NumPy arrays are like lists but faster and can do math easily.
Most scientific Python tools use NumPy arrays to work with data.
Examples
This makes a simple list of numbers as a NumPy array.
NumPy
import numpy as np # Create a 1D array arr1 = np.array([10, 20, 30])
This makes a table of numbers with rows and columns.
NumPy
import numpy as np # Create a 2D array (matrix) arr2 = np.array([[1, 2], [3, 4]])
Calculate the average and total of numbers in the array.
NumPy
import numpy as np arr1 = np.array([10, 20, 30]) # Use NumPy functions mean_val = np.mean(arr1) sum_val = np.sum(arr1)
Sample Program
This program shows how to create a 2D array and do simple math on it. It finds the average of all numbers, then sums by columns and rows.
NumPy
import numpy as np # Create a 2D array representing data data = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]]) # Calculate the mean of all numbers mean_all = np.mean(data) # Calculate the sum of each column sum_columns = np.sum(data, axis=0) # Calculate the sum of each row sum_rows = np.sum(data, axis=1) print(f"Mean of all data: {mean_all}") print(f"Sum of each column: {sum_columns}") print(f"Sum of each row: {sum_rows}")
OutputSuccess
Important Notes
NumPy arrays are faster than regular Python lists for math operations.
Many scientific libraries like pandas, matplotlib, and scikit-learn use NumPy arrays.
Remember to import NumPy as np to use its functions easily.
Summary
NumPy is the base tool for fast number and data handling in Python.
It provides arrays and math functions that other science tools use.
Learning NumPy helps you work with data and do scientific computing easily.