Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 154 - 2615_KEC
P. 154

    To create a two-dimensional array of 3 rows and 4 columns with random integer values less
                        than 10:

                          Program.py
                       File  Edit  Format   Run    Options   Window    Help             Output

                       import numpy as np                                            [[2 1 6 3]
                       a = np.random.randint(10, size=(3,4))                          [4 6 5 9]
                       print(a)                                                       [9 1 5 9]]

                          np.ones() – Creates an array filled with ones.
                        For example, to create two-dimensional array of 3 rows and 4 columns with all ones as value:

                          Program.py

                       File  Edit  Format   Run    Options   Window    Help             Output

                       import numpy as np                                            [[1. 1. 1. 1.]
                       a = np.ones((3,4))                                             [1. 1. 1. 1.]
                       print(a)                                                       [1. 1. 1. 1.]]

                          np.zeros() – Creates an array filled with zeros.

                         For example, to create a two-dimensional array of 3 rows and 3 columns with all zeroes as
                        value:

                          Program.py
                       File  Edit  Format   Run    Options   Window    Help             Output

                                                                                     [[0. 0. 0.]
                       import numpy as np
                       a = np.zeros((3,3))                                            [0. 0. 0.]
                       print(a)                                                       [0. 0. 0.]]

                          np.full() – Creates an array filled with a specified value.
                        For example, to create a two-dimensional array of 3 rows and 4 columns with all six as value:

                          Program.py
                       File  Edit  Format   Run    Options   Window    Help             Output

                                                                                     [[6 6 6 6]
                       import numpy as np
                       a = np.full((3,4),6)                                           [6 6 6 6]
                       print(a)                                                       [6 6 6 6]]

                      152   Premium Edition-VIII

| 149 150 151 152 153 154 155 156 157 158 159 |