Exp 12345
Exp 12345
a. Basic ndarray
A NumPy array (ndarray) is a multi-dimensional container for homogeneous data. We can create
it using np.array().
Output:
Basic ndarray:
[1 2 3 4 5]
b. Array of Zeros
We can create an array filled with zeros using np.zeros().
Explanation:
Array of zeros:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
c. Array of Ones
We can create an array filled with ones using np.ones().
Explanation:
random_array = np.random.rand(3, 3)
print("\nRandom numbers array:\n", random_array)
Explanation:
(Note: The numbers will be different each time you run the program.)
Explanation:
Output:
An array of your choice:
[[10 20 30]
[40 50 60]]
identity_matrix = np.eye(4)
print("\nIdentity Matrix:\n", identity_matrix)
Explanation:
Output:
Identity Matrix:
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
1. Using numpy.linspace()
This function returns an array of evenly spaced values over a specified range.
Syntax:
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
Example:
import numpy as np
Output:
css
CopyEdit
[ 1. 3.25 5.5 7.75 10. ]
Here, NumPy created an array with 5 evenly spaced values between 1 and 10.
2. Using numpy.arange()
Syntax:
numpy.arange(start, stop, step, dtype=None)
Example:
arr = np.arange(1, 10, 2)
print(arr)
Output:
[1 3 5 7 9]
Here, NumPy created an array with a step size of 2 from 1 to 9 (excluding 10).
Example:
import numpy as np
print(arr_1d.ndim) # Output: 1
print(arr_2d.ndim) # Output: 2
print(arr_3d.ndim) # Output: 3
Example:
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) # Output: (2, 3)
Example:
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.size) # Output: 6
Example:
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped_arr = arr.reshape(2, 3)
print(reshaped_arr)
Output:
[[1 2 3]
[4 5 6]]
⚠ Note: The total number of elements must remain the same after reshaping.
Example:
arr = np.array([[1, 2, 3], [4, 5, 6]])
flat_arr = arr.flatten()
print(flat_arr)
Output:
[1 2 3 4 5 6]
Example:
arr = np.array([[1, 2, 3], [4, 5, 6]])
transposed_arr = arr.T # or np.transpose(arr)
print(transposed_arr)
Output:
[[1 4]
[2 5]
[3 6]]
NumPy provides tools to add or remove dimensions from an array using np.expand_dims()
and np.squeeze().
Example:
import numpy as np
Output:
[[1 2 3]]
(1, 3)
Example:
arr = np.array([[[1, 2, 3]]]) # Shape (1,1,3)
squeezed_arr = np.squeeze(arr)
print(squeezed_arr)
print(squeezed_arr.shape)
Output:
[1 2 3]
(3,)
Here, extra dimensions (1,1,3) → (3,) are removed.
NumPy provides sorting using np.sort(), which returns a sorted copy of an array.
Example:
arr = np.array([3, 1, 2, 5, 4])
sorted_arr = np.sort(arr)
print(sorted_arr)
Output:
[1 2 3 4 5]
Output:
[[1 2 3]
[4 5 6]]
Example:
python
CopyEdit
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
Output:
lua
CopyEdit
[[1 2 3]
[4 5 6]]
Example:
python
CopyEdit
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
Output:
lua
CopyEdit
[[1 2]
[3 4]
[5 6]]
Example:
python
CopyEdit
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([1, 2, 3]) # 1D array
Output:
lua
CopyEdit
[[ 2 4 6]
[ 5 7 9]]
NumPy allows efficient indexing and slicing of arrays, making it easy to access and modify
elements. Let's explore slicing for 1D, 2D, and 3D arrays along with negative slicing.
array[start:stop:step]
Example:
import numpy as np
Output:
[2 3 4 5 6]
[0 1 2 3 4]
[3 4 5 6 7 8 9]
[0 2 4 6 8]
array[row_start:row_end, column_start:column_end]
Example:
arr_2d = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Output:
[[4 5]
[7 8]]
[[2 3]
[5 6]]
[[1 3]
[7 9]]
Example:
arr_3d = np.array([[[1, 2, 3], [4, 5, 6]],
[[7, 8, 9], [10, 11, 12]],
[[13, 14, 15], [16, 17, 18]]])
Output:
[[[ 7 8 9]
[10 11 12]]
[[13 14 15]
[16 17 18]]]
[[ 4 5 6]
[10 11 12]
[16 17 18]]
[[[ 1 3]
[ 4 6]]
[[ 7 9]
[10 12]]
[[13 15]
[16 18]]]
Syntax:
array[start:stop:step]
start: The starting index (negative values count from the end).
stop: The stopping index (exclusive, negative values count from the end).
step: The step size (negative step reverses the order).
arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
Output:
[60 70 80 90 100]
[10 20 30 40 50 60 70]
[100 90 80 70 60 50 40 30 20 10]
[30 40 50 60 70]
[30 20 10]
Example:
arr_2d = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
Output:
[[10 11 12]
[14 15 16]]
[[ 1 2]
[ 5 6]
[ 9 10]]
[[16 15 14 13]
[12 11 10 9]
[ 8 7 6 5]
[ 4 3 2 1]]
Example:
arr_3d = np.array([[[1, 2, 3], [4, 5, 6]],
[[7, 8, 9], [10, 11, 12]],
[[13, 14, 15], [16, 17, 18]]])
Output:
[[[ 8 9]
[11 12]]
[[14 15]
[17 18]]]
[[[18 17 16]
[15 14 13]]
[[12 11 10]
[ 9 8 7]]
[[ 6 5 4]
[ 3 2 1]]]
a. Stacking ndarrays
b. Concatenating ndarrays
NumPy provides powerful functions to combine multiple arrays in different ways. Two main
techniques are:
a. Stacking ndarrays
Stacking adds a new axis to combine arrays either vertically, horizontally, or along depth.
import numpy as np
Output:
[[1 2 3]
[4 5 6]]
2. np.hstack() → Horizontal Stacking (Column-wise)
Output:
[[1 2 3 4 5 6]]
Output:
[[[1 5]
[2 6]]
[[3 7]
[4 8]]]
Output:
[[1 4]
[2 5]
[3 6]]
b. Concatenating ndarrays
Concatenation joins arrays along an existing axis without adding a new one.
1. np.concatenate()
Output:
[[1 2]
[3 4]
[5 6]]
Output:
[[1 2 5 6]
[3 4 7 8]]
Output:
[[1 4]
[2 5]
[3 6]]
Output:
[[1 2 3]
[4 5 6]]
Output:
[11 12 13]
Output:
[[11 22 33]
[14 25 36]]
3. Broadcasting in Multiplication
arr1 = np.array([[1], [2], [3]]) # Shape (3,1)
arr2 = np.array([10, 20, 30]) # Shape (3,)
Output:
[[10 20 30]
[20 40 60]
[30 60 90]]