NumPy in Python
What is NumPy?
NumPy (Numerical Python) is a powerful Python library used for numerical computing. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently.
Why Use NumPy?
- High Performance: NumPy operations are faster than traditional Python lists.
- Memory Efficient: Uses less memory compared to Python lists.
- Convenient: Provides many built-in mathematical functions.
- Supports Multidimensional Arrays: Enables handling of complex data structures.
- Used in Data Science & AI: Core library for machine learning and scientific computing.
NumPy Array Creation Example
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)
Data Types in NumPy
NumPy supports various data types (dtypes) which define the type of elements in an array:
int_– Integerfloat_– Floating pointcomplex_– Complex numbersbool_– Boolean (True/False)str_– Stringobject_– Python objects
Example:
arr = np.array([1, 2, 3], dtype='float32')
print(arr.dtype)
Common Inbuilt Methods in NumPy
Array Creation Functions
np.array()np.zeros()np.ones()np.arange()np.linspace()
Mathematical Functions
np.sum()np.mean()np.min()np.max()np.std()
Array Operations
np.reshape()np.transpose()np.concatenate()np.split()
Statistical Functions
np.median()np.var()np.corrcoef()
Example of NumPy Operations
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # Addition
print(a * b) # Multiplication
print(np.mean(a))
Conclusion
NumPy is an essential library for anyone working with data in Python. Its speed, efficiency, and powerful functionalities make it a foundation for data science, machine learning, and scientific computing.
Comments
Post a Comment