- 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 given array in Numpy

To return a new array with the same shape and type as a given array, use the **numpy.empty_like()** method in Python Numpy. It returns the array of uninitialized (arbitrary) data with the same shape and type as prototype. The 1st parameter here is the shape and data-type of prototype(array-like) that define these same attributes of the returned array.

The order overrides the memory layout of the result. ‘C’ means C-order, ‘F’ means F-order, ‘A’ means ‘F’ if prototype is Fortran contiguous, ‘C’ otherwise. ‘K’ means match the layout of prototype as closely as possible. The shape 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. The overrides parameter overrides the data type of the result.

The subok parameter, if True, then the newly created array will use the sub-class type of prototype, otherwise it will be a base-class array. Defaults to True.

## 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([[35, 56, 66], [88, 73, 98]])

Display the array −

print("Array...\n",arr)

Get the type of the array −

print("\nArray type...\n", arr.dtype)

Get the dimensions of the Array −

print("\nArray Dimensions...\n", arr.ndim)

Return a new array with the same shape and type as a given array, use the numpy.empty_like() method in Python Numpy −

newArr = np.empty_like(arr) print("\nNew Array..\n", newArr)

Get the type of the new array −

print("\nNew Array type...\n", newArr.dtype)

Get the dimensions of the new array −

print("\nNew Array Dimensions...\n", newArr.ndim)

## Example

import numpy as np # Create a new array using the numpy.array() method in Python Numpy arr = np.array([[35, 56, 66], [88, 73, 98]]) # Display the array print("Array...\n",arr) # Get the type of the array print("\nArray type...\n", arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n", arr.ndim) # To return a new array with the same shape and type as a given array, use the numpy.empty_like() method in Python Numpy # It returns the array of uninitialized (arbitrary) data with the same shape and type as prototype. # The 1st parameter here is the shape and data-type of prototype(array-like) that define these same attributes of the returned array. newArr = np.empty_like(arr) print("\nNew Array..\n", newArr) # Get the type of the new array print("\nNew Array type...\n", newArr.dtype) # Get the dimensions of the new array print("\nNew Array Dimensions...\n", newArr.ndim)

## Output

Array... [[35 56 66] [88 73 98]] Array type... int64 Array Dimensions... 2 New Array.. [[94573725212560 0 0] [ 0 0 0]] New Array type... int64 New Array Dimensions... 2

- Related Questions & Answers
- Return a new 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 full array with the same shape and type as a given array 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 masked array containing the same data but with a new shape in Numpy