NumPy Operations

Python package NumPy stands for "Numerical Python." The logical computation package includes an efficient N-D array object and provides facilities to link C, C++, and other programming languages. Additionally, it helps with arbitrary number capacity, linear-based math, and other subjects. NumPy displays can be used as a powerful multi-dimensional container for general data. Python Array: Rows and columns make up the robust N-dimensional object known as a Numpy array. We may access its elements and initialize NumPy arrays using a nested Python list. On a structural plane, a Numpy array consists of a mixture of:

  • The Data reference displays the memory location of the Numpy array's initial byte.
  • The name Data type or the dtype argument describes the Python types of elements stored within the particular array.
  • The shape denotes the number of rows and columns the array has.
  • The strides specify how many bytes in the system's memory should be bypassed before moving on to the next array element.

Numpy Basics

Array of Ones

Creates a NumPy array according to the parameters given, with all elements being 1.

Code

Output

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

Array of Zeros

This function returns a NumPy array with all entries set to 0 having dimensions as specified.

Code

Output

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

These functions are simple, and they can be used to create sample arrays which are often needed for various computational purposes.

Eye

Let's now examine how the eye method works. A 2-D array containing ones on its diagonal and zeros everywhere else is this function's output.

Code

Output

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

Similarly, the diag() method builds a 2D array with all other members set to zero, and the items supplied to function as arguments are set to the diagonal elements.

Code

Output

array([[78,  0,  0],
       [ 0, 56,  0],
       [ 0,  0, 89]])

Vstack() and Hstack()

We can use the vstack() function to vertically stack two arrays and hstack() to stack two or more arrays horizontally. Let's practice with several instances.

Code

Output

Vertically stacked: 
 [[2 3 4 5]
 [4 3 5 3]
 [6 3 5 2]
 [8 2 5 8]]
Horizontally stacked: 
 [[2 3 4 5 6 3 5 2]
 [4 3 5 3 8 2 5 8]]

Operations on Numpy Array

Arithmetic Operations

Code

Output

First Array:
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]
Second array:
[[11. 12. 13.]
 [14. 15. 16.]
 [17. 18. 19.]]

Adding two arrays:
[[11. 13. 15.]
 [17. 19. 21.]
 [23. 25. 27.]]

Subtracting two arrays:
[[-11. -11. -11.]
 [-11. -11. -11.]
 [-11. -11. -11.]]

Multiplying two arrays:
[[  0.  12.  26.]
 [ 42.  60.  80.]
 [102. 126. 152.]]

Dividing two arrays:
[[0.         0.08333333 0.15384615]
 [0.21428571 0.26666667 0.3125    ]
 [0.35294118 0.38888889 0.42105263]]

numpy.reciprocal()

This method returns the argument's element-by-element inverse. When an element's absolute value is greater than 1, the outcome is always 0, and an overflow warning is shown for integer 0.

Code

Output

The array is:
[23.  14.  63.9 23.5 23.7 13.   7. ]
After applying the reciprocal function array is:
[0.04347826 0.07142857 0.01564945 0.04255319 0.04219409 0.07692308
 0.14285714]

The second array is: 
[24]
After applying the reciprocal function the array is: 
[0]

numpy.power()

This function treats the original array's elements as the base in the exponents' syntax, which then raises them to the power of the adjacent elements provided in the second array argument.

Code

Output

The original array is:
[3 5 2]
Applying the numpy power function: 
[ 9 25  4]
The second array is:
[2 4 5]
Applying the numpy power function: 
[  9 625  32]

numpy.mod()

This function returns the remainder of the division of the corresponding elements in the input array. The function numpy.remainder() also produces the same result.

Code

Output

The original array:
[ 5 10 15]
The array for diving the original array:
[2 4 5]
Applying the numpy mod function:
[1 2 0]
Applying the numpy remainder function:
[1 2 0]

The following functions are used to perform operations on an array with complex numbers.

numpy.real() ? This function will return the real part of the given complex argument.

numpy.imag() ? This function will return the imaginary part of the complex argument.

numpy.conj() ? This function will return the complex conjugate of the given complex argument. It is obtained by swapping the sign of the imaginary part.

