NumPy array Attributes-Transpose, Real, Imaginary & flat

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 this one by one.

1. NumPy ndarray.T: Transpose


The first attribute in the list is ndarray.T, which is the Transpose of an array. Transpose gives us the axis rotation of the numpy array when we take the transpose of an array. It transposes rows to columns and columns to rows. let us understand with the below 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 the 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.

Python program ndarray.Real

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.]

Python Program ndarray. imaginary

To determine the imaginary part we do it as:

import numpy as np


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

Output:

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

3. ndarray.flat


The numpy ndarray.flat attribute. 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:

import NumPy as np

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 to the given index. If we assign a value without specifying an index then the entire array of elements get assigned to that value. As in the example below, we are assigning all elements to 0.

Example Access the whole array using ndarray.flat

import NumPy as np

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

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, in the below example we are assigning value 1 to index 1 and index 2.

import NumPy as np

nd_array = np.arange(1, 10).reshape(3, 3)
print("Sample array is:",nd_array)
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]]