In [1]:
Copied!
import numpy as np
# Creating arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([[1, 2], [3, 4]])
print("arr1:", arr1)
print("arr2:\n", arr2)
print("Shape of arr2:", arr2.shape)
import numpy as np
# Creating arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([[1, 2], [3, 4]])
print("arr1:", arr1)
print("arr2:\n", arr2)
print("Shape of arr2:", arr2.shape)
arr1: [1 2 3] arr2: [[1 2] [3 4]] Shape of arr2: (2, 2)
1.2 Array Operations¶
NumPy allows element-wise operations for arithmetic, comparisons, etc.
In [2]:
Copied!
arr3 = np.array([10, 20, 30])
# Element-wise arithmetic
print("arr1 + arr3 =", arr1 + arr3)
# Scalar operations
print("arr1 * 2 =", arr1 * 2)
# Comparison
print("arr3 > 15?", arr3 > 15)
arr3 = np.array([10, 20, 30])
# Element-wise arithmetic
print("arr1 + arr3 =", arr1 + arr3)
# Scalar operations
print("arr1 * 2 =", arr1 * 2)
# Comparison
print("arr3 > 15?", arr3 > 15)
arr1 + arr3 = [11 22 33] arr1 * 2 = [2 4 6] arr3 > 15? [False True True]
1.3 Slicing and Indexing¶
In [3]:
Copied!
arr4 = np.array([10, 11, 12, 13, 14, 15])
print("arr4[1:4] =", arr4[1:4])
arr5 = np.array([[1,2,3],[4,5,6],[7,8,9]])
print("\n", arr5)
print("arr5[0, 1] =", arr5[0, 1])
print("arr5[:, 1] =", arr5[:, 1])
arr4 = np.array([10, 11, 12, 13, 14, 15])
print("arr4[1:4] =", arr4[1:4])
arr5 = np.array([[1,2,3],[4,5,6],[7,8,9]])
print("\n", arr5)
print("arr5[0, 1] =", arr5[0, 1])
print("arr5[:, 1] =", arr5[:, 1])
arr4[1:4] = [11 12 13] [[1 2 3] [4 5 6] [7 8 9]] arr5[0, 1] = 2 arr5[:, 1] = [2 5 8]
In [4]:
Copied!
arr6 = np.array([[1, 2, 3], [4, 5, 6]])
arr7 = np.array([10, 20, 30])
# arr6 is (2,3) and arr7 is (3,)
result = arr6 + arr7 # Broadcasting adds arr7 to each row of arr6
print(result)
arr6 = np.array([[1, 2, 3], [4, 5, 6]])
arr7 = np.array([10, 20, 30])
# arr6 is (2,3) and arr7 is (3,)
result = arr6 + arr7 # Broadcasting adds arr7 to each row of arr6
print(result)
[[11 22 33] [14 25 36]]
2.2 Reshaping and Transposing¶
In [5]:
Copied!
arr8 = np.arange(1, 7)
print("Original:", arr8)
arr8_reshaped = arr8.reshape(2, 3)
print("\nReshaped to (2,3):\n", arr8_reshaped)
# Transpose
print("\nTransposed:\n", arr8_reshaped.T)
arr8 = np.arange(1, 7)
print("Original:", arr8)
arr8_reshaped = arr8.reshape(2, 3)
print("\nReshaped to (2,3):\n", arr8_reshaped)
# Transpose
print("\nTransposed:\n", arr8_reshaped.T)
Original: [1 2 3 4 5 6] Reshaped to (2,3): [[1 2 3] [4 5 6]] Transposed: [[1 4] [2 5] [3 6]]
2.3 Mathematical and Statistical Functions¶
NumPy provides a variety of built-in functions for computations like sum, mean, std, etc.
In [6]:
Copied!
arr9 = np.array([1, 2, 3, 4, 5])
print("Sum:", np.sum(arr9))
print("Mean:", np.mean(arr9))
print("Standard Deviation:", np.std(arr9))
arr9 = np.array([1, 2, 3, 4, 5])
print("Sum:", np.sum(arr9))
print("Mean:", np.mean(arr9))
print("Standard Deviation:", np.std(arr9))
Sum: 15 Mean: 3.0 Standard Deviation: 1.4142135623730951
2.4 Random Module¶
Useful for generating random numbers, random samples, etc.
In [7]:
Copied!
random_arr = np.random.rand(3, 3) # uniform distribution
print("Random Array:\n", random_arr)
randint_arr = np.random.randint(0, 10, size=(2, 5))
print("\nRandom Integers:\n", randint_arr)
random_arr = np.random.rand(3, 3) # uniform distribution
print("Random Array:\n", random_arr)
randint_arr = np.random.randint(0, 10, size=(2, 5))
print("\nRandom Integers:\n", randint_arr)
Random Array: [[0.16558855 0.99591053 0.75989262] [0.00898257 0.12894033 0.74504858] [0.60349745 0.82211221 0.25542086]] Random Integers: [[6 6 1 6 5] [3 5 8 0 6]]
3. Exercises ¶
Exercise 1: Basic Array Operations¶
- Create a NumPy array of shape
(4,4)
with numbers from 1 to 16. - Print the slice containing the second row.
- Multiply the entire array by 2.
In [8]:
Copied!
# Your code here
# Your code here
Exercise 2: Reshaping and Broadcasting¶
- Create a 1D array with numbers from 1 to 9.
- Reshape it into
(3,3)
. - Add a 1D array
[10,10,10]
to it using broadcasting.
In [9]:
Copied!
# Your code here
# Your code here
Exercise 3: Statistical Functions¶
- Generate an array of 100 random numbers (using
np.random.randn()
). - Compute the mean and standard deviation.
- Print the values.
In [10]:
Copied!
# Your code here
# Your code here
4. Real-World Applications ¶
Machine Learning¶
- NumPy arrays are the foundation for libraries like scikit-learn, TensorFlow, and PyTorch.
Linear Algebra¶
- Operations like matrix multiplication, decompositions, etc. are common in ML and data analysis.
Signal Processing, Simulations¶
- Researchers often use NumPy for large-scale numerical simulations, random processes, or DSP tasks.
NumPy is indispensable for scientific computing in Python, serving as the backbone for almost every advanced data library!