NumPy
A Python Library
02 [Link]
Source: Kung Fu Panda
03 [Link]
Introduction to NumPy:
NumPy, which stands for Numerical Python, is a foundational library for
numerical computations in Python.
Benefits of Using NumPy:
1 Efficient Memory Usage:
NumPy arrays are stored at one continuous place in memory unlike lists,
making it efficient for NumPy to handle vast amounts of data.
2 Faster Data Access:
NumPy enhances data access speeds because, although it is a Python
library, it is primarily implemented in C and C++.
3 Functionality:
Provides an extensive collection of mathematical functions to perform
operations on these arrays with minimal code.
04 [Link]
Getting Started with NumPy:
Installation: Install NumPy with pip to integrate it into your Python
environment:
Importing NumPy:
Once installed, import it into your Python environment:
Here, we import NumPy using the alias np.
Note:
An "alias" in programming is a shorthand name assigned to a library or
module when importing it, making the code easier to write and read. For
example, importing NumPy as np allows you to use np instead of numpy
throughout your code.
05 [Link]
Core Features of NumPy
Creating Arrays: Arrays are the main feature of NumPy, allowing
efficient storage and manipulation of numerical data.
1 Using [Link]():
This function takes an object as a parameter, which can be a list, tuple,
or any array-like object, and converts it into a NumPy array.
Syntax:
Example:
Output:
[1 2 3 4]
Properties: Arrays have attributes like shape (dimensional structure),
size (total number of elements), and dtype (data type of
the elements).
Output: (4,) Output: 4
Output: int32 or int64
06 [Link]
2 Using [Link]():
This function creates an array with evenly spaced values within a
specified range.
Syntax:
Parameters:
start: The starting value of the sequence (inclusive). Default is 0 if
not provided.
stop: The ending value of the sequence (exclusive).
stop: The spacing between values in the sequence. Default is 1 if not
provided.
dtype: The desired data type of the output array. If not provided, it is
inferred from the input arguments.
Note 1: The parameters in square brackets [] are optional.
Note 2: Inclusion of Start and Exclusion of Stop:
If start is 1 and stop is 4, then the array starts from 1 and includes
numbers up to but not including 4.
Example output for [Link](1, 4) would be [1, 2, 3]. Here, 1 is
included while 4 is excluded from the array.
07 [Link]
Example 1:
Output: [0 1 2 3 4 5 6 7 8 9]
Example 2:
Output: [2 4 6 8]
08 [Link]
3 Using [Link]():
Generates an array of zeros with a specified shape and type.
Syntax:
Parameters:
shape: An integer or tuple of integers that defines the array's size
Example:
Output: [0. 0. 0. 0. 0.]
09 [Link]
Dimensions in Arrays
Single-dimensional (1D) Arrays:
The simplest form of arrays, essentially a single row of elements. They are
often used for simple lists of data points.
Example:
Output:
1D Array: [1 2 3 4 5]
Two-dimensional (2D) Arrays:
2D arrays are like a matrix or a table with rows and columns. This format is
extremely useful for representing and working with data in a structured
form, such as in spreadsheets, grids, or matrices.
Output:
2D Array:
[[1 2 3]
[4 5 6]]
10 [Link]
Higher-dimensional Arrays:
Used in more complex applications like images (3D) or data cubes (4D+).
3D arrays can be visualized as a stack of 2D arrays, often referred to as
"slices" or "layers." These arrays are particularly useful in applications that
require a multi-layered data structure, such as in 3D graphics, medical
imaging (like MRI scans), and complex scientific computations.
11 [Link]
Example:
Output: 3D Array:
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]]
[[10 11 12]
[13 14 15]
[16 17 18]]
[[19 20 21]
[22 23 24]
[25 26 27]]]
12 [Link]
Check out this playlist to delve
deeper into the NumPy library and
get hands-on practice with basic
operations in NumPy.
E N A B L I N G C A R E E R S
Like this way of learning?
Check our Python learning at
[Link]
[Link]