Page 206 - Ai_V3.0_c11_flipbook
P. 206

Output:
                  [1 2 3 4 5]

                  [ 6  7  8  9 10]
              2. Using np.zeros() to create an array of zeros
              The zeros() creates arrays filled with zeros.

                Program 33: To demonstrate the use of np.zeros() to create an array of zeros

                   import numpy as np

                   # Array of zeros
                   arr_zeros = np.zeros(5)
                   print(arr_zeros)
              Output:

                  [0. 0. 0. 0. 0.]
              3. Using np.ones() to create an array of ones
              The ones() function creates arrays filled with ones.

                Program 34: To demonstrate the use of np.ones() to create an array of ones

                   import numpy as np
                   # Array of ones

                   arr_ones = np.ones(5)
                   print(arr_ones)
              Output:

                  [1. 1. 1. 1. 1.]
              4. Using arange() to create an array
              The arange() function in NumPy is used to create arrays with regularly incrementing values. The syntax of arange()
              function is as follows:

                   numpy.arange(start, stop, step, dtype=None)
              where,

               • •  start: The start of the interval (inclusive). If not specified, defaults to 0.
               • •  stop: The end of the interval (exclusive).
               • •  step: The spacing between values. If not specified, defaults to 1.
               • •  dtype: The data type of the array. If not specified, NumPy will infer the data type.

                Program 35: To demonstrate the use of arange() to create an array

                   import numpy as np

                   # Array with a range of values
                   arr_range = np.arange(0, 10, 2)
                   print(arr_range)
              Output:
                   [0 2 4 6 8]





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