Instagram
youtube
Facebook
Twitter

Operations with Numpy Arrays II

Numpy Array Shape and Reshape

Shape

  • A NumPy array's shape can be found using the shape attribute which returns a tuple with each index representing the number of elements in each dimension.

Example

Here, we are going to print the shape of a 2-D array

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

print(arr.shape) # prints (2, 4)

The above code prints a tuple (2, 4) meaning 2 elements in the first dimension and 4 elements in the second dimension.

Example 

Now, we are going to create a 3-Dimensional array using ndmin attribute.   

import numpy as np

arr = np.array([11, 22, 33, 44, 55, 66], ndmin=3)

print(arr) # prints [[[11 22 33 44 55 66]]]

print('shape of array :', arr.shape) # prints shape of array : (1, 1, 6)

This will print that in the first two dimensions the number of elements is one and in the third dimension the number of elements are six.


Reshape

  • The shape means the number of elements in each dimension.

  • Reshaping means changing the shape of an array

  • Which can allow us to add or remove elements in each dimension.

Example

Here, we are going to convert the 1-D array into 2-D using reshape attribute, which will change the array into 2 arrays of 4 elements each.

import numpy as np

arr = np.array([11, 22, 33, 44, 55, 66, 77, 88])

newarr = arr.reshape(2, 4)

print(newarr) # prints [[11 22 33 44]
              #         [55 66 77 88]]

Note

  • We can Reshape into any shape as long as the number of elements is equal in both shapes.

  • For instance, below is the code demonstration of converting a 1-D array with 8 elements into a 2-D array with 2 elements in each dimension which causes an error.

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

newarr = arr.reshape(2, 2)

print(newarr)

Output

Traceback (most recent call last):
  File "./prog.py", line 5, in <module>
ValueError: cannot reshape array of size 8 into shape (2,2)

Array Iterating

  • Iterating in arrays means going through each element in the array one by one in the case of a 1-D array.

  • In multi-dimensional arrays, we can do this using for loop of python. 

Example 

Iterating on elements of a 1-D array:

import numpy as np

arr =  np.array([11, 22, 44, 33, 55, 99])

print(arr)

print('---------------------------------------')
print('---------------------------------------')

for I in arr:

    print(I)

Output:

[11 22 44 33 55 99]
---------------------------------------
---------------------------------------
11
22
44
33
55
99

Iterating Multi-Dimensional Arrays

  • In Multi-dimensional arrays, we can go through all the rows, and also we can go through all the rows and each of their elements.

  • If we are iterating an n-dim array, the iterator will go through the n-1 the dim one by one.

Example 

Iterate through the 2-D array first by row and then go into each element of the row.

import numpy as np

arr = np.array([[11, 22, 33], [44, 55, 66]])

print("The array is ", arr)

print("------------------------------------------")

print("printing the array row wise!")

for x in arr:
 
 print(x)

print("Now printing the array element wise!")

for m in arr:
 
 for n in m:
 
   print(n)

 Output:

('The array is ', array([[11, 22, 33],
       [44, 55, 66]]))
------------------------------------------
printing the array row wise!
[11 22 33]
[44 55 66]
Now printing the array element wise!
11
22
33
44
55
66

Example

Iterating through a 3-D array

import numpy as np

arr = np.array([[[11, 22, 33], [44, 55, 66], [77, 88, 99]]])

print("The array is ", arr)

print("------------------------------------------")

print("printing the array row wise!")

for x in arr:

  print(x)

print("Now printing the array element wise!")

for m in arr:

  for n in m:

    for o in n:

      print(o)

Output:

('The array is ', array([[[11, 22, 33],
        [44, 55, 66],
        [77, 88, 99]]]))
------------------------------------------
printing the array row wise!
[[11 22 33]
 [44 55 66]
 [77 88 99]]
Now printing the array element wise!
11
22
33
44
55
66
77
88
99