numpy
numpy
NumPy (Numerical Python) is a powerful open-source library in Python used for numerical
computa ons. It provides support for arrays, matrices, and many mathema cal func ons to
operate on these data structures efficiently.
It is the founda on of the scien fic Python ecosystem and is widely used in data science,
machine learning, and scien fic compu ng.
Features of NumPy
o NumPy provides the ndarray object, which is a fast and efficient array object for
handling large datasets.
o Provides a wide range of mathema cal func ons like sin, cos, log, exp, etc., that
operate on arrays.
3. Broadcas ng:
4. Linear Algebra:
o Includes func ons for matrix mul plica on, eigenvalue computa on, and more.
o Provides tools for genera ng random numbers from various distribu ons.
o Works seamlessly with libraries like Pandas, SciPy, Matplotlib, and Scikit-learn.
Advantages of NumPy
1. Efficiency:
2. Memory Efficiency:
3. Ease of Use:
4. Versa lity:
o Supports a wide range of mathema cal and sta s cal opera ons.
5. Community Support:
o Large community and extensive documenta on make it easy to learn and use.
Disadvantages of NumPy
o NumPy is designed for numerical computa ons and does not support non-numeric
data types efficiently.
3. No GPU Support:
o NumPy does not na vely support GPU accelera on, which can be a limita on for
large-scale computa ons.
Crea on of Arrays
NumPy provides a powerful data structure called the ndarray (n-dimensional array), which
allows for efficient storage and manipula on of large datasets.
An ndarray is a grid of values, all of the same type, and is indexed by a tuple of nonnega ve
integers. The number of dimensions is called the rank of the array, and the size of each
dimension is called the shape.
Crea ng arrays in NumPy is the founda onal step for numerical computa ons. NumPy
provides several ways to create arrays, which can hold data of a uniform type (homogeneous).
The most common methods include using lists, tuples, or built-in func ons to generate arrays.
Using np.array(): This method converts a list or tuple into a NumPy array.
Using np.zeros(): Creates an array filled with zeros. The shape of the array is specified as a tuple.
Using np.ones(): Similar to np.zeros(), this method creates an array filled with ones.
Using np.arange(): Generates an array with a range of values. It takes parameters for start, stop, and
step.
Key Features:
Range Specifica on: You can specify the lower and upper bounds for the uniform distribu on.
Shape Specifica on: You can define the shape of the output array.
Example:
Reshaping
Reshaping in NumPy refers to changing the shape of an exis ng array without changing its
data. Reshaping is par cularly useful in data preprocessing, where the input data must be in a
specific shape for analysis or model training.
This feature allows you to transform arrays into different dimensions while preserving their
elements.
This is done using the reshape() method, which takes a new shape as its argument. The total
number of elements must remain the same before and a er reshaping.
Key Features:
reshape() Method: You can use the reshape() method to specify a new shape for the array.
Shape Compa bility: The total number of elements must remain the same a er reshaping.
Maximum and Minimum
Finding the maximum and minimum values in an array is crucial for data analysis, allowing you
to assess the range and distribu on of data points.
NumPy provides efficient func ons to compute these values across different axes.
The maximum and minimum func ons in NumPy provide powerful tools for analyzing and
manipula ng numerical data.
By understanding how to use these func ons effec vely, including their ability to operate
along specific axes and retain dimensionality, you can conduct comprehensive data analyses.
1. np.max(array):
2. np.min(array):
o Instance methods of a NumPy array that provide the same func onality as the
aforemen oned func ons.
Key Features:
Element-wise Opera ons: Opera ons such as addi on, subtrac on, mul plica on, and
division are performed on an element-by-element basis.
Broadcas ng: When two arrays of different shapes are involved, NumPy automa cally adjusts
the shapes to allow for element-wise opera ons through broadcas ng.
Example:
1. Addi on:
2. Subtrac on:
4. Division:
5. Power:
Key Features:
Element-wise Func on Applica on: Func ons are applied to each element of the array.
Support for Broadcas ng: Func ons can also handle arrays of different shapes when
necessary.
Example:
3. Square Root:
Key Features:
Accessing Elements: Individual elements can be accessed using their index posi ons.
Slicing: You can slice arrays to obtain subarrays.
Boolean Indexing: You can use condi ons to select elements that sa sfy certain criteria.
Example:
2. Modifying Elements:
4. Slicing:
5. Boolean Indexing:
Broadcas ng
Broadcas ng is a feature in NumPy that allows arrays of different shapes to be used together
in arithme c opera ons.
It automa cally expands the smaller array across the larger array so that they have compa ble
shapes.
This feature simplifies coding and improves performance by elimina ng the need for explicit
replica on of data.
Key Features:
Shape Compa bility: Two arrays can be broadcast together if their shapes are compa ble.
Compa bility rules include:
o One of the dimensions can be 1 (the smaller array is stretched to match the larger).
o The number of dimensions can be different (the smaller array is padded with ones).
Example:
Key Features:
Element Access: Access specific elements by specifying row and column indices.
Example:
1. Accessing Rows:
2. Accessing Columns:
4. Slicing a Sub-Matrix: