Page 257 - Robotics and AI class 10
P. 257

# Creating a numpy array

                     arr = np.array([50, 10, 40, 20, 30])
                     # Sorting the array in ascending order

                     sorted_arr = np.sort(arr)
                     print("Sorted Array (ascending):", sorted_arr)
                     # Sorting the array in descending order

                     sorted_arr_desc = np.sort(arr)[::-1]
                     print("Sorted Array (descending):", sorted_arr_desc)
                     Output:
                     Sorted Array (ascending): [10  20  30  40  50]

                     Sorted Array (descending): [50  40  30  20  10]
                 g.  Create a Numpy array of marks and calculate the mean, median and mode.
                     import numpy as np

                     from scipy import stats

                     # Create a NumPy array of marks
                     marks = np.array([85, 92, 78, 65, 88, 75, 90, 70, 82, 95])
                     # Calculate the mean using NumPy's mean() function

                     mn = np.mean(marks)
                     print("Mean:", mn)
                     # Calculate the median using NumPy's median() function

                     mdn = np.median(marks)
                     print("Median:", mdn)
                     # Calculate the mode using SciPy's mode() function

                     mode = stats.mode(marks)
                     print("Mode:", mode.mode[0])
                     Output:

                     Mean: 82.0
                     Median: 83.5
                     Mode: 65

              5.  Display an image using matplotlib and print its numpy array form. Also check the data type and shape of
                 the numpy array. Use the library `skimage' for getting sample images.

              Ans.  import numpy as np
                 import matplotlib.pyplot as plt
                 from skimage import data

                 # Load a sample image using skimage
                 image = data.astronaut()




                                                                                            Assignment     255
   252   253   254   255   256   257   258   259   260   261   262