What is NumPy?
NumPy stands for Numerical Python. It is a fundamental package used for numerical and scientific computing in Python.
- It allows you to work with multidimensional arrays (matrices) efficiently.
- NumPy provides a large library of mathematical functions that can operate on arrays directly.
To use it:
NumPy Arrays
What is a NumPy Array?
- A NumPy array is like a list, but with fixed size, same data type, and much faster computation.
- It can be 1D, 2D, or even multi-dimensional.
Creating Arrays
Array Attributes
a.shape
→ Returns the dimensions of the arraya.dtype
→ Data type of elementsa.ndim
→ Number of dimensions
Built-in Functions to Create Arrays
Math Functions in NumPy
NumPy has built-in vectorized operations, which means you don’t need to write loops to do math.
Basic Arithmetic
Universal Functions (ufuncs)
These functions apply element-wise on arrays
np.sqrt(a)
→ Square rootnp.exp(a)
→ Exponentialnp.log(a)
→ Natural logarithmnp.abs(a)
→ Absolute valuenp.power(a, 2)
→ Power
Aggregation Functions
These functions help you summarize data:
np.sum(a)
→ Total sumnp.mean(a)
→ Averagenp.median(a)
→ Middle valuenp.std(a)
→ Standard deviationnp.max(a)
/np.min(a)
→ Max and min
Example:
Feature | Benefit |
---|---|
Arrays | Handle large datasets efficiently |
Vectorization | Perform math operations without loops |
ufuncs | Apply fast element-wise math functions |
Aggregations | Summarize and analyze data easily |
Summary
- NumPy arrays are fast and efficient containers for numerical data.
- You can use NumPy to do everything from basic arithmetic to complex statistical calculations.