NumPy Array
1. Introduction
NumPy (Numerical Python) is a powerful library for numerical computations. It supports
multi-dimensional arrays and matrices, along with mathematical operations. NumPy arrays
are more efficient than Python lists and are widely used in data science and AI.
2. Creating NumPy Arrays
Code:
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
Output:
[1 2 3 4 5]
<class '[Link]'>
Code:
arr2 = [Link]((1, 2, 3, 4, 5))
print(arr2)
Output:
[1 2 3 4 5]
3. Array Dimensions
Code:
a = [Link](42)
b = [Link]([1,2,3,4,5])
c = [Link]([[1,2,3],[4,5,6]])
d = [Link]([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])
print([Link], [Link], [Link], [Link])
Output:
0, 1, 2, 3
4. Array Properties: Shape and Dimension
Code:
arr = [Link]([[1,2,3,4],[5,6,7,8]])
print([Link])
Output:
(2, 4)
5. Data Types
Code:
arr = [Link]([1, 2, 3, 4])
print([Link])
Output:
int64
Code:
arr = [Link]([1.1, 2.1, 3.1])
newarr = [Link]('i')
print(newarr)
print([Link])
Output:
[1 2 3]
int32
6. Joining Arrays
Code:
arr1 = [Link]([1,2,3])
arr2 = [Link]([4,5,6])
arr = [Link]((arr1, arr2))
print(arr)
Output:
[1 2 3 4 5 6]
7. Reshaping Arrays
Code:
arr = [Link](1, 9)
reshaped = [Link](2, 4)
print(reshaped)
Output:
[[1 2 3 4]
[5 6 7 8]]
8. Flattening Arrays
Code:
flat = [Link]()
print(flat)
Output:
[1 2 3 4 5 6 7 8]
9. Axis Parameter in NumPy
Code:
arr2d = [Link]([[1, 2, 3], [4, 5, 6]])
sum_row = [Link](arr2d, axis=1)
sum_col = [Link](arr2d, axis=0)
print(sum_row, sum_col)
Output:
Row sums: [ 6 15]
Column sums: [5 7 9]
10. Random Module
Code:
from numpy import random
x = [Link](100, size=10)
print(x)
Output:
[38 83 73 38 93 93 68 99 6 53]
Visual: Distribution of random integers.
11. Universal Functions (ufuncs)
Code:
arr = [Link]([1, 2, 3, 4])
sqrt_arr = [Link](arr)
log_arr = [Link](arr)
print(sqrt_arr, log_arr)
Output:
Square roots: [1. 1.41421356 1.73205081 2. ]
Log values: [0. 0.69314718 1.09861229 1.38629436]
12. Summary
This comprehensive guide covers all essential NumPy array operations including creation,
reshaping, flattening, slicing, and mathematical operations. It provides both the code and
executed results, along with a visualization. NumPy forms the foundation for data science,
machine learning, and AI applications.