NumPy array Attributes-Transpose, Real, Imaginary & flat

Numpy

In this article, we are going to learn about some of the more attributes that ndarray supports.These are helpful attributes that we need during our operations with ndarray. so let us understand these one by one.

To understand the attributes of NumPy array we will take a sample array of 2-D array and then we will go through the properties.

import numpy as np
samplearray = np.array([[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]])
samplearray1D = np.array([1,2,3,4,5,6])

print("Sample Array One elements",samplearray)
print("Sample Array Two elements",samplearray1D)

Output:

Sample Array One elements [[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]]
Sample Array Two elements [1 2 3 4 5 6]

1. ndarray.T : Transpose

The first attribute in the list is ndarray.T , which is Transpose of an array. Transpose gives us the axis rotation array when we take the transpose of an array.

Here is an example

import numpy as np

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

print("Transpose of array one:",samplearray.T)
print("Transpose of array Two:",samplearray1D.T)

Output:

Transpose of array one: [[1. 4. 7.]
 [2. 5. 8.]
 [3. 6. 9.]]
Transpose of array Two: [1 2 3 4 5 6]

2.ndarray.real & ndarray.imag

The next two attributes that we have in ndarray are real and imag. We will take an array of imaginary numbers that have the real part and imaginary part.So if we want to know the real part of all the elements then we can use the
ndarray. real attribute and similarly for the imaginary part we can use the ndarray.imag attribute.

Let us understand this with an example below: Here we have an array of imaginary numbers

import numpy as np


Imginaryarray = np.array([1 + 1j,2 + 4j,3 + 9j,4 + 16j,5 + 25j,6 + 36j])
print("Real Part of array :",Imginaryarray.real)

Output

Real Part of array : [1. 2. 3. 4. 5. 6.]

So determine the imaginary part we do it as:

print("Imaginary Part of array",Imginaryarray.imag)

Output:

Imaginary Part of array [ 1.  4.  9. 16. 25. 36.]

3. ndarray.flat

Next in the list of attributes is ndarray.flat. This attribute acts as A 1-D iterator over the array.By using this attribute we can iterate over the array and also we can change the value at a particular or multiple indexes.

let us jump to our example to understand this:

nd_array = np.arange(1, 10).reshape(3, 3)
print("Sample array is:",nd_array)

print("Access index 3 element: ",nd_array.flat[3])
print("Type of ndarray.flat:",type(nd_array.flat))

print("After transpose array:",nd_array.T)

print("Index access of a tranposed array with flat:",nd_array.T.flat[3])

Output:

Sample array is: [[1 2 3]
 [4 5 6]
 [7 8 9]]
Access index 3 element:  4
Type of ndarray.flat: <class 'numpy.flatiter'>
After transpose array: [[1 4 7]
 [2 5 8]
 [3 6 9]]
Index access of a tranposed array with flat: 2

The most powerful use of flat is to assign values at the given index. If we assign a value without specifying an index then the entire array elements get assigned to that value. As in the example below, we are assigning all elements to 0.

nd_array.flat = 0
print("Assign all elements as 0:",nd_array)

Output:

Assign all elements as 0:
 [[0 0 0]
 [0 0 0]
 [0 0 0]]

Similarly, we can assign elements to multiple indexes, like in the below example we are assigning value 1 to index 1 and index 2.

nd_array.flat[[1,2]] = 1
print("Assign elements at index 1 & 2 to 1:",nd_array)

Output:

Assign elements at index 1 & 2 to 1:
 [[0 1 1]
 [0 0 0]
 [0 0 0]]