Monday, February 25, 2019

NumPy library -3 (Basic operations on NumPy)

In the previous posts we learnt how to create NumPy arrays and define items in it. In this post we shall discuss how to apply various operations to them.

Arithmetic Operators

The common arithmetic operators used with arrays are addition and multiplication. The following program show adding an array with a scalar:

import numpy as np

arr = np.arange(5)
print(arr)
arr= arr+5
print('\n')
print(arr)


The output of the program is shown below:

[0 1 2 3 4]


[5 6 7 8 9]
------------------
(program exited with code: 0)

Press any key to continue . . .


Similarly multiplication of array with a scalar can be done as follows:

import numpy as np

arr = np.arange(5)
print(arr)
arr= arr*5
print('\n')
print(arr)


The output of the program is shown below:

[0 1 2 3 4]

[ 0  5 10 15 20]
------------------
(program exited with code: 0)

Press any key to continue . . .


These operators can also be used between two arrays. In NumPy, these operations are element-wise, that is, the operators are applied only between corresponding elements. These are objects that occupy the same position, so that the end result will be a new array containing the results in the same location of the operands. The following program shows addition and multiplication of two arrays:

import numpy as np

arr1 = np.arange(5)
arr2 = np.arange(5,10)

print('Addition of two arrays\n')
print(arr1+arr2)

print('\nMultiplication of two arrays\n')
print(arr1*arr2)


The output of the program is shown below:

Addition of two arrays

[ 5  7  9 11 13]

Multiplication of two arrays

[ 0  6 14 24 36]


------------------
(program exited with code: 0)

Press any key to continue . . .


We can also multiply the array by the sine or the square root of the elements of array b as shown in the following program:

import numpy as np

arr1 = np.arange(5)
arr2 = np.arange(5,10)

print('Adding the array by the sine value of the elements of array of another array\n')
print(arr1 + np.sin(arr2))

print('\nAdding the array by the square root of the elements of array\n')
print(arr1 + np.sqrt(arr2))

print('\nMultiplying the array by the sine value of the elements of array of another array\n')
print(arr1 * np.sin(arr2))

print('\nMultiplying the array by the square root of the elements of array\n')
print(arr1 * np.sqrt(arr2))


The output of the program is shown below:

Adding the array by the sine value of the elements of array of another array

[-0.95892427  0.7205845   2.6569866   3.98935825  4.41211849]

Adding the array by the square root of the elements of array

[2.23606798 3.44948974 4.64575131 5.82842712 7.        ]

Multiplying the array by the sine value of the elements of array of another arra
y

[-0.         -0.2794155   1.3139732   2.96807474  1.64847394]

Multiplying the array by the square root of the elements of array

[ 0.          2.44948974  5.29150262  8.48528137 12.        ]

------------------
(program exited with code: 0)

Press any key to continue . . .

These operators can also be used with the multidimensional arrays. The following program shows addition and multiplication of the multidimensional arrays:

import numpy as np

arr1 = np.arange(0,9).reshape(3,3)
arr2 = np.ones((3,3))

print(arr1)
print('\n')
print(arr2)

print('\nAdding the arrays\n')
print(arr1 + arr2)


print('\nMultiplying the arrays\n')
print(arr1 * arr2)


The output of the program is shown below:

[[0 1 2]
 [3 4 5]
 [6 7 8]]


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

Adding the arrays

[[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]]

Multiplying the arrays

[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]


------------------
(program exited with code: 0)

Press any key to continue . . .  


We have converted the one dimensional array arr1 into a 3,3 array using the reshape() function. In this case also element wise operation takes place. If a matrix product is required then we can use the dot() function as shown in the following program:

import numpy as np

arr1 = np.arange(0,9).reshape(3,3)
arr2 = np.ones((3,3))

print(arr1)
print('\n')
print(arr2)

print('\nMatrix product of the arrays\n')
print(np.dot(arr1,arr2))


The output of the program is shown below:

[[0 1 2]
 [3 4 5]
 [6 7 8]]


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

Matrix product of the arrays

[[ 3.  3.  3.]
 [12. 12. 12.]
 [21. 21. 21.]]
------------------
(program exited with code: 0)

Press any key to continue . . . 


I hope you are familiar with the matrix product which is the sum of the products of each element of
the corresponding row of the first matrix with the corresponding element of the corresponding column of the second matrix.

