
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
Numpy empty_like() Function
The Numpy empty_like() function in Python is used to create a new array with the same shape and type as an existing array, but with uninitialized values. It's useful for quickly setting up an array of a specific shape without pre-assigning values, making it efficient for scenarios where initial values are not required.
The numpy.empty_like() function leaves the array values uninitialized, so the output may vary and contain arbitrary values present in memory.
Syntax
Following is the syntax of the Numpy empty_like() function −
numpy.empty_like(arr, dtype=None, order='K', subok=True, shape=None)
Parameters
Following are the parameters of the Numpy empty_like() function −
- arr: Array whose shape and type define the shape and type of the output array.
- dtype(optional): The desired data type for the returned array. If None, the data type of arr is used.
- subok(optional): If True, the newly created array will use the subclass type of arr.
- shape(optional): Overrides the shape of the result.
- order (optional): It specifys the memory layout of the array. If object is not an array, the newly created array will be in C order (row major) unless F is specified, in which case it will be in Fortran order (column major) −
- 'C': C-style row-major order.
- 'F': Fortran-style column-major order.
- 'A': 'F' if the input is Fortran contiguous, 'C' otherwise.
- 'K': This is the default value keep the order as close as possible to the input.
Return Values
This function returns an array with uninitialized values that has the same shape and type as the input array.
Example
Following is a basic example to create a numpy array with the same shape as an existing array but with uninitialized values using Numpy empty_like() function −
import numpy as np original_array = np.array([[1, 2, 3], [4, 5, 6]]) empty_array = np.empty_like(original_array) print("Numpy Array with uninitialized values -\n", empty_array)
Output
Following is the output of the above code −
Numpy Array with uninitialized values - [[ 140086793428272 140086793428272 7575175998026834798] [7307491100562890867 8390032522262880356 7232535146496532584]]
Specifying Data Type
We can specify the data type of the uninitialized array using the dtype parameter. For example, if we need the float data type, we set the dtype parameter to 'float'.
Float Datatype Array
In the following example, the array has a specified data type of float32:
import numpy as np array_c = np.array([[7, 8, 9], [10, 11, 12]], dtype=np.int32) empty_array_float = np.empty_like(array_c, dtype=np.float32) print("Numpy Array with uninitialized values and Float Data Type -\n", empty_array_float)
Output
Following is the output of the above code −
Numpy Array with uninitialized values and Float Data Type - [[-3.1457752e-09 4.5900932e-41 -3.1457752e-09] [ 4.5900932e-41 1.9096748e-19 1.9348873e-19]]
String Datatype Array
When we create a string data type array using numpy.empty_like(), it initializes each entry with an unassigned placeholder, often random characters due to uninitialized memory.
import numpy as np array_d = np.array([[7, 8, 9], [10, 11, 12]], dtype=np.int32) empty_array_string = np.empty_like(array_d, dtype=np.str_) print("Numpy Array with uninitialized String Data Type -\n", empty_array_string)
Output
Following is the output of the above code −
Numpy Array with uninitialized String Data Type - [['' '' ''] ['' '' '']]
Example : Multi-dimensional Array
The numpy.empty_like() function can also be used to create a multi-dimensional array with the same shape and type as an existing multi-dimensional array, but with uninitialized values. This is useful when working with higher-dimensional data structures.
In this example, we create a 3D array with uninitialized values, matching the shape of a 3x3x3 array:
import numpy as np original_3d_array = np.ones((3, 3, 3)) empty_3d_array = np.empty_like(original_3d_array) print("3D Numpy Array with uninitialized values -\n", empty_3d_array)
Output
Following is the output of the above code −
3D Numpy Array with uninitialized values - [[[ 1.13390552e-313 0.00000000e+000 6.91661577e-310] [-9.36148521e-273 6.91661583e-310 6.91661577e-310] [ 1.13241387e-009 6.91661576e-310 6.91661577e-310]] [[ 1.99122267e-022 6.91661576e-310 6.91661577e-310] [-4.66087507e-037 6.91661576e-310 6.91661577e-310] [ 1.78189299e+079 6.91661577e-310 6.91661577e-310]] [[ 3.14604004e-188 6.91661576e-310 6.91661577e-310] [-4.72870412e-200 6.91661576e-310 6.91661577e-310] [-2.68644763e+091 6.91661577e-310 1.73911107e-321]]]