- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return a full array with the same shape and type as a given array in Numpy
To return a full array with the same shape and type as a given array, use the numpy.full_like() method in Python Numpy. The 1st parameter here is the shape and data-type, define these same attributes of the returned array. The 2nd parameter is the fill value.
The order overrides the memory layout of the result. ‘C’ means C-order, ‘F’ means F-order, ‘A’ means ‘F’ if a is Fortran contiguous, ‘C’ otherwise. ‘K’ means match the layout of a as closely as possible.
The subok parameter, if True, then the newly created array will use the sub-class type of a, otherwise it will be a base-class array. Defaults to True. The shape parameter overrides the shape of the result. If order=’K’ and the number of dimensions is unchanged, will try to keep order, otherwise, order=’C’ is implied.
Steps
At first, import the required library −
import numpy as np
Create a new array using the numpy.array() method in Python Numpy −
arr = np.array([[91, 34, 89], [29, 87, 55]])
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 return a full array with the same shape and type as a given array, use the numpy.full_like() method in Python Numpy −
newArr = np.full_like(arr, 999) print("
New Array..
", newArr)
Get the type of the new array −
print("
New Array type...
", newArr.dtype)
Get the dimensions of the new array −
print("
New Array Dimensions...
", newArr.ndim)
Example
import numpy as np # Create a new array using the numpy.array() method in Python Numpy arr = np.array([[91, 34, 89], [29, 87, 55]]) # 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 return a full array with the same shape and type as a given array, use the numpy.full_like() method in Python Numpy # The 1st parameter here is the shape and data-type, define these same attributes of the returned array. # The 2nd parameter is the fill value newArr = np.full_like(arr, 999) print("
New Array..
", newArr) # Get the type of the new array print("
New Array type...
", newArr.dtype) # Get the dimensions of the new array print("
New Array Dimensions...
", newArr.ndim)
Output
Array... [[91 34 89] [29 87 55]] Array type... int64 Array Dimensions... 2 New Array.. [[999 999 999] [999 999 999]] New Array type... int64 New Array Dimensions... 2