Page 205 - AI_Ver_3.0_class_11
P. 205

Output:
                    [[[ 1  2  3]
                      [ 4  5  6]
                      [ 7  8  9]]


                     [[10 11 12]
                      [13 14 15]
                      [16 17 18]]


                     [[19 20 21]
                      [22 23 24]
                      [25 26 27]]]
                 In NumPy, arrays are homogeneous, which means all elements in an array must be of the same data type, for example,
                 integers, floats, etc.
                 You can install NumPy using pip. For installing NumPy, you need to open your terminal or command prompt and run
                 the following command:

                                                             pip install numpy


                 NumPy Library in Artificial Intelligence
                 Let us understand why and where we can use the NumPy library in Artificial Intelligence with the help of an example.
                 Suppose, you have a dataset containing daily temperature readings from weather stations across different cities. You can
                 utilise NumPy arrays to efficiently manage and analyse this data.
                 With NumPy's array operations, you can easily perform the following tasks:

                  • •  Calculating the average temperature for each city over the recorded days.
                  • •  Finding the total temperature recorded for each day across all cities.
                  • •  Determining the overall average temperature across all cities and days.
                  • •  Identifying the highest and lowest temperatures recorded.
                 NumPy's array operations streamline these computations that enables you to handle large datasets with ease. This
                 makes NumPy an indispensable tool for processing and analysing data in different fields.


                 Creating a NumPy Array
                 NumPy array are created in several ways:
                 1. Using np.array() to create arrays from lists or tuples

                  Program 32: To demonstrate the use of np.array() to create arrays from lists or tuples

                     import numpy as np
                     # From a list

                     arr1 = np.array([1, 2, 3, 4, 5])
                     print(arr1)
                     # From a tuple
                     arr2 = np.array((6, 7, 8, 9, 10))

                     print(arr2)

                                                                                         Python Programming     203
   200   201   202   203   204   205   206   207   208   209   210