0% found this document useful (0 votes)
4 views6 pages

Python Assignment

NumPy is a crucial Python library for numerical and scientific computing, offering features like N-dimensional arrays, mathematical functions, and linear algebra tools. It allows for efficient array creation, indexing, slicing, and vectorized operations, significantly improving performance over traditional Python lists. Additionally, NumPy supports various numerical operations, loading/saving arrays, and random number generation, making it foundational for other libraries such as Pandas and TensorFlow.

Uploaded by

21065ch2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Python Assignment

NumPy is a crucial Python library for numerical and scientific computing, offering features like N-dimensional arrays, mathematical functions, and linear algebra tools. It allows for efficient array creation, indexing, slicing, and vectorized operations, significantly improving performance over traditional Python lists. Additionally, NumPy supports various numerical operations, loading/saving arrays, and random number generation, making it foundational for other libraries such as Pandas and TensorFlow.

Uploaded by

21065ch2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1)Explain NumPy and its importance in numerical computation.

A) Explain NumPy and its Importance in Numerical Computation


NumPy (Numerical Python) is a fundamental Python library used for numerical and scientific computing.
It provides:
A powerful N-dimensional array object (ndarray)
Mathematical functions
Linear algebra tools
Random number generation
Fourier transforms
Much faster than Python lists
Uses optimized C-based implementation
Supports vectorized operations
Uses less memory
Foundation for libraries like Pandas, SciPy, TensorFlow
Example:
Import numpy as np
A = [Link]([1, 2, 3])
Print(a * 2)
Output [2 4 6]

2. Explain NumPy array creation methods with examples.


A) NumPy provides several methods to create arrays:
Using array()
Creates an array from a list or tuple.
Example:
[Link]([1, 2, 3])
Zeros()
Creates an array filled with zeros.
Example:
[Link]((2, 3))
Ones()
Creates an array filled with ones.
Example:
[Link]((2, 2))
Arange()
Creates an array with evenly spaced values.
Example:
[Link](0, 10, 2)
Linspace()
Creates evenly spaced numbers over a specified interval.
Example:
[Link](0, 1, 5)
Eye()
Creates an identity matrix.
Example:
[Link](3)
Random functions
Generates random values.
Example:
[Link](2, 2)
3. Explain indexing and slicing of NumPy arrays.
A) Indexing is used to access individual elements of an array.
Example (1D array):A[1]
Example (2D array):A[1, 2]
Slicing is used to extract a portion of an array.
Example:A[1:4]
In 2D arrays:
A[:, 1] (Selects all rows of column 1)
4. Explain fancy indexing in NumPy.
Fancy indexing allows selecting multiple elements using an array of indices.
Example:a[[0, 2]]
Boolean indexing selects elements based on conditions.
Example:
a[a > 10]
Fancy indexing is useful for filtering and selecting specific elements efficiently.

5. Explain vectorized computation and its advantages.


A) Vectorization means performing operations on entire arrays without using explicit loops.
Example:
a+b
Advantages:
Faster execution
Cleaner and shorter code
Efficient memory usage
Avoids manual loops
6. Explain numerical operations on NumPy arrays.
A) NumPy supports various numerical operations:
Arithmetic Operations:
Addition: a + b
Subtraction: a - b
Multiplication: a * b
Division: a / b
Power: a ** 2
Aggregate Functions:
sum()
mean()
max()
min()
std()
These operations are performed element-wise.

7. Explain commonly used NumPy array functions.


A) Some commonly used functions are:
shape – Returns dimensions of the array.
reshape() – Changes the shape of an array.
flatten() – Converts multi-dimensional array into 1D.
transpose() – Swaps rows and columns.
concatenate() – Joins arrays.
split() – Splits arrays.
sort() – Sorts array elements.
These functions help in manipulating and organizing array data efficiently.
8. Explain loading and saving NumPy arrays.
A) NumPy allows saving and loading arrays to files.
To save an array:
[Link]('[Link]', array_name)
To load an array:
[Link]('[Link]')
To save multiple arrays:
[Link]('[Link]', a=array1, b=array2)
These files can be reused later without recreating the arrays.

9. Explain linear algebra operations using NumPy.


A) NumPy provides a linalg module for linear algebra operations.
Common operations include:
Matrix multiplication: [Link](A, B)
Determinant: [Link](A)
Inverse of matrix: [Link](A)
Eigenvalues and Eigenvectors: [Link](A)
Solving linear equations: [Link](A, b)
These operations are widely used in engineering and machine learning.

10. Explain random number generation in NumPy.


A) NumPy provides random number generation functions.
Common functions:
rand() – Generates random numbers between 0 and 1.
randint() – Generates random integers.
randn() – Generates numbers from normal distribution.
seed() – Sets seed for reproducibility.
Example:
[Link](0)
Random number generation is useful in simulations and machine learning.

You might also like