NumPy is the fundamental package for scientific computing in Python
NumPy (Numerical Python) is an open source Python library
The NumPy library contains multidimensional array and matrix data structures
ndarray, a homogeneous n-dimensional array object
Installing NumPy
pip install numpy
import NumPy
import numpy as np
difference between a Python list and a NumPy array
A Python list is a built-in data structure that can hold a collection of arbitrary objects, while a NumPy array is a data structure specifically designed for numerical operations and can only hold elements of the same data type.
NumPy arrays are more efficient than Python lists for numerical operations due to their homogeneous data type and memory allocation.
NumPy arrays have many built-in functions for numerical operations, such as matrix multiplication and linear algebra, while Python lists do not have these functions.
NumPy arrays can have multiple dimensions, while Python lists are limited to one dimension.
NumPy arrays can be sliced and indexed in a similar way to Python lists, but with more advanced options like Boolean indexing and fancy indexing.
Why use NumPy?
NumPy arrays are faster and more compact than Python lists. An array consumes less memory and is convenient to use. NumPy uses much less memory to store data and it provides a mechanism of specifying the data types. This allows the code to be optimized even further.
We can access the elements in the array using square brackets. When you’re accessing elements, remember that indexing in NumPy starts at 0. That means that if you want to access the first element in your array
In this example, we create a 1-D array with four elements using the np.array() function and store it in the variable arr1. We then print the array using the print() function.
In this example, we create a 2-D array with two rows and three columns using the np.array() function and store it in the variable arr2. We then print the array using the print() function.
In this example, we create a 3x4 array of zeros using the np.zeros() function and store it in the variable arr3. We then print the array using the print() function.
In this example, we create a 2x2 array of ones using the np.ones() function and store it in the variable arr4. We then print the array using the print() function.
NumPy is the fundamental package for scientific computing in Python
NumPy (Numerical Python) is an open source Python library The NumPy library contains multidimensional array and matrix data structures
ndarray, a homogeneous n-dimensional array object
Installing NumPy
import NumPy
difference between a Python list and a NumPy array
Why use NumPy?
NumPy arrays are faster and more compact than Python lists. An array consumes less memory and is convenient to use. NumPy uses much less memory to store data and it provides a mechanism of specifying the data types. This allows the code to be optimized even further.
We can access the elements in the array using square brackets. When you’re accessing elements, remember that indexing in NumPy starts at 0. That means that if you want to access the first element in your array
Output:
An array is usually a fixed-size container of items of the same type and size.
dimensions are called axes.
create a basic array
np.array(), np.zeros(), np.ones(), np.empty(), np.arange(), np.linspace()
Creating array by np.array
You can create a NumPy array using the
np.array()
function. Here are a few examples:Example 1: Creating a 1-D array
In this example, we create a 1-D array with four elements using the
np.array()
function and store it in the variablearr1
. We then print the array using theprint()
function.Example 2: Creating a 2-D array
In this example, we create a 2-D array with two rows and three columns using the
np.array()
function and store it in the variablearr2
. We then print the array using theprint()
function.Example 3: Creating an array of zeros
In this example, we create a 3x4 array of zeros using the
np.zeros()
function and store it in the variablearr3
. We then print the array using theprint()
function.Example 4: Creating an array of ones
In this example, we create a 2x2 array of ones using the
np.ones()
function and store it in the variablearr4
. We then print the array using theprint()
function.Creating array by np.empty()