Construct a record array from a wide-variety of objects in Numpy


To display the numpy record array, use the numpy.core.records.array() method in Python Numpy. The 1st parameter is the obj i.e. the input. If obj is None, then call the recarray constructor. If obj is a string, then call the fromstring constructor. If obj is a list or a tuple, then if the first object is an ndarray, call fromarrays, otherwise call fromrecords. If obj is a recarray, then make a copy of the data in the recarray (if copy=True) and use the new formats, names, and titles. If obj is a file, then call fromfile. Finally, if obj is an ndarray, then return obj.view(recarray), making a copy of the data if copy=True.

Steps

At first, import the required library −

import numpy as np

Create a new array using the numpy.array() method −

arr = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45], [50, 55, 60]])

Display the array −

print("Array...
",arr)

Get the type of the array −

print("
Array type...
", arr.dtype)

Get the dimensions of the Array: −

print("
Array Dimensions...
", arr.ndim)

To display the numpy record array, use the numpy.core.records.array() method in Python Numpy −

print("
Record Array...
",np.core.records.array(arr))

Example

import numpy as np

# Create a new array using the numpy.array() method
arr = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45], [50, 55, 60]])

# Display the array
print("Array...
",arr) # Get the type of the array print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
", arr.ndim) # To display the numpy record array, use the numpy.core.records.array() method in Python Numpy print("
Record Array...
",np.core.records.array(arr))

Output

Array...
[[ 5 10 15]
[20 25 30]
[35 40 45]
[50 55 60]]

Array type...
int64

Array Dimensions...
2

Record Array...
[[ 5 10 15]
[20 25 30]
[35 40 45]
[50 55 60]]

Updated on: 10-Feb-2022

100 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements