Ex.
No:1
Working with Numpy arrays: Perform array operations, aggregations, Boolean
logic, and broadcasting
Aim:
To explore and implement various operations in NumPy, such as creation, reshaping, indexing,
slicing, aggregation, boolean logic, broadcasting, and fancy indexing on 1D and 2D arrays.
Algorithm:
1. Import the NumPy module.
2. Create arrays using different array creation functions.
3. Display and explore array attributes.
4. Perform indexing and slicing on 1D and 2D arrays.
5. Reshape the array and perform reshaping operations.
6. Apply arithmetic operations on arrays.
7. Use aggregation functions like sum, mean, std, etc.
8. Demonstrate boolean logic on arrays using comparison.
9. Apply broadcasting with arrays of different shapes.
10. Perform fancy indexing using index arrays or masks.
Program:
import numpy as np
# 1. Array Creation
print("1. Array Creation Functions")
a = np.arange(10)
b = np.linspace(0, 1, 5)
c = np.zeros((2, 3))
d = np.ones((3, 2))
e = np.eye(3)
print("a (arange):", a)
print("b (linspace):", b)
print("c (zeros):\n", c)
print("d (ones):\n", d)
print("e (identity):\n", e)
# 2. Array Attributes
a1 = np.arange(1, 11) # 1D Array
a2 = np.arange(1, 13).reshape(3, 4) # 2D Array
print("1D Array: a1\n", a1)
print("2D Array: a2\n", a2)
print("\n--- Array Attributes ---")
print("a2 Shape:", a2.shape)
print("a2 Size:", a2.size)
print("a2 Dimensions:", a2.ndim)
print("a2 Data type:", a2.dtype)
print("a2 Item size:", a2.itemsize, "bytes")
# 3. Indexing and Slicing
print("\n--- Indexing & Slicing ---")
print("a1[2]:", a1[2]) # 1D indexing
print("a1[2:7]:", a1[2:7]) # 1D slicing
print("a2[1,2]:", a2[1,2]) # 2D indexing
print("a2[0:2,1:3]:\n", a2[0:2,1:3]) # 2D slicing
# 4. Reshaping
reshaped = a1.reshape(2, 5)
print("\n--- Reshaped 1D to 2D (2x5) ---\n", reshaped)
# 5. Array Operations
print("\n--- Array Operations ---")
print("a1 + 10:\n", a1 + 10)
print("a2 * 2:\n", a2 * 2)
# 6. Aggregations
print("\n--- Aggregations ---")
print("Sum of a2:", np.sum(a2))
print("Mean of a2:", np.mean(a2))
print("Max of a2:", np.max(a2))
print("Min of a2:", np.min(a2))
print("Standard Deviation of a2:", np.std(a2))
# 7. Broadcasting
print("--- Broadcasting ---")
# 1D and Scalar Broadcasting
a = np.array([1, 2, 3])
b = 5
result1 = a + b
print("\n1D + Scalar Broadcasting:")
print("a:", a)
print("b:", b)
print("a + b:", result1)
# 1D and 1D Broadcasting
x = np.array([1, 2, 3])
y = np.array([10])
result2 = x * y
print("\n1D + 1D Broadcasting with compatible shape:")
print("x:", x)
print("y:", y)
print("x * y:", result2)
# 2D and 1D Broadcasting
A = np.array([[1, 2, 3],
[4, 5, 6]])
B = np.array([10, 20, 30])
result3 = A + B
print("\n2D + 1D Broadcasting (Row-wise):")
print("A:\n", A)
print("B:", B)
print("A + B:\n", result3)
# 2D and Column Vector Broadcasting
C = np.array([[1], [2], [3]])
D = np.array([10, 20, 30])
result4 = C + D
print("\n2D Column + 1D Broadcasting (Mixed shape):")
print("C:\n", C)
print("D:", D)
print("C + D:\n", result4)
# 8. Boolean Logic
print("\n--- Boolean Logic ---")
bool_mask = a2 > 5
print("a2 > 5:\n", bool_mask)
print("Filtered Elements (a2 > 5):", a2[a2 > 5])
# 9. Fancy Indexing
print("\n--- Fancy Indexing ---")
rows = [0, 2]
cols = [1, 3]
print("a2[rows, cols]:", a2[rows, cols]) # Fancy indexing
Sample Output:
1. Array Creation Functions
a (arange): [0 1 2 3 4 5 6 7 8 9]
b (linspace): [0. 0.25 0.5 0.75 1. ]
c (zeros):
[[0. 0. 0.]
[0. 0. 0.]]
d (ones):
[[1. 1.]
[1. 1.]
[1. 1.]]
e (identity):
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
1D Array a1:
[ 1 2 3 4 5 6 7 8 9 10]
2D Array a2:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
--- Array Attributes ---
a2 Shape: (3, 4)
a2 Size: 12
a2 Dimensions: 2
a2 Data type: int64
a2 Item size: 8 bytes
--- Indexing & Slicing ---
a1[2]: 3
a1[2:7]: [3 4 5 6 7]
a2[1,2]: 7
a2[0:2,1:3]:
[[2 3]
[6 7]]
--- Reshaped 1D to 2D (2x5) ---
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
--- Array Operations ---
a1 + 10:
[11 12 13 14 15 16 17 18 19 20]
a2 * 2:
[[ 2 4 6 8]
[10 12 14 16]
[18 20 22 24]]
--- Aggregations ---
Sum of a2: 78
Mean of a2: 6.5
Max of a2: 12
Min of a2: 1
Standard Deviation of a2: 3.452052529534662
--- Broadcasting ---
1D + Scalar Broadcasting:
a: [1 2 3]
b: 5
a + b: [6 7 8]
1D + 1D Broadcasting with compatible shape:
x: [1 2 3]
y: [10]
x * y: [10 20 30]
2D + 1D Broadcasting (Row-wise):
A:
[[1 2 3]
[4 5 6]]
B: [10 20 30]
A + B:
[[11 22 33]
[14 25 36]]
2D Column + 1D Broadcasting (Mixed shape):
C:
[[1]
[2]
[3]]
D: [10 20 30]
C + D:
[[11 21 31]
[12 22 32]
[13 23 33]]
--- Boolean Logic ---
a2 > 5:
[[False False False False]
[False True True True]
[ True True True True]]
Filtered Elements (a2 > 5): [ 6 7 8 9 10 11 12]
--- Fancy Indexing ---
a2[rows, cols]: [ 2 12]
Result:
Thus, various NumPy operations on 1D and 2D arrays were successfully implemented, including
creation, reshaping, indexing, slicing, aggregation and techniques like boolean logic, broadcasting, and
fancy indexing were applied to manipulate and analyze array data efficiently.