Python Numpy and N-dimensional array operations-1

Numpy is a very essential Python package used in Numerical and Scientific computing. Numpy library comes with rich mathematical computations like linear algebra, mathematical transforms as Fourier transforms etc. Numpy is the foundation for many opensource Python packages for Maths, scientific computing, and data analysis (scikit, pandas, etc…). These packages are built on top of Numpy.

The most important data type of numpy is ndarray which is an N-dimensional array.

#### Numpy Example creating an array

import numpy as np
array_ex = np.array ([0,1,2,3,4,5,6,7,9])

print ("array_ex",array_ex)

list_1 = [0,1,2,3,4,5,6,7,9]
array_ex1 = np.array(list_1)

print("array_ex1",array_ex1)

Above example show how to use numpy for creating an array from Python list. In this simple example, we are creating an array using np.array () function which takes python list as argument.

import numpy as np
array_ex = np.array ([1,2,3,4,5,6])
print (array_ex)

# [1 2 3 4 5 6]  This is the output of above statements

new_array = np.reshape (array_ex,(2,3))
print (new_array)

### Output of new_array print
[[1 2 3]
 [4 5 6]]

array_ex2 = np.array ([1,2,3,4,5,6,7,8,9])
new_array_ex2 = np.reshape (array_ex2,(3,3))
print (new_array_ex2)

#### Output of new_array_ex2
[[1 2 3]
 [4 5 6]
 [7 8 9]]

zeros_array = np.zeros ((3,3))
print (zeros_array)

## Output of print zeros_array

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

arra_ones = np.ones ((5,4))
print (arra_ones)
## Output of print arra_ones
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]

array_ex_num_str = np.array ([["c2plabs","makerspace","robotics"],[23,45,43],[21,56,45]])
print (array_ex_num_str)
## Output of print array_ex_num_str is
[['c2plabs' 'makerspace' 'robotics']
 ['23' '45' '43']
 ['21' '56' '45']]

diag_array = np.eye (4)
print (diag_array)
## Output of print diag_array is

[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

When numpy array is created with Python List the array that is created is a one-dimensional vector. To convert the array into multidimensional  np.reshape can be used as shown in the example where np.reshape (array_ex,(2,3)) convert array_ex into a 2-dimensional array with 2-rows and 3-columns and the newly created array is stored in new_array.

To create a multidimensional array with all elements set to zero, np.zeros ((shape)) function is used. In the above given code snippet zeros_array = np.zeros ((3,3)) creates 2-dimensional array with 3-rows and 3-columns and every element as zero. Similarly, np.ones((shape)) is used to create a multidimensional with all elements set to value one. arra_ones = np.ones ((5,4)) in the above code creates 2-dimensional array with 5-rows and 4-columns and every element as one.

Numpy multidimensional arrays can be a combination of different data types like strings and integers. np.array () funcation can be used to create a multidimensional array by giving a list of lists as an argument.

array_ex_num_str = np.array ([[“c2plabs”,”makerspace”,”robotics”],[23,45,43],[21,56,45]])

creates a multidimensional array as

[['c2plabs' 'makerspace' 'robotics']
 ['23' '45' '43']
 ['21' '56' '45']]

To create diagonal multidimensional  array numpy provides a function np.eye (dimension)

np.eye creates diagnol square array/matrix  where all diagnoal elements are initiaized to one and all other elements are set to zero.

In the next blog of this series will covers about slicing, indexing and iterating over of multidimensional array using numpy.

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *