Create numpy array of specific type and dimension

In this post, we are going to learn about how to create numpy array of specific types and dimensions using numpy library functions np.tile() ,np.empty(),np.zeros(),np.array() with examples by using these examples.

1. np.tile() create numpy array of specify type


The numpy. tile() create and return a new array after repeating the given element number of reps. In the below example, we have created a 1D array by using tile() in which the number is 9 and reps is 10.

We have created a three-dimensional array in which the number is 9 and reps is (3,3).

import numpy as np
print(np.tile(9, 10))

print(np.tile(9, (3,3)))

Output

[9 9 9 9 9 9 9 9 9 9]

[[9 9 9]
 [9 9 9]
 [9 9 9]]

Tile(): to create an array of specific type

  • In this example, we have created an array of specific types using np. array()
  • Passed the array created by using np.array() to tile() function along with shape(1,1)
import numpy as np
arr = np.array([[3,6,9,12],[12,15,18,21]],dtype='i')
nparr = np.tile(arr,(1,1))
print(nparr)

Output

[[ 3  6  9 12]
 [12 15 18 21]]

2. np.empty() to create numpy array of specific type


In this example, we have created a numpy array of space(3,3) specify type int. The numpy. empty(shape, type) return a new array of given shape and type without initializing any value

import numpy as np
nparr = np.empty((3, 3), dtype=int)
print(nparr)

Output

[[  0   0   0]
 [  0   0   0]
 [920   0   0]]

3. np.empty() to create numpy array of specific type


In this example, we will learn how to create a numpy array of specific dimensional using np. full(shape,fill_value, type) function that returns a new array of a given shape and fills all elements with the same value that passed the fillvalue parameter

We have created an array of shapes (3,3),fillvalue=9 specify the dtype as int.

import numpy as np
nparr = np.full((3,3), 9, dtype = int)
print(nparr)

Output

[[9 9 9]
 [9 9 9]
 [9 9 9]]

4.np.zeros() to create numpy array of specific type


The python numpy.zeros(shape,dtype) function returns a new array of given shape and type and fills the array with zero. In this example, we have created a numpy array of shapes (3,3, type =int and filled it with values 90 instead of zeros.

import numpy as np
nparr = np.zeros((3,3), dtype=int) + 90
print(nparr)

Output

[[90 90 90]
 [90 90 90]
 [90 90 90]]

5. numpy.array to create array of specify type


In this python program example, we have created an array of the specific type, passing an array with the datatype. It will create an array of the specified type and dimensional. We can change the datatype of the numpy array as per the need datatype selected from datatype.

character usedDatatype
irepresent an integer data type
bused for boolean datatype
uused for unsigned integer
ffloat type
ccomplex float
mtime delta
MDatetime
Oobject
Sstring
UUnicode string
NumPy Datatype

Python Program to Create numpy array of specific type and dimension

import numpy as np
nparr = np.array([[3,6,9,12],[12,15,18,21]],dtype='i')

print(nparr)


Output

[[ 3  6  9 12]
 [12 15 18 21]]

Summary

In this post, we have learned how to Create numpy array of specific type and dimension using numpy library function np.tile() ,np.empty(),np.zeros(),np.array().