There is an alternative way to write the matrix product which is to see the dot() function as an
object’s function of one of the two matrices, i.e. arr1.dot(arr2). In our previous program use this and verify if the resulting matrix is the same as obtained with np.dot(arr1,arr2). What if the order of the operands are changed? i.e result of arr2.dot(arr1) ..The following program shows the result:

import numpy as np

arr1 = np.arange(0,9).reshape(3,3)
arr2 = np.ones((3,3))

print(arr1)
print('\n')
print(arr2)

print('\nMatrix product of the arrays\n')
print(np.dot(arr1,arr2))
print("\nMatrix product of the arrays using dot function as an object's function of one of the two matrices \n")
print(arr1.dot(arr2))
print('\nMatrix product of the arrays by changing the order of the operands\n')
print(arr2.dot(arr1))


The output of the program is shown below:

[[0 1 2]
 [3 4 5]
 [6 7 8]]


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

Matrix product of the arrays

[[ 3.  3.  3.]
 [12. 12. 12.]
 [21. 21. 21.]]

Matrix product of the arrays using dot function as an object's function of one o
f the two matrices

[[ 3.  3.  3.]
 [12. 12. 12.]
 [21. 21. 21.]]

Matrix product of the arrays by changing the order of the operands

[[ 9. 12. 15.]
 [ 9. 12. 15.]
 [ 9. 12. 15.]]
------------------
(program exited with code: 0)

Press any key to continue . . .


The above result shows that the matrix product is not a commutative operation, thus the order of the
operands is important.

Increment and Decrement Operators

In Python the increment and decrement operations is conducted using += and -= operators. The following example shows their usage:

import numpy as np

arr1 = np.arange(5)

print(arr1)
print('\n')
arr1 += 1
print(arr1)
print('\n')
arr1 -= 1
print(arr1)


The output of the program is shown below:

[0 1 2 3 4]

[1 2 3 4 5]


[0 1 2 3 4]
------------------
(program exited with code: 0)

Press any key to continue . . . 


These operators instead of creating a new array with the results, reassign the results to the same array. Thus we can use them every time we want to change the values in an array without generating a new one. The following program shows how this can be done:

import numpy as np

arr1 = np.arange(5)

print(arr1)
print('\n')
arr1 += 5
print(arr1)
print('\n')
arr1 *= 5
print(arr1)


The output of the program is shown below:

[0 1 2 3 4]

[5 6 7 8 9]
 

[25 30 35 40 45]
------------------
(program exited with code: 0)

Press any key to continue . . .


Universal Functions (ufunc)

These functions operate on an array in an element by element fashion. Thus they act individually on each single element of the input array to generate a corresponding result in a new output array of the same size as the input. The most common examples of these are the mathematical and trigonometric operations. The following program shows some of these functions:

import numpy as np

arr1 = np.arange(1,5)
print(arr1)
print('\n')
print(np.sqrt(arr1))
print('\n')
print(np.log(arr1))
print('\n')
print(np.sin(arr1))


The output of the program is shown below:

[1 2 3 4]

[1.         1.41421356 1.73205081 2.        ]


[0.         0.69314718 1.09861229 1.38629436]


[ 0.84147098  0.90929743  0.14112001 -0.7568025 ]


------------------
(program exited with code: 0)

Press any key to continue . . . 


Aggregate functions

The Aggregate functions perform an operation on an array and produce a single result. Therefore, the sum of all the elements in an array is an aggregate function. Many Aggregate functions are implemented within the class ndarray. The following program shows some of these functions:

import numpy as np

arr1 = np.arange(1,5)
print(arr1)
print('\n')
print(arr1.sum())
print('\n')
print(arr1.min())
print('\n')
print(arr1.max())
print('\n')
print(arr1.mean())
print('\n')
print(arr1.std())

The output of the program is shown below:

[1 2 3 4]

10

1

4

2.5

1.118033988749895

------------------
(program exited with code: 0)

Press any key to continue . . .

Here I am ending today's post. In the next post we shall further explore NumPy library and see how to manipulate the array objects, i.e how to select elements through indexes and slices, in order to obtain the values contained in them or to make assignments in order to change their values. Till we meet next keep practicing and learning Python as Python is easy to learn!


Share:

0 comments:

Post a Comment