How to create an empty and a full NumPy array?


In this tutorial, we will learn to create an empty and a full NumPy array. NumPy stands for Numerical Python. It is a Python library that performs numerical calculations. It provides a multidimensional array object. NumPy is a popular Python library used for working with arrays. It also has functions for working in the domain of linear algebra, sorting, and matrices and is optimized to work with the latest CPU architectures.

NumPy is very fast as it is written in C language, making it more effective for creating arrays. An array represents a collection of items of the same data type stored in memory. By the end of this tutorial, we will be able to learn to create both empty and full array.

Syntax

This is the syntax to create a full array and an empty array.

numpy.full(order of array, fill_value, dtype = None)
numpy.empty(order of array, dtype = None,)

In this syntax, we used ‘numpy.full()’ to create a full array and ‘numpy.empty()’ to create an empty array. The order of the array parameter represents the shape of array. The fill_value and dtype parameter is optional. Fill_value can be a scaler or array which will be assigned to the array. Dtype shows the data type of the array.

Example 1

Here’s an example in which we used the ‘np.empty()’ function to create an empty array, and then we set the order of the array to 2*3. And then we showed the array using the ‘print()’ function.

import numpy as np
arr_empty = np.empty((2, 3))
print(arr_empty)

Output

[[6.93167257e-310 6.93171505e-310 6.93167256e-310]
 [6.93167256e-310 6.93167256e-310 6.93167256e-310]]

Example 2

In this example, in which we used ‘np.full()’ function to create an array, then we set the order of the array to 3*3. And then, we filled the value to 5. Finally, we showed the array by using the ‘print()’ function.

import numpy as np
arr_full = np.full((3, 3), 5)
print(arr_full)

Output

[[5 5 5]
 [5 5 5]
 [5 5 5]]

Example 3

In this example, we set the order of the array to 5*5, and then we set the data type to ‘int’ to create an empty integer array. Whereas in the full array, we set the value to 7, and then we showed the array of integer values.

import numpy as np
arr_empty = np.empty((5, 5), dtype=int)
print("Empty Array")
print(arr_empty)

arr_full = np.full([5, 5], 7, dtype=int)
print("\n Full Array")
print(arr_full)

Output

Empty Array
[[4607182418800017408 4607182418800017408 4607182418800017408
  4607182418800017408 4607182418800017408]
 [4607182418800017408 4607182418800017408 4607182418800017408
  4607182418800017408 4607182418800017408]
 [4607182418800017408 4607182418800017408 4607182418800017408
  4607182418800017408 4607182418800017408]
 [4607182418800017408 4617315517961601024 4617315517961601024
  4621819117588971520 4621819117588971520]
 [4632233691727265792 4636737291354636288 4641240890982006784
  4645744490609377280 4650248090236747776]]

 Full Array
[[7 7 7 7 7]
 [7 7 7 7 7]
 [7 7 7 7 7]
 [7 7 7 7 7]
 [7 7 7 7 7]]

Example 4

In this example, we set the order of the array to 4*4, and then we set the data type to float to create an empty float array. Whereas in full array, we set the value to 6.34 and then we showed the array of floating values.

import numpy as np  
arr_empty = np.empty((4, 4), dtype=float)
print("Empty Array")
print(arr_empty)

arr_full = np.full([4, 4], 6.34, dtype=float)
print("\n Full Array")
print(arr_full)

Output

Empty Array
[[ 1.         -0.11756978  0.87175378  0.81794113]
 [-0.11756978  1.         -0.4284401  -0.36612593]
 [ 0.87175378 -0.4284401   1.          0.96286543]
 [ 0.81794113 -0.36612593  0.96286543  1.        ]]

 Full Array
[[6.34 6.34 6.34 6.34]
 [6.34 6.34 6.34 6.34]
 [6.34 6.34 6.34 6.34]
 [6.34 6.34 6.34 6.34]]

Conclusion

We learned the use of NumPy to create arrays. NumPy is a famous library for creating arrays. Working with NumPy also includes easy-to-use functions for mathematical computations on the array data set. It has several modules for performing basic and special mathematical functions in NumPy. We can also combine multiple libraries with NumPy to enhance the arrays. In conclusion, NumPy is very effective and fast for creating arrays; developers can customize it further for specific uses.

Updated on: 11-May-2023

635 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements