0% found this document useful (0 votes)
15 views

Data Science Assignment 1

This document is an assignment for a Data Science course, detailing questions related to NumPy array operations. It covers topics such as accessing elements, checking for empty arrays, differences between shallow and deep copies, reversing arrays, and performing element-wise operations. Each question is followed by explanations and examples demonstrating the concepts.

Uploaded by

Shivansh Shahi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Data Science Assignment 1

This document is an assignment for a Data Science course, detailing questions related to NumPy array operations. It covers topics such as accessing elements, checking for empty arrays, differences between shallow and deep copies, reversing arrays, and performing element-wise operations. Each question is followed by explanations and examples demonstrating the concepts.

Uploaded by

Shivansh Shahi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Science (21CSS303T)

ASSIGNMENT – 1
Date: 27-01-2025

NAME REG.NO

Surya Teja RA2211032010034

Maneesh RA2211032010040

Hemanth RA2211032010041

Shivansh RA2211032010085

Navaneeth RA2211032010087

Guna Sundar RA2211032010088

Questions:
1. How do to access elements in a NumPy array. Demonstrate the methods with suitable
examples
2. How do to check for an empty array (or zero elements array)?
3. Demonstrate the difference between a shallow copy and a deep copy in NumPy?
4. How can you reverse a NumPy array? Demonstrate in two different ways
5. How to perform element-wise operations on NumPy arrays?

Data Set Link:


https://drive.google.com/file/d/1A2PyrrCgfYHkVoT6tFMfu3JXaQLbZ9Oy/view?usp=sharing
Question:
1. How do to access elements in a NumPy array. Demonstrate the methods with suitable
examples

Answer:

In NumPy, elements in an array can be accessed using indexing, slicing, , and fancy indexing.
Indexing allows access to specific elements using their positions, while slicing retrieves a range of
elements. fancy indexing retrieves multiple elements using lists of indices. In 1D arrays, elements
are accessed with arr[index], while in 2D arrays, they are accessed with arr [row, col]. These
methods provide flexible and efficient ways to manipulate NumPy arrays for data analysis and
scientific computing. The below code shows the real time implantation of the 4 methods
mentioned above.
2. Question:
How do to check for an empty array (or zero elements array)?

Answer:
To check if a NumPy array is empty (i.e., has zero elements), you can use the. size attribute of the
array. The. size attribute returns the total number of elements in the array, and if it is 0, the array is
empty. The condition if arr. size == 0: can be used to check for emptiness. And another approach is
by length of the array where total characters are printed. Additionally, you can use the arr.any ()
function, which returns False if the array is empty or contains only 0 values. However, arr.any () is
not the best approach for checking strict emptiness because it also depends on the values inside the
array.
Question:

3. Demonstrate the difference between a shallow copy and a deep copy in NumPy?

Answer:

In NumPy, shallow copy (view ()) and deep copy (copy ()) are two ways to create copies of an
array, but they differ in how they store and reference data. When we modify the original data
and we print the shallow copy data it shows the new array which was created. Shallow Copy
(view ()). And deep copy prints the original data (means the previous data of the array which
sorted earlier.
Question:

4. How can you reverse a NumPy array? Demonstrate in two different ways.

Answer:

In NumPy, an array can be reversed using the np. flip () function and slicing ([::-1]). The np. flip
()
function reverses the order of elements along a specified axis while maintaining the shape of
the array. Another efficient way to reverse a NumPy array is by using slicing with [::-1], which
is a simple and fast approach. In this method, [::-1] creates a new view of the array where
elements are accessed in reverse order.
Question:

5. How to perform element-wise operations on NumPy arrays?

Answer:

Element-wise operations on NumPy arrays refer to performing operations on each individual


element of the array, typically without the need for explicit loops. NumPy allows you to
perform arithmetic operations such as addition, subtraction, multiplication, division, and more
directly on arrays. These operations are automatically applied element-wise, meaning they are
performed on each corresponding element of the arrays involved.

You might also like