Array Creation Routines using Numpy

Array Creation Routines using Numpy

A new ndarray object can be constructed by any of the following array creation routines or using a low-level ndarray constructor.

numpy.empty

It creates an uninitialized array of specified shape and dtype. It uses the following constructor −

numpy.empty(shape, dtype = float, order = 'C')

The constructor takes the following parameters.

Sr.No. Parameter & Description
1 Shape
Shape of an empty array in int or tuple of int
2 Dtype
Desired output data type. Optional
3 Order
'C' for C-style row-major array, 'F' for FORTRAN style column-major array

Shape
Shape of an empty array in int or tuple of int
Dtype
Desired output data type. Optional
Order
'C' for C-style row-major array, 'F' for FORTRAN style column-major array

Example

The following code shows an example of an empty array.

import numpy as np
x = np.empty([5,5], dtype = int)
print(x)

The output is as follows −

[[-1725735032         611          64           0           0]
[          0           0           0           0           0]
[  926115427  1664301412   811808869   878785330  1633825330]
[ 1633759587   828715320   959658032   875979365   895759460]
[  895837752  1697657910  1700868152   909258851   842086244]]

Note − The elements in an array show random values as they are not initialized.

numpy.zeros

Returns a new array of specified size, filled with zeros.

numpy.zeros(shape, dtype = float, order = 'C')

The constructor takes the following parameters.

Sr.No. Parameter & Description
1 Shape
Shape of an empty array in int or sequence of int
2 Dtype
Desired output data type. Optional
3 Order
'C' for C-style row-major array, 'F' for FORTRAN style column-major array

Shape
Shape of an empty array in int or sequence of int
Dtype
Desired output data type. Optional
Order
'C' for C-style row-major array, 'F' for FORTRAN style column-major array

Example 1

# array of nine zeros. Default dtype is float
import numpy as np
x = np.zeros(9)
print(x)

The output is as follows −

[0. 0. 0. 0. 0. 0. 0. 0. 0.]

Example 2

import numpy as np
x = np.zeros((6,), dtype = np.int)
print(x)

Now, the output would be as follows −

[0 0 0 0 0 0]

Example 3

# custom type
import numpy as np
x = np.zeros((3,3), dtype = [('x', 'i2'), ('y', 'i2')])
print(x)

It should produce the following output −

[[(0, 0) (0, 0) (0, 0)]
[(0, 0) (0, 0) (0, 0)]
[(0, 0) (0, 0) (0, 0)]]

numpy.ones

Returns a new array of specified size and type, filled with ones.

numpy.ones(shape, dtype = None, order = 'C')

The constructor takes the following parameters.

Sr.No. Parameter & Description
1 Shape
Shape of an empty array in int or tuple of int
2 Dtype
Desired output data type. Optional
3 Order
'C' for C-style row-major array, 'F' for FORTRAN style column-major array

Shape
Shape of an empty array in int or tuple of int
Dtype
Desired output data type. Optional
Order
'C' for C-style row-major array, 'F' for FORTRAN style column-major array

Example 1

# array of ten ones. Default dtype is float
import numpy as np
x = np.ones(10)
print(x)

The output is as follows −

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

Example 2

import numpy as np
x = np.ones([4,4], dtype = int)
print(x)

Now, the output would be as follows −

[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
Array Attributes in Numpy (Prev Lesson)
(Next Lesson) Array From Existing Data using Numpy
', { 'anonymize_ip': true });