Array creation using numpy methods
NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation. For example: np.zeros,np.empty etc.
numpy.empty(shape, dtype = float, order = ‘C’) : Return a new array of given shape and type, with random values.numpy.reshape(array, shape, order = ‘C’)
# Python Programming illustrating
# numpy.empty method
import numpy as geek
b = geek.empty(2, dtype = int)
print("Matrix b : \n", b)
a = geek.empty([2, 2], dtype = int)
print("\nMatrix a : \n", a)
c = geek.empty([3, 3])
print("\nMatrix c : \n", c)
Output :
Matrix b :
[ 0 1079574528]
Matrix a :
[[0 0]
[0 0]]
Matrix a :
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
numpy.zeros(shape, dtype = None, order = ‘C’) : Return a new array of given shape and type, with zeros.
# Python Program illustrating
# numpy.zeros method
import numpy as geek
b = geek.zeros(2, dtype = int)
print("Matrix b : \n", b)
a = geek.zeros([2, 2], dtype = int)
print("\nMatrix a : \n", a)
c = geek.zeros([3, 3])
print("\nMatrix c : \n", c)
Output :
Matrix b :
[0 0]
Matrix a :
[[0 0]
[0 0]]
Matrix c :
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
Numpy | Array Creation
In this article, we are going to explore numpy array creation technique.
Table of Content
- Array creation using List :
- Array creation using array functions
- Methods for array creation in Numpy
Contact Us