New Tutorials:

Python NumPy Array Creation - Empty, Zeroes and Ones

In this tutorial, we will learn how to create an array in the Numpy Library.

In Numpy, a new ndarray object can be constructed by the following given array creation routines or using a low-level ndarray constructor

• `numpy.empty`

• `numpy.zeroes`

• `numpy.ones`

1. Empty Array - Using `numpy.empty`

This is used to create an uninitialized array of specified shape and `dtype`.

The syntax to use this constructor is as follows:

``numpy.empty(shape, dtype, order)  ``

Following is a description of parameters by this constructor:

• shape:
This parameter is used to indicate the desired shape of the specified array.

• dtype:
This parameter indicates the data type of the array items. The default value of this parameter is float.

• order:
The default order or value of this option is the c-style row-major order. This parameter can be set to F for FORTRAN-style (column-major order)

Using `numpy.empty` Basic Example:

The following code is used to create an empty array. Also, elements in an array will show random values because they are not initialized:

``````import numpy as np

# an array with 4 rows and 3 columns
x = np.empty([4,3], dtype = int)
print (x)``````

[[206 0 0]
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]

2. Zero Value Array - Using `numpy.zeroes`

This is used to return a new array of the specified size and each array item will be initialized with 0.

The syntax to use this constructor is as follows:

``numpy.zeros(shape, dtype, order)  ``

Following is a description of parameters by this constructor:

• shape:
This parameter is used to indicate the desired shape of the specified array.

• dtype:
This parameter indicates the data type of the array items. The default value of this parameter is float.

• order:
The default order or value of this option is the c-style row-major order. This parameter can be set to F for FORTRAN-style (column-major order)

Using `numpy.zeroes` Basic Example:

The below code example will create an array of dimension 3x3 with all elements initialized as 0.

``````import numpy as np

arr = np.zeros((3,3), dtype = int)
print(arr)  ``````

[[0 0 0]
[0 0 0]
[0 0 0]]

3. One Value Array - Using `numpy.ones`

This is used to return a new array of the specified size and each array item will be initialized as 1.

The syntax to use this constructor is as follows:

``numpy.ones(shape, dtype, order)  ``

Following is a description of parameters by this constructor:

• shape:
This parameter is used to indicate the desired shape of the specified array.

• dtype:
This parameter indicates the data type of the array items. The default value of this parameter is float.

• order:
The default order or value of this option is the c-style row-major order. This parameter can be set to F for FORTRAN-style (column-major order)

Using `numpy.ones` Basic Example:

Here is a basic example:

``````import numpy as np

arr = np.ones((3,3), dtype = int)
print(arr)  ``````

[[1 1 1]
[1 1 1]
[1 1 1]]

Summary

This tutorial was all about array creation techniques in Numpy library where we covered different ways for array creation with their syntax, parameters and example.