Join Class

mumpy array functions and operators

In earlier post we talked about how to start working with numpy array, here we learn about some arithmetic operators and some array function that we may use in our regular python program development.

numpy complex array example

Arithmetic Operators on Arrays

We can simply multiply or add two array with same dimension. here we see some example of how to use operators with one dimension and two dimension array.

import numpy as np

ar1 = np.array([40,30,70,90])
ar2= np.array([15,20,60,20])


#result will be 
[ 600  600 4200 1800]

To multiply array, we must keep same number of elements in each array otherwise it will produce error, for example the following will throw an error.

import numpy as np

ar1 = np.array([40,30,70,90, 15])
ar2= np.array([15,20,60,20])


#this will throw error

Like multiplication, we also can add array with same number of elements in it, here is an example

import numpy as np

ar1 = np.array([40,30,70,90])
ar2= np.array([15,20,60,20])

print(ar1 + ar2)

#result will be 
[ 55  50 130 110]

Same addition and multiplication can be done with two dimension array, but make sure you have same data length and data structure.

Here is an example of how operator can be used with two-dimension array.

ar1 = np.array([[40,30], [70,90]])
ar2= np.array([[15,20], [60,20]])

print(ar1 * ar2)

[[ 600  600]
 [4200 1800]]

print(ar1 + ar2)
[[ 55  50] 
 [130 110]]

Complex Number and Complex Array

First we need to understand complex number then array.

A complex number is any number that can be represented by one real part and one imaginary part, like a+bj, a is the real number and bj is the imaginary number.

This is how we can define complex number.

cn0= (a + b)

We also can create a complex number from two real number using complex(1,2) method, and from that complex number (result) we can find the real part using real property and imaginary part using imag property like below example.

cn1= complex(4,7) 

print (cn1.real)
print (cn1.imag)

There are many built-in function supports complex number in numpy, like we can call abs, pow etc with any complex number.

cn1= complex(4,7) 
#result: (4+7j)

print(abs(cn1)) #result: 8.06225774829855
print(pow(cn1,2)) #reult: (-33+56j)

dot function and matrix product

matrix product can be performed two ways, one using @ operator and secondly using dot method, both will produce exact same result

ar1 = np.array([[2,3], [6,7]])
ar2= np.array([[5,2], [3,2]])

print(ar1 @ ar2)

[[19 10]
 [51 26]]

print(np.matmul(ar1, ar2))
print(, ar2))

Numpy dot function dot(x-array, y-array) returns dot results from two arrays (X-axis and y-axis), which is a scalar value for 1D array normal matrix multiplication

Here is an example of 1D array

xArr1 = [6, 8, 10]
yArr1 = [5, 6, 30]

print(,yArr1)) #378

#(6*5 + 8*6 + 10*30)= 378

Instead of 1d array, if we try two dimensions array with dot function, it will rerun an array instead of scalar property.

Find an element from array in numpy

Here we see how to find an element from array, if we find the element we return the position number otherwise return -1, which indicates specified element not found.

import numpy as np

def searchElement(ar, key):
    length = len(ar)
    for i in range (length):
        if (ar[i] == key):
            return i
    return -1

arr1 = [19, 58, 10, 96, 70]
key = 10
result= searchElement(arr1, key)

Now instead of searching any element in above traditional way, in numpy we have a more efficient way to search any element using where clause like linq query.

arr1 = np.array([11, 10, 13, 9, 15, 12, 28, 9, 57, 32, 8, 24, 7, 20, 40])
result = np.where(arr1 == 9)

in above code, we are searching number 9 in a specified array, the result will be an array, which will contain all the index position wherever the number 9 is found in array.

We can specify any conditions in where clause, for example, to find all even numbers np.where(arr1%2 == 0) or odd numbers np.where(arr1%2 == 1)

numpy array functions
Free Tutorials
numpy array operators
Python NumPy examples | Join Python Course