In this post, we will understand how to create an empty numpy array. The Numpy array empty array can be created by using numpy. zeros() and numpy.empty().The difference is that zero() initializes the numpy array with zero whereas numpy.empty() create an array without initializing any value.
Ways to Create empty Numpy array
- Create empty Numpy array using empty() function.
- Create empty NumPy array using list
- Create empty NumPy array using zeros() function.
- Create empty NumPy array without shape.
- Create empty NumPy 2D array
- Create empty numpy 3d array
- Create NumPy empty array of string
- Create Numpy empty array and concatenate
1. Create empty NumPy array using empty() function
To create an empty numpy array in the Python Numpy library empty() function is used. It creates an empty array of a given shape. The empty() function takes two arguments shapes and datatype.
Syntax
numpy.empty(shape,datatype=float,order={c,f},like=none) |
Parameters
- Shape :It is shape of empty array like (2,3)
- datatype: The datatype of output array by default it is numpy.float64()
- order :Whether to store multidimesional array data row-wise(c) or column-wise (f).
- like :array_like
Return
It returns a new array of given shapes and sizes. But without initializing entries.
Python Program to create numpy array
In this python program example, We will learn how to create a numpy empty array by simply using numpy.empty() function that takes two arguments shape and datatype. We have given shape(0,3) and type int. empty() function. This is how to create NumPy empty array.to create a numpy empty array and append
import numpy as np
#numpy 2d array initialize
nparr = np.empty((0, 3), int)
print(nparr)
#output
[]
2. Create empty NumPy array from list
To create an empty NumPy array we can pass an empty list to the np.array() function. It will create an empty array without initializing any value. This is how we create a numpy empty array from a list.
import numpy as np
mylst = []
nparr = np.array(mylst)
print(nparr)
#output
[]
3. Create empty NumPy array using np.zeros()
The numpy. zeros() function Return a new array of given shape and type, initialized with zeros. Let us understand how will create an empty numpy array using np. zeros() function.
import numpy as np
nparr = np.zeros((3,2))
print(nparr)
Output
[[0. 0.]
[0. 0.]
[0. 0.]]
4. Create empty NumPy array without shape
In this Python example, we will understand how to create an empty NumPy array without shape
- We have declared an empty list to an empty array.
- we have np array(nparr) that is created from list of elements.
- for loop to through list and using np.append() function to append to element in list.list is empty,so it create an python numpy empty array.
import numpy as np
lst=[]
nparr = np.array([3,6,9,12])
for item in lst:
nparr = np.append(nparr, item)
print('empty array:'lst)
Output
empty array :[]
5. Create empty numpy 2d array
To creat empty numpy 2d array we have used numpy.empty()function.
- It takes shape and datatype as agrument.We have specified shape(0,3) as a tuple and datatype as int.
- The np.empty() function will return a 2dimesional empty Numpy array of given shape and would have datatype int.
- if you dnot specified the dataype the default will be float
import numpy as np
nparr_empty = np.empty((0, 3), int)
print('Create 2D Numpy Empty array:')
print(nparr_empty)
Output
Create 2D Numpy Empty array:
[]
6. Create empty numpy 3d array
In this Python program, we will learn how to create an empty numpy 3d array by using the numpy.empty() function.
- It takes shape and datatype as agrument.We have specified shape(2, 3,3) and datatype as int.where 2 is legth of matrix.
- The np.empty() function will return a 3dimesional empty Numpy array of given shape and would have datatype int.
- if we donot specified the dataype the default will be float.
import numpy as np
nparr_empty = np.empty((2,3,3), int)
print('3D Numpy Empty array:')
print(nparr_empty,nparr_empty.shape)
Output
3D Numpy Empty array:
[] (0, 2, 3)
7. Create empty Numpy array of string
In this Python program, we will learn to create a NumPy array of strings by using the np.empty() function.
- To create an NumPy array of string we can simply use np.empty() function.
- To create a NumPy array of string of 3 string we have to specified dtype=’s2′.The np.empty() function takes shape and dtype as agruments.
import numpy as np
nparr = np.empty(3, dtype='S2')
print(nparr)
Output/SY
[b'\xbc\xf8' b'\xed]' b'\x0c\x02']
8. Create empty array and concatenate
In this python program, we will learn how to create an empty array and concatenate by using the numpy empty() function.
- We have used np.empty() function to create an empty array of shape(2,2).It takes two argument shape and datatype the defulat datatype is float.
- We have created an another array of from a list of lists.
- We are simply using np.concatenate() function to concatenate
import numpy as np
nparr1 = np.empty([2,2])
nparr2 = np.array([[2, 3], [4,5]])
Res_nparr = np.concatenate((nparr1,nparr2))
print('\n',Res_nparr)
Output
[[0. 0.]
[0. 0.]
[2. 3.]
[4. 5.]]