Discover NumPy basics in Python! Learn to manipulate data effortlessly with arrays and boost your coding skills. Perfect for beginners diving into numerical computing.
In Python we have lists that serve the purpose of arrays, but they are slow to process.
NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.
Install NumPy using the following command:
pip install numpy
Requirement already satisfied: numpy in /Users/akhilgurrapu/anaconda3/lib/python3.11/site-packages (1.23.5)
Note: you may need to restart the kernel to use updated packages.
Import NumPy in your Python script or Jupyter notebook:
import numpy as np
Create a simple array:
arr = np.array([1, 2, 3])
arr
array([1, 2, 3])
Note: All the items in the array should be of same data type unlike lists in python.
arr * 2
array([2, 4, 6])
print(arr[0]) # Access the first element
print(arr[1:3]) # Slice from index 1 to 2
1
[2 3]
one_dimensional_array = np.array([1, 2, 3])
print(one_dimensional_array )
# Accessing elements
print(one_dimensional_array[0]) # Accessing the first element
[1 2 3]
1
two_dimensional_array = np.array([[1, 2, 3], [4, 5, 6]])
print(two_dimensional_array)
#Accessing elements:
print(two_dimensional_array[0, 1]) # # Accessing the element in the first row and second column
[[1 2 3]
[4 5 6]]
2
# Extracting a subarray:
two_dimensional_array[0:,1:]
array([[2, 3],
[5, 6]])
multi_dimensional_array = np.array([[[1, 2, 3, 4], [5, 6, 7 ,8]], [[9, 10, 11, 12], [13, 14, 15, 16]]])
multi_dimensional_array
array([[[ 1, 2, 3, 4],
[ 5, 6, 7, 8]],
[[ 9, 10, 11, 12],
[13, 14, 15, 16]]])
#Accessing elements:
print(multi_dimensional_array[0, 1, 0]) # Accessing the element in the first "layer," second row, and first column
5
print(one_dimensional_array.shape)
print(two_dimensional_array.shape)
print(multi_dimensional_array.shape)
(3,)
(2, 3)
(2, 2, 2)
original_array = np.array([1, 2, 3, 4, 5, 6])
original_array.reshape((2, 3))
# In this example, the original one-dimensional array is reshaped into a 2x3 matrix.
array([[1, 2, 3],
[4, 5, 6]])
original_array = np.array([[1, 2, 3], [4, 5, 6]])
original_array.T
#In this example, the original 2x3 matrix is transposed into a 3x2 matrix.
array([[1, 4],
[2, 5],
[3, 6]])
array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])
# Concatenation
concatenated_array = np.concatenate((array1, array2), axis=1)
# Horizontal stacking
horizontal_stack = np.hstack((array1, array2))
# Vertical stacking
vertical_stack = np.vstack((array1, array2))
print("Array 1:")
print(array1)
print("\nArray 2:")
print(array2)
print("\nConcatenated Array:")
print(concatenated_array)
print("\nHorizontal Stack:")
print(horizontal_stack)
print("\nVertical Stack:")
print(vertical_stack)
Array 1:
[[1 2]
[3 4]]
Array 2:
[[5 6]
[7 8]]
Concatenated Array:
[[1 2 5 6]
[3 4 7 8]]
Horizontal Stack:
[[1 2 5 6]
[3 4 7 8]]
Vertical Stack:
[[1 2]
[3 4]
[5 6]
[7 8]]
# Scalar and 1D array broadcasting
scalar_value = 5
array_1d = np.array([1, 2, 3])
result = scalar_value * array_1d
print(result)
[ 5 10 15]
# 1D and 2D array broadcasting
array_1d = np.array([1, 2, 3])
array_2d = np.array([[4, 5, 6], [7, 8, 9]])
result = array_1d + array_2d
print(result)
[[ 5 7 9]
[ 8 10 12]]
# Broadcasting with reshaping
array_a = np.array([[1], [2], [3]])
array_b = np.array([4, 5, 6])
result = array_a * array_b
print(result)
[[ 4 5 6]
[ 8 10 12]
[12 15 18]]
arr = np.arange(1, 10, 2) # Start at 1, stop before 10, step by 2
print(arr)
[1 3 5 7 9]
arr = np.linspace(0, 1, 5) # Start at 0, end at 1, with 5 elements
print(arr)
[0. 0.25 0.5 0.75 1. ]
zeros_arr = np.zeros((2, 3)) # 2x3 array of zeros
ones_arr = np.ones((3, 2)) # 3x2 array of ones
print("Zeros Array:")
print(zeros_arr)
print("\nOnes Array:")
print(ones_arr)
Zeros Array:
[[0. 0. 0.]
[0. 0. 0.]]
Ones Array:
[[1. 1.]
[1. 1.]
[1. 1.]]
# Specific Data type can be added to the Zeros and Ones, using 'dtype'
zeros_arr = np.zeros((2, 3), dtype = int) # 2x3 array of zeros
ones_arr = np.ones((3, 2), dtype = int) # 3x2 array of ones
print("Zeros Array:")
print(zeros_arr)
print("\nOnes Array:")
print(ones_arr)
Zeros Array:
[[0 0 0]
[0 0 0]]
Ones Array:
[[1 1]
[1 1]
[1 1]]
random_values = np.random.rand(3, 2) # 3x2 array of random values [0, 1)
normal_values = np.random.randn(2, 4) # 2x4 array of random values from a normal distribution
print("Random Values:")
print(random_values)
print("\nNormal Distribution Values:")
print(normal_values)
Random Values:
[[0.6936222 0.88476966]
[0.17138852 0.42484089]
[0.29378554 0.82487876]]
Normal Distribution Values:
[[-1.78761297 -0.65459643 -0.55196277 -1.17857765]
[ 0.01644488 0.22102268 -0.7390483 -1.72983102]]
# Generate a random integer between 0 (inclusive) and 10 (exclusive)
random_integer = np.random.randint(0, 10)
print("Random Integer:", random_integer)
# Generate a 1D array with 5 random integers between 0 (inclusive) and 10 (exclusive)
random_array = np.random.randint(0, 10, 6)
print("Random Array:", random_array)
#Reshaping the random_array
random_array.reshape(3,2)
Random Integer: 7
Random Array: [9 9 8 8 7 7]
array([[9, 9],
[8, 8],
[7, 7]])
arr = np.array([[1, 2, 3], [4, 5, 6]])
sum_result = np.sum(arr)
mean_result = np.mean(arr)
min_result = np.min(arr)
max_result = np.max(arr)
print("Sum:", sum_result)
print("Mean:", mean_result)
print("Min:", min_result)
print("Max:", max_result)
Sum: 21
Mean: 3.5
Min: 1
Max: 6
Normal Assignment (=):
numpy.copy():
# Normal Assignment (=):
original_array = np.array([[1, 2, 3], [4, 5, 6]])
# Normal assignment, creating a reference to the same array
new_array = original_array
# Modifying the new array
new_array[0, 0] = 100
print("Original Array: after modification of new array")
print(original_array)
print("\nNew Array:")
print(new_array)
Original Array: after modification of new array
[[100 2 3]
[ 4 5 6]]
New Array:
[[100 2 3]
[ 4 5 6]]
new_array is not a new copy of original_array; rather, it’s a reference to the same underlying array.
# numpy.copy():
original_array = np.array([[1, 2, 3], [4, 5, 6]])
# Create a deep copy using numpy.copy()
copied_array = np.copy(original_array)
# Modify the copied array
copied_array[0, 0] = 100
print("Original Array:")
print(original_array)
print("\nCopied Array:")
print(copied_array)
Original Array:
[[1 2 3]
[4 5 6]]
Copied Array:
[[100 2 3]
[ 4 5 6]]
using numpy.copy() creates a completely independent copy of the array. Modifying the copied array does not affect the original array, and vice versa.*
import numpy as np
# Scalar and 1D array broadcasting
scalar_value = 5
array_1d = np.array([1, 2, 3])
result = scalar_value * array_1d
print(result)
[ 5 10 15]
import numpy as np
# 1D and 2D array broadcasting
array_1d = np.array([1, 2, 3])
array_2d = np.array([[4, 5, 6], [7, 8, 9]])
result = array_1d + array_2d
print(result)
[[ 5 7 9]
[ 8 10 12]]
import numpy as np
# Broadcasting with reshaping
array_a = np.array([[1], [2], [3]])
array_b = np.array([4, 5, 6])
result = array_a * array_b
print(result)
[[ 4 5 6]
[ 8 10 12]
[12 15 18]]