0% found this document useful (0 votes)
5 views4 pages

NumPy Data Science Assignments Guide

The document outlines an assignment for a Data Science course at Madhav Institute of Technology & Science, focusing on various tasks related to NumPy, including array slicing, flattening, cumulative operations, and broadcasting. It consists of 20 questions that require Python code to manipulate and analyze NumPy arrays in different ways. Each question specifies a task, such as creating arrays, performing mathematical operations, and utilizing broadcasting techniques.

Uploaded by

Kratika Jain
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)
5 views4 pages

NumPy Data Science Assignments Guide

The document outlines an assignment for a Data Science course at Madhav Institute of Technology & Science, focusing on various tasks related to NumPy, including array slicing, flattening, cumulative operations, and broadcasting. It consists of 20 questions that require Python code to manipulate and analyze NumPy arrays in different ways. Each question specifies a task, such as creating arrays, performing mathematical operations, and utilizing broadcasting techniques.

Uploaded by

Kratika Jain
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

MADHAV INSTITUTE OF TECHNOLOGY & SCIENCE, GWALIOR

Deemed to be University
(Declared under Distinct Category by Ministry of Education, Government of India)
NAAC Accredited with A++ Grade
Department of Computer Science and Engineering

Course Title: Data Science​ ​ ​ ​ Course Code: 150511/ 2290501

Assignment-3
Numpy
Instruction: Write Python code

Question 1:
Title: Slicing and Indexing
Description: Given a 4x4 NumPy array filled with consecutive integers starting from 1, extract
the 2x2 subarray from the bottom-right corner.

Question 2:
Title: Flattening Arrays
Description: Create a 3x3 NumPy array with random integers between 1 and 9. Flatten the array
into a 1D array.

Question 3:
Title: Cumulative Operations
Description: Generate a 1D NumPy array with 10 random integers between 1 and 20. Compute
the cumulative sum and cumulative product of the array.

Question 4:
Title: Linear Spacing
Description: Create a 1D NumPy array with 15 linearly spaced values between 10 and 50.
Reshape it into a 3x5 array.

Dr. Rohit Agrawal


MADHAV INSTITUTE OF TECHNOLOGY & SCIENCE, GWALIOR
Deemed to be University
(Declared under Distinct Category by Ministry of Education, Government of India)
NAAC Accredited with A++ Grade
Department of Computer Science and Engineering

Question 5:
Title: Sorting Arrays
Description: Generate a 1D NumPy array of 10 random integers. Sort the array in descending
order and find the index of the maximum value.

Question 6:
Title: Inverse and Determinant
Description: Create a 3x3 NumPy array with random integers. Calculate the determinant and
inverse of the array, ensuring it is invertible.

Question 7:
Title: Eigenvalues and Eigenvectors
Description: Create a 2x2 matrix with specific integer values. Compute its eigenvalues and
eigenvectors.

Question 8:
Title: Logical Operations
Description: Given a 1D NumPy array of 10 random integers, find the indices where the array
elements are even and greater than 5.

Question 9:
Title: Array Concatenation
Description: Create three 1D NumPy arrays with 5 random integers each. Concatenate them into
a single 1D array.

Question 10:
Title: Fourier Transform
Description: Create a 1D NumPy array of 100 evenly spaced values between 0 and 2π. Compute
the Fast Fourier Transform (FFT) of the array.

Dr. Rohit Agrawal


MADHAV INSTITUTE OF TECHNOLOGY & SCIENCE, GWALIOR
Deemed to be University
(Declared under Distinct Category by Ministry of Education, Government of India)
NAAC Accredited with A++ Grade
Department of Computer Science and Engineering

Question 11: Title: Scalar Broadcasting


Description: Create a 2D NumPy array with shape (4, 3) filled with random integers between 1
and 10. Add a scalar value of 5 to each element using broadcasting.

Question 12: Title: Vector Broadcasting


Description: Given a 3x3 matrix and a 1D array of size 3, use broadcasting to add the 1D array to
each row of the matrix.

Question 13: Title: Broadcasting with Different Shapes


Description: Create a 2D NumPy array with shape (4, 5) filled with random integers between 0
and 10. Create a 1D array with shape (5,). Subtract the 1D array from each row of the 2D array
using broadcasting.

Question 14: Title: Column-wise Broadcasting


Description: Generate a 2D NumPy array with shape (5, 4) and a 1D array of shape (5,). Use
broadcasting to multiply each column of the 2D array by the corresponding element in the 1D
array.

Question 15: Title: Broadcasting with Scalar Multiplication


Description: Create a 1D array with shape (10,). Reshape it into a 2D array of shape (10, 1) and
multiply it with a scalar value to demonstrate broadcasting.

Question 16: Title: Broadcasting in Element-wise Division


Description: Given a 3x4 matrix of integers, divide each element by a 1D array of length 4 using
broadcasting, and print the resulting matrix.

Question 17: Title: Broadcasting with 3D Arrays


