0% found this document useful (0 votes)
3 views

numpy

NumPy is an open-source Python library that provides support for efficient numerical computations through its powerful array and matrix handling capabilities. It includes features such as multidimensional arrays, mathematical functions, broadcasting, and integration with other libraries, making it essential for data science and scientific computing. While it offers advantages like efficiency and community support, it has limitations such as a steep learning curve and lack of GPU support.

Uploaded by

7refie5l6f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

numpy

NumPy is an open-source Python library that provides support for efficient numerical computations through its powerful array and matrix handling capabilities. It includes features such as multidimensional arrays, mathematical functions, broadcasting, and integration with other libraries, making it essential for data science and scientific computing. While it offers advantages like efficiency and community support, it has limitations such as a steep learning curve and lack of GPU support.

Uploaded by

7refie5l6f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

What is 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

1. Mul dimensional Arrays:

o NumPy provides the ndarray object, which is a fast and efficient array object for
handling large datasets.

o Supports arrays of any dimensionality (1D, 2D, 3D, etc.).

2. Mathema cal Func ons:

o Provides a wide range of mathema cal func ons like sin, cos, log, exp, etc., that
operate on arrays.

3. Broadcas ng:

o Allows opera ons on arrays of different shapes and sizes.

4. Linear Algebra:

o Includes func ons for matrix mul plica on, eigenvalue computa on, and more.

5. Random Number Genera on:

o Provides tools for genera ng random numbers from various distribu ons.

6. Integra on with Other Libraries:

o Works seamlessly with libraries like Pandas, SciPy, Matplotlib, and Scikit-learn.

Advantages of NumPy

1. Efficiency:

o NumPy is implemented in C and op mized for performance, making it faster than


na ve Python lists for numerical computa ons.

2. Memory Efficiency:

o NumPy arrays consume less memory compared to Python lists.

3. Ease of Use:

o Provides a simple and intui ve interface for array opera ons.

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

1. Steep Learning Curve:

o Beginners may find it challenging to understand advanced concepts like broadcas ng


and vectoriza on.

2. Limited to Numerical Data:

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.

Key Methods for Array Crea on:

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.

Array Genera on from Uniform Distribu on


 In NumPy, you can generate arrays filled with random numbers from a uniform distribu on
using the np.random.uniform() func on.
 This func on can create arrays of any shape, filled with random values uniformly distributed
over a specified range.
 Genera ng arrays from a uniform distribu on means crea ng arrays of random numbers that
are evenly distributed over a specified range.
 The uniform distribu on is characterized by constant probability across the interval. This is
par cularly useful in simula ons, randomized algorithms, and sta s cal analysis.

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:

Random Array Genera on


 Random array genera on in NumPy is facilitated by the np.random module, which includes
func ons for genera ng random numbers, random samples, and random choices.
 This capability is essen al in simula ons, stochas c modeling, and various applica ons in data
science and machine learning.
 Random number genera on is a cri cal part of data science and scien fic compu ng. NumPy
provides func ons to generate random values for various sta s cal distribu ons, including
uniform, normal, and binomial distribu ons. Random arrays are o en used for tes ng
algorithms, crea ng synthe c datasets.

Key Func ons:

1. np.random.rand(): Generates an array of random numbers drawn from a uniform distribu on


over the interval [0, 1).

2. np.random.randn(): Generates an array of random numbers drawn from a standard normal


distribu on (mean 0, variance 1).

3. np.random.randint(): Generates an array of random integers within a specified range.

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.

Key Func ons:

1. np.max(array):

o Returns the maximum value of the elements in the input array.

o Can operate over different axes of a mul dimensional array.

2. np.min(array):

o Returns the minimum value of the elements in the input array.

o Also capable of opera ng over specified axes.

3. array.max() and array.min():

o Instance methods of a NumPy array that provide the same func onality as the
aforemen oned func ons.

o Useful for cleaner syntax when opera ng directly on array objects.

Arithme c Opera ons


 Arithme c opera ons in NumPy allow for element-wise mathema cal opera ons on arrays.
 This means that when you perform an opera on between two arrays, each element of one
array is combined with the corresponding element of the other array.
 This func onality is essen al for efficient numerical computa ons and data manipula on.

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:

3. Mul plica on:

4. Division:

5. Power:

Mathema cal Func ons


 NumPy offers a wide range of mathema cal func ons that can operate element-wise on
arrays.
 These func ons cover various mathema cal opera ons, including trigonometric, logarithmic,
and sta s cal func ons, enabling efficient numerical calcula ons.

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:

1. Trigonometric Func ons:


2. Exponen al and Logarithmic Func ons:

3. Square Root:

4. Sta s cal Func ons:

Bracket Indexing and Selec on


Bracket indexing in NumPy allows you to access and modify elements, rows, columns, or slices of arrays
using brackets []. This feature provides a powerful mechanism for data manipula on, enabling you to
retrieve specific subsets of data efficiently.

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:

1. Accessing Specific Elements:

2. Modifying Elements:

3. Accessing Rows and Columns:

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 Dimensions can be equal.

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:

1. Basic Broadcas ng:

2. Broadcas ng with Different Shapes:

Indexing a 2D Array (Matrices)


Indexing in 2D arrays, or matrices, allows you to retrieve specific rows, columns, or elements from a
two-dimensional structure. This is essen al for tasks involving data extrac on, manipula on, and
analysis in applica ons like machine learning, data science, and computer vision.

Key Features:

 Row Access: Retrieve en re rows using their index.

 Column Access: Retrieve en re columns using slicing.

 Element Access: Access specific elements by specifying row and column indices.
Example:

1. Accessing Rows:

2. Accessing Columns:

3. Accessing Specific Elements:

4. Slicing a Sub-Matrix:

5. Boolean Indexing with Matrices:

You might also like