SR UNIVERSITY
SCHOOL OF COMPUTER SCIENCE AND ENGINEERING
Course- M .Tech Type- Core
Course Code- 24CS301PC50 Course Name- Fundamentals of Data Science
Year- 2024-2025 Date -24th October 2024
Semester- Odd H.T.NO : 2403B05107
1A. Given a 2D NumPy array
arr = np.array([[1, 2, 3], [4, 2, 5]]).
Write a Python program to perform the following operations using NumPy:
1. Print the type of the array object.
2. Find and display the number of dimensions (axes) of the array.
3. Find and display the shape of the array.
4. Find and display the total number of elements in the array.
5. Display the data type of the elements stored in the array. Expected
Output:
1. Type of array object (should be <class 'numpy.ndarray'>).
2. Number of dimensions (should be 2).
3. Shape of the array (should be (2, 3)).
4. Total number of elements (should be 6).
5. Data type of elements (e.g., int32 or int64).
Program:
import numpy as np
# Creating array object
arr = np.array( [[ 1, 2, 3],[ 4, 2, 5]] ) # Printing
type of arr object print("Array is of type: ",
type(arr)) # Printing array dimensions (axes)
print("No. of dimensions: ", arr.ndim) # Printing
shape of array print("Shape of array: ",
arr.shape)
# Printing size (total number of elements) of array print("Size of
array: ", arr.size)
# Printing type of elements in array
print("Array stores elements of type: ", arr.dtype)
OUTPUT :
Array is of type: <class 'numpy.ndarray'>
No. of dimensions: 2
Shape of array: (2, 3)
Size of array: 6
Array stores elements of type: int64
1B. Given a 3x3 NumPy array:
a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
Write a Python program to:
1. Print the original array.
2. Use slicing to extract and print all rows of the array starting from the second row.
Expected Output:
The original array.
The array after slicing, containing only rows starting from the second row.
Program:
import numpy as np
a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
print(a)
print("After slicing")
print(a[1:])
OUTPUT :
[[1 2 3]
[3 4 5]
[4 5 6]]
After slicing
[[3 4 5]
[4 5 6]]
1C.Given a 3x3 NumPy array:
a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
Write a Python program to perform the following tasks using array slicing:
1. Print the original array.
2. Extract and print all items in the second column.
3. Extract and print all items in the second row.
4. Extract and print all items from column 1 onwards (columns 1 and 2).
Expected Output:
The original array.
The items in the second column.
The items in the second row.
The items in columns 1 and 2.
Program:
import numpy as np
# Define the array
a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
print('Our array is:')
print(a)
# This returns array of items in the second column
print('The items in the second column are:')
print(a[..., 1])
print('\n')
# Now we will slice all items from the second row
print('The items in the second row are:')
print(a[1, ...])
print('\n')
# Now we will slice all items from column 1 onwards
print('The items from column 1 onwards are:')
print(a[..., 1:])
OUTPUT :
Our array is:
[[1 2 3]
[3 4 5]
[4 5 6]]
The items in the second column are:
[2 4 5]
The items in the second row are:
[3 4 5]
The items from column 1 onwards are:
[[2 3]
[4 5]
[5 6]]