Creating Arrays Using Numpy.ndarray

Overview:               

  • NumPy enables usage of multidimensional arrays with its ndarray class.

 

  • An ndarray instance can hold arrays of any dimension subject to the availability of the physical memory of the system.

 

  • The elements held in an ndarray are of same type. Hence an ndarray is a container of homogeneous types.

 

  • Unlike the array class offered by the python standard library, the ndarray from numpy, offers different variants of fundamental types that can be stored.

 

  • For example, the types int8, int16, int32, int64, float16, float32, float64, complex64, complex128 are all different variants of fundamental types supported by numpy.ndarray, based on the maximum value that can be represented and precision.

 

  • ndrray can store any object of type 'object'. When type of the elements is 'object', the elements are stored as references to the objects and not the object themselves.

 

  • There is no explicit support to specify the data type stored as a user-defined type. However, the facility to store the type 'object', allows storing any type of user-defined types polymorphically.

 

  • ndarray  objects created are of fixed length. The ndarrays cannot be made to grow. However, there are methods available that return a new modified/appended array objects.

 

Creating arrays of 'n' dimensions using numpy.ndarray:

 

  • Creation of ndarray objects using NumPy is simple and straightforward.

 

  • Import the numpy module. Since ndarray is a class, ndarray instances can be created using the constructor.

 

  • The important and mandatory parameter to be passed to the ndarray constructor is the shape of the array.

 

  • The shape of an ndarray is a tuple describing the size of each dimension of the array. The shape of a ndarray object is given by the attribute shape.

 

  • For a one-dimensional array of 128 elements the shape will be 128. For a two dimensional array of 2 rows and 3 columns the shape of the array will be (2,3).

 

Example – Creating a one dimenstional array using numpy.ndarray:

import numpy as np

 

# Create an numpy ndarray of 1 dimension and length 10

array_1d = np.ndarray(10)

 

# Populate the ndarray

for x in range (0, array_1d.shape[0]):

    array_1d[x] = x*10

   

# Print the attributes of the array   

print("The shape of the numpy.ndarray object:%d"%(array_1d.shape[0]))

print("The dimension of the numpy.ndarray object:%d"%(array_1d.ndim))

print("The type of elements stored in the numpy.ndarray object is:%s"%(array_1d.dtype))

print("Printing the contents of the ndarray object:")

 

# Print the ndarray

for x in range (0, array_1d.shape[0]):

                 print(array_1d[x])

Output:

The shape of the numpy.ndarray object:10

The dimension of the numpy.ndarray object:1

The type of elements stored in the numpy.ndarray object is:float64

Printing the contents of the ndarray object:

0.0

10.0

20.0

30.0

40.0

50.0

60.0

70.0

80.0

90.0

Example – Creating a two dimenstional array using numpy.ndarray:

import numpy as np

 

# Create a matrix of 3x4 dimensions - 3 rows and four columns

array_2d = np.ndarray((3,4))

 

# Populate the 2 dimensional array created using nump.ndarray

for x in range(0, array_2d.shape[0]):

    for y in range(0, array_2d.shape[1]):

        array_2d[x][y] = x*y*10

       

 

# Print the attributes of the 2 dimensional ndarray   

print("The shape of the numpy.ndarray object:%d,%d"%(array_2d.shape[0],array_2d.shape[1]))

print("The dimension of the numpy.ndarray object:%d"%(array_2d.ndim))

print("The type of elements stored in the numpy.ndarray object is:%s"%(array_2d.dtype))

 

# Print the 2 dimensional ndarray object:

print("Printing the 2 dimensional ndarray contents:")

for x in range(0, array_2d.shape[0]):

    for y in range(0, array_2d.shape[1]):

        print("(%d, %d):%d"%(x,y,array_2d[x][y]))      

 

Output:

The shape of the numpy.ndarray object:3,4

The dimension of the numpy.ndarray object:2

The type of elements stored in the numpy.ndarray object is:float64

Printing the 2 dimensional ndarray contents:

(0, 0):0

(0, 1):0

(0, 2):0

(0, 3):0

(1, 0):0

(1, 1):10

(1, 2):20

(1, 3):30

(2, 0):0

(2, 1):20

(2, 2):40

(2, 3):60


Copyright 2020 © pythontic.com