- 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 new array with the same shape and type as a given array and change the order to K style 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. We have set the order to 'K' style using the "order" parameter. ‘K’ means match the layout of prototype as closely as possible.
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
Creating a numpy Three-Dimensional array using the array() method. We have added elements of int type −
arr = np.array([ [[5,10],[15,20]],[[25,30],[35,40]],[[50,60],[70,80]]])
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 new array with the same shape and type as a given array, use the numpy.empty_like() method in Python Numpy. Return the array of uninitialized (arbitrary) data with the same shape and type as prototype. We have set the order to 'K' style using the "order" parameter −
newArr = np.empty_like(arr, order = 'K') 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 # Creating a numpy Three-Dimensional array using the array() method # We have added elements of int type arr = np.array([[[5,10],[15,20]],[[25,30],[35,40]],[[50,60],[70,80]]]) # 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 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. # We have set the order to 'K' style using the "order" parameter. # ‘K’ means match the layout of prototype as closely as possible. newArr = np.empty_like(arr, order = 'K') 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... [[[ 5 10] [15 20]] [[25 30] [35 40]] [[50 60] [70 80]]] Array type... int64 Array Dimensions... 3 New Array.. [[[ 0 0] [ 0 0]] [[140451415756208 140451415756144] [140451415756272 140451415626672]] [[140451415626864 140451415626928] [140451415626992 140451415627568]]] New Array type... int64 New Array Dimensions... 3