ASSIGNMENT
ASSIGNMENT
Q.5 What is Numpy? Explain different methods of Numpy.
Answer :-Â
NumPy stand for (Numerical Python) is a powerful open-source library in Python used for numerical computing.
It provides support for handling arrays and matrices, along with a wide range of mathematical functions to operate on them efficiently.
NumPy is the foundation for many other scientific computing and data analysis libraries, such as pandas, SciPy, and scikit-learn.
Key Features of NumPy
N-dimensional Arrays: Provides support for multi-dimensional arrays, called ndarray.
Broadcasting: Enables efficient operations between arrays of different shapes.
Mathematical Functions: Offers a variety of mathematical tools for operations like linear algebra, statistics, and Fourier transforms.
Efficiency: Written in C and optimized for performance, making it faster than pure Python for numerical operations.
Integration: Easily integrates with other libraries and tools in Python.
Different Methods of NumPy
NumPy offers numerous methods for creating, manipulating, and performing operations on arrays. Below are some of the key methods categorized based on their functionality:
1. Array Creation Methods
numpy.array(): Creates an array from a Python list or tuple.
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr) # Output: [1 2 3 4]numpy.zeros(): Creates an array filled with zeros.
arr = np.zeros((2, 3))
print(arr)numpy.ones(): Creates an array filled with ones.
arr = np.ones((2, 3))
print(arr)numpy.linspace(): Creates an array with evenly spaced numbers over a specified range.
arr = np.linspace(0, 1, 5)
print(arr) # Output: [0. 0.25 0.5 0.75 1. ]numpy.eye(): Creates an identity matrix.
arr = np.eye(3)
print(arr)
2.Array Manipulation Methods
reshape(): Reshapes an array without changing its data.
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped = arr.reshape((2, 3))
print(reshaped)transpose(): Transposes the rows and columns of a matrix.
arr = np.array([[1, 2], [3, 4]])
transposed = arr.T
print(transposed)flatten(): Converts a multi-dimensional array into a 1D array.
arr = np.array([[1, 2], [3, 4]])
flat = arr.flatten()
print(flat) # Output: [1 2 3 4]
3. Mathematical Operations
sum(): Computes the sum of array elements.
arr = np.array([1, 2, 3])
print(np.sum(arr)) # Output: 6
mean(): Computes the mean of the array.
arr = np.array([1, 2, 3])
print(np.mean(arr)) # Output: 2.0Â
dot(): Computes the dot product of two arrays.
a = np.array([1, 2])
b = np.array([3, 4])
print(np.dot(a, b)) # Output: 11
sqrt(): Computes the square root of each element.
arr = np.array([1, 4, 9])
print(np.sqrt(arr)) # Output: [1. 2. 3.]
4. Random Number Generation
numpy.random.rand(): Generates an array of random numbers between 0 and 1.
arr = np.random.rand(3, 2)
print(arr)
numpy.random.randint(): Generates random integers within a specified range.
arr = np.random.randint(1, 10, size=(3, 3))
print(arr)
5. Array Indexing and Slicing
Accessing Elements:
arr = np.array([10, 20, 30])
print(arr[1]) # Output: 20
Slicing:
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4]) # Output: [20 30 40]
6. Statistical Methods
min() and max(): Find the minimum and maximum values.
arr = np.array([1, 3, 2])
print(np.min(arr)) # Output: 1
print(np.max(arr)) # Output: 3
std(): Calculates the standard deviation.
arr = np.array([1, 2, 3])
print(np.std(arr)) # Output: 0.816…
7. Stacking and Splitting Arrays
vstack(): Vertically stacks arrays.
a = np.array([1, 2])
b = np.array([3, 4])
print(np.vstack((a, b)))
hstack(): Horizontally stacks arrays.
a = np.array([1, 2])
b = np.array([3, 4])
print(np.hstack((a, b)))
split(): Splits an array into multiple sub-arrays.
arr = np.array([1, 2, 3, 4, 5, 6])
print(np.split(arr, 3)) # Output: [array([1, 2]), array([3, 4]), array([5, 6])]
Â
Â