Experiment No.
1: NumPy Library
PART A
A.1 Aim
To explore various functions of NumPy library and understand their logic for numerical
computation.
A.2 Prerequisite
Python basics
Arrays and matrices
Basic mathematical operations
A.3 Outcome
Students will be able to:
- Create NumPy arrays
- Use array creation functions
- Perform mathematical operations
- Apply statistical functions
- Use reshaping and indexing
- Understand vectorized computation
- Use NumPy in Machine Learning preprocessing
A.4 Theory
NumPy is a powerful Python library used for numerical computations. It provides support
for multidimensional arrays and mathematical functions.
NumPy is faster than Python lists because:
- Implemented in C
- Uses contiguous memory
- Supports vectorization
- Avoids loops
Used in ML for preprocessing, matrix operations, feature scaling, and statistical analysis.
PART B
B.1 Code
import numpy as np
a = [Link]([1,2,3,4])
b = [Link]([[1,2],[3,4]])
print("1D Array:",a)
print("2D Array:",b)
print([Link]((2,2)))
print([Link]((2,2)))
print([Link](0,10,2))
print([Link](0,1,5))
x = [Link]([10,20,30])
y = [Link]([1,2,3])
print("Addition:",x+y)
print("Multiplication:",x*y)
data = [Link]([10,20,30,40,50])
print("Mean:",[Link](data))
print("Std:",[Link](data))
r = [Link](6).reshape(2,3)
print(r)
B.2 Observations
NumPy arrays are faster.
Vectorized operations improve performance.
Statistical functions are useful in ML.
B.3 Conclusion
NumPy helps in efficient numerical computation and is widely used in ML.
B.4 Questions
1. What is NumPy?
NumPy is a Python library for numerical computing using arrays.
2. Why is NumPy faster?
Because it uses C backend and vectorized operations.
3. What is vectorization?
Performing operations without loops.
4. What is reshape?
Changing array shape without changing data.
5. Why NumPy used in ML?
For fast computation, preprocessing, and matrix operations.