w3resource

NumPy: Create a two-dimensional array of specified format

NumPy: Basic Exercise-46 with Solution

Write a NumPy program to create a two-dimensional array of a specified format.

Sample format:

[[  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  28  29  30]
 [ 31  32  33  34  35  36  37  38  39  40]
 [ 41  42  43  44  45  46  47  48  49  50]
 [ 51  52  53  54  55  56  57  58  59  60]
 [ 61  62  63  64  65  66  67  68  69  70]
 [ 71  72  73  74  75  76  77  78  79  80]
 [ 81  82  83  84  85  86  87  88  89  90]
 [ 91  92  93  94  95  96  97  98  99 100]
 [101 102 103 104 105 106 107 108 109 110]
 [111 112 113 114 115 116 117 118 119 120]
 [121 122 123 124 125 126 127 128 129 130]
 [131 132 133 134 135 136 137 138 139 140]
 [141 142 143 144 145 146 147 148 149 150]]

Sample Solution :

Python Code :

# Importing the NumPy library with an alias 'np'
import numpy as np   

# Printing a message indicating the creation of an array of shape (15,10)
print("Create an array of shape (15,10):") 

# Printing a message indicating Command-1 and displaying the result
print("Command-1")
print(np.arange(1, 151).reshape(15, 10)) 

# Printing a message indicating Command-2 and displaying the result
print("\nCommand-2")
print(np.arange(1, 151).reshape(-1, 10)) 

# Printing a message indicating Command-3 and displaying the result
print("\nCommand-3")
print(np.arange(1, 151).reshape(15, -1)) 

Sample Output:

Create an array of shape (15,10):
Command-1
[[  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  28  29  30]
 [ 31  32  33  34  35  36  37  38  39  40]
 [ 41  42  43  44  45  46  47  48  49  50]
 [ 51  52  53  54  55  56  57  58  59  60]
 [ 61  62  63  64  65  66  67  68  69  70]
 [ 71  72  73  74  75  76  77  78  79  80]
 [ 81  82  83  84  85  86  87  88  89  90]
 [ 91  92  93  94  95  96  97  98  99 100]
 [101 102 103 104 105 106 107 108 109 110]
 [111 112 113 114 115 116 117 118 119 120]
 [121 122 123 124 125 126 127 128 129 130]
 [131 132 133 134 135 136 137 138 139 140]
 [141 142 143 144 145 146 147 148 149 150]]

Command-2
[[  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  28  29  30]
 [ 31  32  33  34  35  36  37  38  39  40]
 [ 41  42  43  44  45  46  47  48  49  50]
 [ 51  52  53  54  55  56  57  58  59  60]
 [ 61  62  63  64  65  66  67  68  69  70]
 [ 71  72  73  74  75  76  77  78  79  80]
 [ 81  82  83  84  85  86  87  88  89  90]
 [ 91  92  93  94  95  96  97  98  99 100]
 [101 102 103 104 105 106 107 108 109 110]
 [111 112 113 114 115 116 117 118 119 120]
 [121 122 123 124 125 126 127 128 129 130]
 [131 132 133 134 135 136 137 138 139 140]
 [141 142 143 144 145 146 147 148 149 150]]

Command-3
[[  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  28  29  30]
 [ 31  32  33  34  35  36  37  38  39  40]
 [ 41  42  43  44  45  46  47  48  49  50]
 [ 51  52  53  54  55  56  57  58  59  60]
 [ 61  62  63  64  65  66  67  68  69  70]
 [ 71  72  73  74  75  76  77  78  79  80]
 [ 81  82  83  84  85  86  87  88  89  90]
 [ 91  92  93  94  95  96  97  98  99 100]
 [101 102 103 104 105 106 107 108 109 110]
 [111 112 113 114 115 116 117 118 119 120]
 [121 122 123 124 125 126 127 128 129 130]
 [131 132 133 134 135 136 137 138 139 140]
 [141 142 143 144 145 146 147 148 149 150]]

Explanation:

In the above code –

print(np.arange(1, 151).reshape(15, 10)): This statement creates a NumPy array with integers from 1 (inclusive) to 151 (exclusive), i.e., integers from 1 to 150, and reshapes it into a 15x10 matrix (15 rows and 10 columns). A copy of the results is printed.

print(np.arange(1, 151).reshape(-1, 10)): This statement creates a NumPy array with integers from 1 to 150, similar to the first example. It then reshapes it into a matrix with 10 columns, and the number of rows is inferred automatically (indicated by -1). In this case, the inferred number of rows is 15. A copy of the results is printed.

print(np.arange(1, 151).reshape(15, -1)): This statement creates a NumPy array with integers from 1 to 150. It then reshapes it into a matrix with 15 rows, and the number of columns is inferred automatically (indicated by -1). In this case, the inferred number of columns is 10. A copy of the results is printed.

Python-Numpy Code Editor:

Previous: NumPy program to create one-dimensional array of single, two and three digit numbers.
Next: NumPy program to create a one dimensional array of forty pseudo-randomly generated values. Select random numbers from a uniform distribution between 0 and 1.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.