Numpy ಪರಿಚಯ (Introduction to Numpy)
NumPy (Numerical Python) ಪೈಥಾನ್ನಲ್ಲಿ ವೈಜ್ಞಾನಿಕ ಮತ್ತು ಸಂಖ್ಯಾತ್ಮಕ ಗಣನೆಗಳಿಗೆ (scientific and numerical computing) ಬಳಸಲಾಗುವ ಒಂದು ಪ್ರಮುಖ ಲೈಬ್ರರಿ. ಇದು ದೊಡ್ಡ, ಬಹು-ಆಯಾಮದ (multi-dimensional) ಅರೇಗಳು ಮತ್ತು ಮ್ಯಾಟ್ರಿಕ್ಸ್ಗಳೊಂದಿಗೆ ಸುಲಭವಾಗಿ ಕೆಲಸ ಮಾಡಲು ಸಹಾಯ ಮಾಡುತ್ತದೆ.
NumPy ಯ ಮುಖ್ಯ ವೈಶಿಷ್ಟ್ಯವೆಂದರೆ ಅದರ ಶಕ್ತಿಯುತ ndarray (n-dimensional array) ಆಬ್ಜೆಕ್ಟ್. ಪೈಥಾನ್ನ ಲಿಸ್ಟ್ಗಳಿಗಿಂತ NumPy ಅರೇಗಳು ಹೆಚ್ಚು ವೇಗವಾಗಿ ಮತ್ತು ಮೆಮೊರಿ-ಸಮರ್ಥವಾಗಿರುತ್ತವೆ (memory-efficient).
NumPy ಅನ್ನು ಏಕೆ ಬಳಸಬೇಕು?
- ವೇಗ (Speed): NumPy ಕಾರ್ಯಾಚರಣೆಗಳು C ಭಾಷೆಯಲ್ಲಿ ಬರೆಯಲ್ಪಟ್ಟಿರುವುದರಿಂದ, ಅವು ಪೈಥಾನ್ನ ಲಿಸ್ಟ್ಗಳಿಗಿಂತ ಹೆಚ್ಚು ವೇಗವಾಗಿರುತ್ತವೆ.
- ಕಡಿಮೆ ಮೆಮೊರಿ (Less Memory): NumPy ಅರೇಗಳು ಲಿಸ್ಟ್ಗಳಿಗಿಂತ ಕಡಿಮೆ ಮೆಮೊರಿ ಬಳಸುತ್ತವೆ.
- ಗಣಿತೀಯ ಕಾರ್ಯಗಳು (Mathematical Functions): ಸಂಕೀರ್ಣ ಗಣಿತೀಯ ಕಾರ್ಯಾಚರಣೆಗಳನ್ನು ಸುಲಭವಾಗಿ ಮಾಡಲು ಅನೇಕ ಬಿಲ್ಟ್-ಇನ್ ಫಂಕ್ಷನ್ಗಳನ್ನು ಒದಗಿಸುತ್ತದೆ.
- ಬಹು-ಆಯಾಮದ ಅರೇಗಳು (Multi-dimensional Arrays): ವೆಕ್ಟರ್ ಮತ್ತು ಮ್ಯಾಟ್ರಿಕ್ಸ್ ಕಾರ್ಯಾಚರಣೆಗಳಿಗೆ ಇದು ಅತ್ಯುತ್ತಮವಾಗಿದೆ.
NumPy ಅರೇಗಳನ್ನು ರಚಿಸುವುದು (Creating NumPy Arrays)
ಮೊದಲು, NumPy ಲೈಬ್ರರಿಯನ್ನು ಇನ್ಸ್ಟಾಲ್ ಮಾಡಬೇಕು:
pip install numpy
ನಂತರ ಅದನ್ನು ಇಂಪೋರ್ಟ್ ಮಾಡಿಕೊಳ್ಳಿ:
1. ಲಿಸ್ಟ್ನಿಂದ ಅರೇ ರಚಿಸುವುದು:
# 1-D ಅರೇ
a = np.array([1, 2, 3, 4, 5])
print(a)
# Output: [1 2 3 4 5]
# 2-D ಅರೇ (ಮ್ಯಾಟ್ರಿಕ್ಸ್)
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
# Output:
# [[1 2 3]
# [4 5 6]]
2. ವಿಶೇಷ ಅರೇಗಳನ್ನು ರಚಿಸುವುದು:
# ಸೊನ್ನೆಗಳ ಅರೇ (Array of zeros)
zeros_array = np.zeros((2, 3)) # 2 ಸಾಲುಗಳು, 3 ಕಾಲಂಗಳು
print(zeros_array)
# Output:
# [[0. 0. 0.]
# [0. 0. 0.]]
# ಒಂದರ ಅರೇ (Array of ones)
ones_array = np.ones((3, 2))
print(ones_array)
# Output:
# [[1. 1.]
# [1. 1.]
# [1. 1.]]
# ನಿರ್ದಿಷ್ಟ ವ್ಯಾಪ್ತಿಯಲ್ಲಿ ಸಂಖ್ಯೆಗಳ ಅರೇ (Array with a range of numbers)
range_array = np.arange(10, 20, 2) # 10 ರಿಂದ 20 ರವರೆಗೆ, 2 ರ ಅಂತರದಲ್ಲಿ
print(range_array)
# Output: [10 12 14 16 18]
ಅರೇಯ ಗುಣಲಕ್ಷಣಗಳು (Array Attributes)
arr = np.array([[1, 2, 3], [4, 5, 6]])
# ಆಯಾಮಗಳ ಸಂಖ್ಯೆ (Number of dimensions)
print(arr.ndim) # Output: 2
# ಅರೇಯ ಆಕಾರ (Shape of the array)
print(arr.shape) # Output: (2, 3) -> 2 ಸಾಲುಗಳು, 3 ಕಾಲಂಗಳು
# ಒಟ್ಟು ಅಂಶಗಳ ಸಂಖ್ಯೆ (Total number of elements)
print(arr.size) # Output: 6
# ಡೇಟಾ ಪ್ರಕಾರ (Data type of elements)
print(arr.dtype) # Output: int32
ಅರೇ ಕಾರ್ಯಾಚರಣೆಗಳು (Array Operations)
NumPy ಅರೇಗಳ ಮೇಲೆ ಗಣಿತದ ಕಾರ್ಯಾಚರಣೆಗಳನ್ನು ಸುಲಭವಾಗಿ ಮಾಡಬಹುದು.
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
# ಸಂಕಲನ (Addition)
print(x + y) # Output: [5 7 9]
# ವ್ಯವಕಲನ (Subtraction)
print(x - y) # Output: [-3 -3 -3]
# ಗುಣಾಕಾರ (Element-wise multiplication)
print(x * y) # Output: [ 4 10 18]
# ಭಾಗಾಕಾರ (Element-wise division)
print(x / y) # Output: [0.25 0.4 0.5 ]
# ಸ್ಕೇಲಾರ್ ಗುಣಾಕಾರ (Scalar multiplication)
print(x * 2) # Output: [2 4 6]
ಇಂಡೆಕ್ಸಿಂಗ್ ಮತ್ತು ಸ್ಲೈಸಿಂಗ್ (Indexing and Slicing)
ಅರೇಯ ನಿರ್ದಿಷ್ಟ ಅಂಶಗಳನ್ನು ಪ್ರವೇಶಿಸಲು ಇಂಡೆಕ್ಸಿಂಗ್ ಮತ್ತು ಸ್ಲೈಸಿಂಗ್ ಬಳಸಲಾಗುತ್ತದೆ.
data = np.array([10, 20, 30, 40, 50, 60])
# ಇಂಡೆಕ್ಸಿಂಗ್ (Indexing)
print(data[0]) # Output: 10
print(data[3]) # Output: 40
# ಸ್ಲೈಸಿಂಗ್ (Slicing)
print(data[1:4]) # Output: [20 30 40] (index 1 ರಿಂದ 3 ರವರೆಗೆ)
# 2-D ಅರೇ ಸ್ಲೈಸಿಂಗ್
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# ಮೊದಲ ಸಾಲು (First row)
print(matrix[0, :]) # Output: [1 2 3]
# ಎರಡನೇ ಕಾಲಂ (Second column)
print(matrix[:, 1]) # Output: [2 5 8]
# ಉಪ-ಮ್ಯಾಟ್ರಿಕ್ಸ್ (Sub-matrix)
print(matrix[0:2, 1:3])
# Output:
# [[2 3]
# [5 6]]
ನೈಜ-ಪ್ರಪಂಚದ ಉದಾಹರಣೆ (Real-world Use Case)
ಒಂದು ತರಗತಿಯಲ್ಲಿ 5 ವಿದ್ಯಾರ್ಥಿಗಳ ಗಣಿತ ಮತ್ತು ವಿಜ್ಞಾನ ವಿಷಯಗಳ ಅಂಕಗಳಿವೆ. NumPy ಬಳಸಿ ಅವರ ಸರಾಸರಿ ಅಂಕಗಳನ್ನು ಕಂಡುಹಿಡಿಯೋಣ.
# ವಿದ್ಯಾರ್ಥಿಗಳ ಅಂಕಗಳು: [[ಗಣಿತ], [ವಿಜ್ಞಾನ]]
student_scores = np.array([
[85, 92, 78, 88, 95], # ಗಣಿತ ಅಂಕಗಳು
[90, 88, 82, 85, 91] # ವಿಜ್ಞಾನ ಅಂಕಗಳು
])
# ಪ್ರತಿ ವಿಷಯದ ಸರಾಸರಿ ಅಂಕಗಳು
average_scores = np.mean(student_scores, axis=1)
print(f"ಗಣಿತದ ಸರಾಸರಿ ಅಂಕ: {average_scores[0]}")
print(f"ವಿಜ್ಞಾನದ ಸರಾಸರಿ ಅಂಕ: {average_scores[1]}")
# ಪ್ರತಿ ವಿದ್ಯಾರ್ಥಿಯ ಒಟ್ಟು ಅಂಕಗಳು
total_scores_per_student = np.sum(student_scores, axis=0)
print(f"ಪ್ರತಿ ವಿದ್ಯಾರ್ಥಿಯ ಒಟ್ಟು ಅಂಕಗಳು: {total_scores_per_student}")
# ಅತಿ ಹೆಚ್ಚು ಗಣಿತ ಅಂಕ
max_math_score = np.max(student_scores[0])
print(f"ಗಣಿತದಲ್ಲಿ ಅತಿ ಹೆಚ್ಚು ಅಂಕ: {max_math_score}")