NumPy Arrays
NumPy arrays are the foundation of NumPy. They are multidimensional, homogeneous data structures that can be used to store and manipulate large amounts of data efficiently.
Here are some of the key features of NumPy arrays:
- They are multidimensional, meaning that they can have more than one dimension. This makes them ideal for storing and manipulating data that has a natural multidimensional structure, such as images, matrices, and tensors.
- They are homogeneous, meaning that all of the elements in an array must be of the same data type. This makes it easy to perform operations on arrays, as the compiler can optimize the code for the specific data type.
- They are efficient, as NumPy has been designed to take advantage of the underlying hardware. This makes NumPy arrays much faster than traditional Python lists for numerical operations.
NumPy arrays can be created in a variety of ways, but the most common way is to use the array() function. The array() function takes a sequence of data as input and creates a NumPy array with that data. For example, the following code creates a NumPy array with the numbers 1, 2, 3, and 4:
import numpy as np
array = np.array([1, 2, 3, 4])
- Creating arrays: NumPy arrays can be created from lists, tuples, or other arrays.
- Arithmetic operations: NumPy arrays support all of the standard arithmetic operations, such as addition, subtraction, multiplication, and division.
- Indexing: NumPy arrays can be indexed like Python lists. This allows you to access individual elements or subarrays of an array.
- Slicing: NumPy arrays can be sliced like Python lists. This allows you to extract a subset of an array.
- Joining: NumPy arrays can be joined together to create a larger array.
- Broadcasting: NumPy arrays support broadcasting, which allows you to perform operations between arrays of different shapes.
- Math functions: NumPy provides a variety of mathematical functions for working with arrays, such as addition, subtraction, multiplication, division.trigonometric functions, exponential functions, and logarithm functions.
NumPy arrays are similar to Python lists, but they have several advantages:
- NumPy arrays are stored in contiguous memory, which makes them much faster to access than Python lists.
- NumPy arrays can be used to perform vectorized operations, which means that operations can be performed on multiple elements of the array at the same time.
- NumPy arrays have a rich set of mathematical functions and methods that can be used to manipulate them.
Here is an another example of how to create a NumPy array:
array = np.array([1, 2, 3, 4], dtype=int)
The dtype argument specifies the data type of the array. In this case, the dtype argument is set to int, which means that the array will contain integers.
Here is an example of how to index a NumPy array:
print(array[1])
This will print the following output:
2
Here is an example of how to slice a NumPy array:
array[1:3]
This code will return a subarray of array that starts at the second element and ends at the third element (excluding the third element).
NumPy arrays also have a wide range of mathematical operations available. For example, the following code adds 1 to each element of the array:
array += 1
This will update the array so that it contains the numbers 2, 3, 4, 5, and 6.
Here is an example of how to join two NumPy arrays:
This code joins array1 and array2 together to create a new array called array.
Here is an example of how to broadcast NumPy arrays:
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5])array = array1 + array2This code adds array1 and array2 together. The + operator is automatically broadcasted to add the corresponding elements of the two arrays.

Comments
Post a Comment