Return a new array of given shape without initializing entries and change the default type in Numpy

NumpyServer Side ProgrammingProgramming

To return a new array of given shape, without initializing entries, use the numpy.empty() method in Python Numpy. The 1st parameter is the Shape of the empty array. The default type for empty() is float. We are changing it to "int" using the 2nd parameter i.e. "dtype".

The dtype is the desired output data-type for the array, e.g, numpy.int8. Default is numpy.float64. The order suggests whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.

The function empty() returns an array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None.

NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.

Steps

At first, import the required library −

import numpy as np

To return a new array of given shape, without initializing entries, use the numpy.empty() method in Python Numpy −

arr = np.empty([3, 3], dtype = int)

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("\nOur Array Dimensions...\n", arr.ndim)

Get the number of elements in the Array −

print("\nNumber of elements...\n", arr.size)

Example

import numpy as np

# To return a new array of given shape, without initializing entries, use the numpy.empty() method in Python Numpy
# The 1st parameter is the Shape of the empty array
# The default type for empty() is float. We are changing it to "int" using the 2nd parameter i.e. "dtype"
arr = np.empty([3, 3], dtype = int)

# 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("\nOur Array Dimensions...\n", arr.ndim)

# Get the number of elements in the Array
print("\nNumber of elements...\n", arr.size)

Output

Array...
[0                         0             7307491100562890867]
[8390032522262880356 7232535146496532584 2334111870318900321]
[8314046642435483764 7953674041135475819 160]]

Array type...
int64

Our Array Dimensions...
2

Number of elements...
9
raja
Updated on 10-Feb-2022 05:56:59

Advertisements