numpy.angle() ? This function will return the angle of the given complex argument. The function has a parameter having the keyword- degree. If set to true, the function will return the angle in degrees; otherwise, the angle is returned in radians.

Code

Output

Our complex array is:
[-0.-6.6j  0.+0.9j 14.+0.j   1.+9.j ]
Applying the numpy real function: 
[-0.  0. 14.  1.]
Applying the numpy imag function: 
[-6.6  0.9  0.   9. ]
Applying the numpy conj function: 
[-0.+6.6j  0.-0.9j 14.-0.j   1.-9.j ]
Applying the numpy angle function: 
[-1.57079633  1.57079633  0.          1.46013911]
Applying the numpy angle function again (result in degrees)
[-90.          90.           0.          83.65980825]

Using Numpy Arrays with Conditional Expressions

To identify the values that meet your criterion, utilize conditionals. The outcome of a conditional operating condition is also a numpy array because "array" is also a numpy array. Our conditional check produces an array containing boolean values as its result.

Code

Output

[False  True False False  True  True False]
[30 53 54]
[30 53 54]

numpy.dot()

We will begin with the cases in which both arguments are scalars or one-dimensional arrays.

Code

Output

12
Dimensions of a:  1
The dot product of [3] and [4] is:  12
The dot product of [ 4 -3] and [-6  3] is:  -33

Logical Operators

The logical operators "or" and "and" also apply to numpy arrays elementwise. For this, we can use the numpy logical_or and logical_and methods.

Code

Output

After or operation: 
 [[ True  True]
 [ True False]]
After and operation: 
 [[False False]
 [ True False]]

Implementing Operations on Arrays Having Different Shapes

Up until now, we have dealt with two distinct instances using simple operations such as "+" or "*":

  • an operation performed on a scalar and a numpy array
  • an operation used to combine two arrays having the same shape.

The next section will demonstrate that we can still apply operators even if an array has a unique shape. But only particular circumstances allow it to function.

Broadcasting

Arrays of different dimensions can be used for arithmetic operations thanks to Numpy's robust Broadcasting technique. This implies that we take a larger dimension array and a smaller dimension array, and we convert or extend the smaller dimension array to the larger dimension array multiple times to carry out an operation. To put this in another way, the smaller array can occasionally be "broadcasted" so that it takes on the same dimension as the larger array.

In our Python software, we can avoid loops with the help of broadcasting. In the C-based Numpy implementations, the iteration happens implicitly. Additionally, we refrain from making duplicates of our data.

A straightforward example is used to illustrate the broadcasting operational principle.

Code

Output

arr1: 
 [[3 2 4]
 [1 3 2]
 [5 3 3]]
arr2: 
 [2 2 2]
Multiplication using broadcasting: 
[[ 6  4  8]
 [ 2  6  4]
 [10  6  6]]
Addition using broadcasting: 
[[5 4 6]
 [3 5 4]
 [7 5 5]]

Distance Matrix

A distance matrix in geometry is a matrix or a two-dimensional array that stores the distances between the members of a set, pairwise taken, in mathematics, computer science, and particularly in graph theory. If the set has n elements, this two-dimensional array's size is n x n.

A distance matrix connecting the coordinates, in our case, the alphabet, to a hypothetical point:

Code

Output

[12 14 13 18 18 16 69 12 23 24 10 45]
[[ 0  2  1  6  6  4 57  0 11 12  2 33]
 [ 2  0  1  4  4  2 55  2  9 10  4 31]
 [ 1  1  0  5  5  3 56  1 10 11  3 32]
 [ 6  4  5  0  0  2 51  6  5  6  8 27]
 [ 6  4  5  0  0  2 51  6  5  6  8 27]
 [ 4  2  3  2  2  0 53  4  7  8  6 29]
 [57 55 56 51 51 53  0 57 46 45 59 24]
 [ 0  2  1  6  6  4 57  0 11 12  2 33]
 [11  9 10  5  5  7 46 11  0  1 13 22]
 [12 10 11  6  6  8 45 12  1  0 14 21]
 [ 2  4  3  8  8  6 59  2 13 14  0 35]
 [33 31 32 27 27 29 24 33 22 21 35  0]]





Latest Courses