

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 new array with the same shape and type as a given array in Numpy
To return a new array with the same shape and type as a given array, use the ma.empty_like() method in Python Numpy. It returns and array of uninitialized (arbitrary) data with the same shape and type as prototype.
The order parameter overrides the memory layout of the result. 'C' means C-order, 'F' means Forder, 'A' means 'F' if prototype is Fortran contiguous, 'C' otherwise. 'K' means match the layout of prototype as closely as possible.
Steps
At first, import the required library −
import numpy as np import numpy.ma as ma
Create a new array using the numpy.array() method in Python Numpy −
arr = np.array([[77, 51, 92], [56, 31, 69], [73, 88, 51], [62, 45, 67]])
Displaying our array −
print("Array...\n",arr)
To return a new array with the same shape and type as a given array, use the ma.empty_like() method in Python Numpy. The parameter is the prototype i.e. the shape and data-type of prototype define these same attributes of the returned array −
arr = ma.empty_like(arr)
Displaying our array −
print("\nNew Array...\n",arr)
Get the dimensions of the Array −
print("\nArray Dimensions...\n",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...\n",arr.shape)
Get the number of elements of the Array −
print("\nElements in the Array...\n",arr.size)
Example
# Python ma.MaskedArray - Return a new array with the same shape and type as a given array import numpy as np import numpy.ma as ma # Create a new array using the numpy.array() method in Python Numpy arr = np.array([[77, 51, 92], [56, 31, 69], [73, 88, 51], [62, 45, 67]]) # Displaying our array print("Array...\n",arr) # To return a new array with the same shape and type as a given array, use the ma.empty_like() method in Python Numpy # The parameter is the prototype i.e. the shape and data-type of prototype define these same attributes of the returned array arr = ma.empty_like(arr) # Displaying our array print("\nNew Array...\n",arr) # Get the dimensions of the Array print("\nArray Dimensions...\n",arr.ndim) # Get the shape of the Array print("\nOur Array Shape...\n",arr.shape) # Get the number of elements of the Array print("\nElements in the Array...\n",arr.size)
Output
Array... [[77 51 92] [56 31 69] [73 88 51] [62 45 67]] New Array... [[ 0 0 0] [ 0 140657801869488 140657802018864] [140657801869552 140657801869616 140657801869680] [140657801997616 140657801869744 140657801997680]] Array Dimensions... 2 Our Array Shape... (4, 3) Elements in the Array... 12
- Related Questions & Answers
- Return a new array with the same shape and type as given array in Numpy
- Return a full array with the same shape and type as a given array in Numpy
- Return a new array with the same shape and type as a given array and change the order in Numpy
- Return a new array with the same shape as a given array but change the default type in Numpy
- Return an array of ones with the same shape and type as a given array in Numpy
- Return an array of zeros with the same shape and type as a given array in Numpy
- Return a new array with the same shape and type as a given array and change the order to K style in Numpy
- Return a new array with the same shape and type as a given array and change the order to C style in Numpy
- Return a new array of given shape and type, filled with array-like in Numpy
- Return an array of zeroes with the same shape as a given array but with a different type in Numpy
- Return a new array of given shape and type filled with a fill value in Numpy
- Return a new array of given shape and type without initializing entries in Numpy
- Return a new array of given shape filled with ones in Numpy
- Return a new array of given shape filled with zeros in Numpy
- Return a new array of a given shape filled with ones in Numpy