Description: Create a 3D NumPy array with shape (2, 3, 4) and a 2D array with shape (3, 4). Add
the 2D array to each "slice" of the 3D array using broadcasting.

Dr. Rohit Agrawal


MADHAV INSTITUTE OF TECHNOLOGY & SCIENCE, GWALIOR
Deemed to be University
(Declared under Distinct Category by Ministry of Education, Government of India)
NAAC Accredited with A++ Grade
Department of Computer Science and Engineering

Question 18: Title: Broadcasting in Arithmetic Operations


Description: Create two 1D arrays of different sizes, one with 4 elements and another with 8
elements. Reshape the smaller array and use broadcasting to add them together.

Question 19: Title: Broadcasting and Boolean Masking


Description: Create a 3x3 matrix of random integers between 1 and 10. Use broadcasting to
compare each element to the mean of the matrix and create a boolean mask where elements
greater than the mean are True.

Question 20: Title: Broadcasting with Different Dimensions


Description: Create a 2x5 matrix and a 1x5 row vector. Subtract the row vector from each row of
the matrix using broadcasting, and then add a column vector with shape (2, 1) to the result.

Dr. Rohit Agrawal

Common questions

Powered by AI

Slicing and indexing techniques in NumPy provide powerful tools for data manipulation by allowing specific sub-portions of arrays to be accessed and modified efficiently. For example, extracting a 2x2 subarray from the bottom-right corner of a 4x4 array aids in focusing analyses on pertinent sections of data. These techniques support vectorized operations, increasing computational efficiency and enabling more complex data transformations with simpler, more readable code .

When calculating the inverse of a matrix, it is crucial to ensure the matrix is square and has a non-zero determinant; these conditions guarantee invertibility. For example, a 3x3 matrix with random integers must be checked for its determinant being non-zero. If the determinant is zero, the matrix is singular and non-invertible. Ensuring the matrix's linear equations are independent is also necessary, as dependent equations lead to a zero determinant .

Linear spacing in creating structured data sets offers advantages such as uniform value distribution and predictable pattern formation, beneficial for simulations and modeling. In the context of creating a 1D array with 15 linearly spaced values between 10 and 50, reshaped into a 3x5 array, this approach facilitates tasks like interpolation and sampling, enhancing data consistency and comparability in analyses like statistical forecasting or simulations .

Scalar broadcasting in NumPy involves applying a scalar operation uniformly across all elements of an array. For a 2D array with shape (4, 3) filled with random integers, adding a scalar value like 5 results in each element of the array being increased by that scalar. This simplifies code by eliminating the need for explicit loops and enhances performance by leveraging underlying optimized operations .

Cumulative operations like sum and product play an essential role in analyzing data trends by providing insights into the progression of data over a sequence. In a 1D array with 10 random integers, computing the cumulative sum highlights ongoing totals, revealing growth patterns, whereas cumulative products reflect compounding effects. These cumulative analyses are crucial in finance for cumulative returns, in statistics for integrating data, and in various simulations .

Eigenvalues and eigenvectors of matrices find practical applications in various fields like stability analysis, quantum mechanics, and facial recognition. For instance, in a 2x2 matrix, computing eigenvalues and eigenvectors can help identify key properties of linear transformations such as rotation or scaling applied to vector spaces. This is frequently used in Principal Component Analysis (PCA) where data dimensionality reduction relies on eigenvectors to transform data to a new basis where variance is maximized .

Validation before broadcasting in matrices of different sizes ensures compatibility and prevents runtime errors. Broadcasting requires that trailing dimensions must be the same for the operations to proceed (e.g., a 2x5 matrix and 1x5 row vector must align correctly). Failing to validate dimensions might lead to broadcasting errors, unintended results, or performance issues. Proper validation ensures operations like element-wise subtraction or addition enhance performance by avoiding unnecessary data duplication .

Boolean masking combined with broadcasting enables efficient data filtering by applying conditions across entire arrays. For instance, given a 3x3 matrix, broadcasting can compare each element with the matrix's mean, creating a Boolean mask where true indicates elements greater than the mean. This mask can then be applied to filter elements, providing a powerful tool for statistical analysis, data cleaning, or selecting subsets based on specific criteria .

Broadcasting in NumPy allows operations on arrays of different shapes without explicit element-wise loops. It effectively 'stretches' the smaller array to match the larger array's shape. For instance, in the problem of creating a 2D array with shape (4, 5) and a 1D array with shape (5,), broadcasting enables element-wise subtraction of the 1D array from each row of the 2D array. This concept extends to any operation (addition, multiplication, etc.) where compatible dimensions are aligned implicitly .

Fourier Transform, specifically Fast Fourier Transform (FFT), is crucial in signal processing and data analysis as it transforms a time-domain signal into its frequency components, allowing analysis of the signal's frequency characteristics. For example, in a 1D array of 100 evenly spaced values representing a signal, applying FFT reveals dominant frequencies, aids in filtering noise, and helps to compress data by focusing on significant frequencies. This transformation simplifies complex operations like convolution in the frequency domain .

You might also like