Page 204 - Ai_V3.0_c11_flipbook
P. 204

For example, if you want to use a specific function like sqrt(), pow(), abs(), or sin() in Python, you have to tell
              Python that you want to use the math library where this function is stored. You do this by adding the line 'import math'
              at the beginning of your program. This lets you access and use all the helpful tools that the math library offers, including
              the ‘sqrt()’ function.
              Python offers a wide range of libraries, such as NumPy, Pandas, Matpoltlib, Scikit-Learn, etc. for various purposes that
              make Python a versatile language. With these libraries, you can do various tasks, such as web development, data analysis,
              machine learning, scientific computing, etc.


                       Introduction to NumPy

              NumPy is the short form of Numerical Python. It is a fundamental library in Python that is used for performing numerical
              computation. It provides support for arrays, matrices, and a variety of mathematical functions to operate on these
              data  structures efficiently. Its array-based data  structures and operations execution make it very useful for various
              applications, such as data analysis, machine learning, scientific computing, etc.
              In NumPy, there are several types of arrays, which are as follows:

               • •    One-dimensional  Arrays (1D Arrays): These arrays  contain  elements arranged  in a  single row  or  column.
                  One-dimensional arrays are created using the numpy.array() function with a Python list or tuple as input.

                   Program 29: To demonstrate the use of 1D array

                   import numpy as np
                   arr_1d = np.array([1, 2, 3, 4, 5])
                   print(arr_1d)
                    Output:

                  [1 2 3 4 5]
               • •    Two-dimensional Arrays (2D Arrays): Two-dimensional arrays are arranged in rows and columns, forming a grid-
                  like structure. Two-dimensional arrays are created using nested lists or by reshaping a one-dimensional array.

                   Program 30: To demonstrate the use of 2D array

                   import numpy as np
                   arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
                   print(arr_2d)
                    Output:

                  [[1 2 3]
                   [4 5 6]
                   [7 8 9]]
               • •    Multi-dimensional Arrays (nD Arrays): These arrays have more than two dimensions, which helps in complex data
                  representations. Multi-dimensional arrays can be created using nested lists or by reshaping existing arrays.

                   Program 31: To demonstrate the use of nD array

                  import numpy as np
                  # Creating a 3x3x3 ndarray

                  arr_nd = np.array([[[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]]])
                  print(arr_nd)

                    202     Touchpad Artificial Intelligence (Ver. 3.0)-XI
   199   200   201   202   203   204   205   206   207   208   209