NumPy Examples
NumPy Library How To Examples in Python
NumPy is a powerful library in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently. In this tutorial, we will cover various examples of how to use the NumPy library in Python.
Example 1: Creating NumPy Arrays
To create a NumPy array, you can use the numpy.array() function. Here's an example:
import numpy as np
# Creating a 1-dimensional array
arr1 = np.array([1, 2, 3, 4, 5])
print(arr1)
# Creating a 2-dimensional array
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr2)
Expected Output:
[1 2 3 4 5]
[[1 2 3]
[4 5 6]]
Example 2: Array Shape and Dimensions
You can use the shape attribute to get the shape of a NumPy array. The shape is represented as a tuple of integers indicating the size of each dimension. Here's an example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
Expected Output:
(2, 3)
Example 3: Array Data Type
To check the data type of a NumPy array, you can use the dtype attribute. Here's an example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr.dtype)
Expected Output:
int64
Example 4: Array Indexing
You can access individual elements of a NumPy array using indexing. The indexing starts from 0. Here's an example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[0]) # Accessing the first element
print(arr[2]) # Accessing the third element
Expected Output:
1
3
Example 5: Array Slicing
NumPy arrays support slicing to extract a portion of the array. Slicing is done using the : operator. Here's an example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[1:4]) # Slicing from index 1 to 3
Expected Output:
[2 3 4]
Example 6: Array Reshaping
You can reshape a NumPy array using the reshape() function. The new shape should be compatible with the original shape. Here's an example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped_arr = arr.reshape(2, 3) # Reshaping to a 2x3 array
print(reshaped_arr)
Expected Output:
[[1 2 3]
[4 5 6]]
Example 7: Array Concatenation
You can concatenate multiple NumPy arrays using the concatenate() function. Here's an example:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
concatenated_arr = np.concatenate((arr1, arr2))
print(concatenated_arr)
Expected Output:
[1 2 3 4 5 6]
Example 8: Array Arithmetic Operations
NumPy arrays support arithmetic operations such as addition, subtraction, multiplication, and division. Here's an example:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
addition = arr1 + arr2
subtraction = arr1 - arr2
multiplication = arr1 * arr2
division = arr1 / arr2
print(addition)
print(subtraction)
print(multiplication)
print(division)
Expected Output:
[5 7 9]
[-3 -3 -3]
[4 10 18]
[0.25 0.4 0.5 ]
Example 9: Array Mathematical Functions
NumPy provides various mathematical functions to operate on arrays efficiently. Here's an example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
mean = np.mean(arr)
median = np.median(arr)
std_dev = np.std(arr)
print(mean)
print(median)
print(std_dev)
Expected Output:
3.0
3.0
1.4142135623730951
Example 10: Array Filtering
You can filter a NumPy array based on a certain condition using Boolean indexing. Here's an example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
filtered_arr = arr[arr > 3]
print(filtered_arr)
Expected Output:
[4 5]
These are just a few examples to demonstrate the usage of the NumPy library in Python. NumPy provides many more functionalities to perform advanced mathematical operations efficiently.