Find the indices of the maximum value of array using NumPy

In this python numpy program, we will find the indices of the maximum value of array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Find the indices of the maximum value of the array using np.argmax().
  4. Print the output
				
					import numpy as np
x = np.array([76,36,56,90])

print("Index of maximum Values: ",np.argmax(x))
				
			

Output :

				
					Index of maximum Values:  3
				
			

find the union of two arrays.

find the indices of the minimum value of an array.

Find the union of two arrays using NumPy

In this python numpy program, we will find the union of two arrays using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create two arrays using np.array().
  3. Find the union of two arrays using np.union1d().
  4. Print the output
				
					import numpy as np
x = np.array([56,18,28,36])
y = np.array([76,36,56,90])

print(np.union1d(x,y))
				
			

Output :

				
					[18 28 36 56 76 90]
				
			

find the set difference between two arrays.

find the indices of the maximum value of an array.

Find the set difference between two arrays using NumPy

In this python numpy program, we will find the set difference between two arrays using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create two arrays using np.array().
  3. Find the set difference between two arrays using np.setdiff1d().
  4. Print the output
				
					import numpy as np
x = np.array([56,18,28,36])
y = np.array([76,36,56])

print(np.setdiff1d(x,y))
				
			

Output :

				
					[18 28]
				
			

get the unique elements of an array.

find the union of two arrays.

Get the unique elements of an array using NumPy

In this python numpy program, we will get the unique elements of an array using NumPy

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Find unique elements of the array using np.unique().
  4. Print the output
				
					import numpy as np
x = np.array([25,33,10,45,33,10])

print(np.unique(x))
				
			

Output :

				
					[10 25 33 45]
				
			

find common elements between two arrays.

find the set difference between two arrays.

Find common elements between two arrays using NumPy

In this python numpy program, we will find common elements between two arrays using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create two arrays using np.array().
  3. Find common elements between two arrays using np.intersect1d().
  4. Print the output
				
					import numpy as np
x = np.array([56,18,28,36])
y = np.array([76,36,56])

print(np.intersect1d(x,y))
				
			

Output :

				
					[36 56]
				
			

find the real and imaginary parts of an array of complex numbers.

get the unique elements of an array.

Find the real and imaginary parts of an array of complex numbers using NumPy.

In this python numpy program, we will find the real and imaginary parts of an array of complex numbers using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array of complex numbers using np.array().
  3. Find the real and imaginary parts of an array of complex numbers using real and imag.
  4. Print the output.
				
					import numpy as np
x = np.array([6+2j,10+5j])
print("Real part: ",x.real)
print("Imaginary part: ",x.imag)
				
			

Output :

				
					Real part:  [ 6. 10.]
Imaginary part:  [2. 5.]
				
			

add a row of a matrix into another row.

find common elements between two arrays.

program to add a row of a matrix into another row using NumPy

In this python numpy progra, we will add a row of a matrix into another row using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create a matrix using np.array().
  3. Add row of the matrix to another row of the same matrix using Indexing and logic.
  4. Print the output.
				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])
print("Original matrix: ",x)
x[0,:] = x[0,:]+x[1,:]
print("After addition: ",x)
				
			

Output :

				
					Original matrix:  [[ 6  5]
 [10  9]
 [ 8  7]]
After addition:  [[16 14]
 [10  9]
 [ 8  7]]
				
			

multiply a row of an array by a scalar.

find the real and imaginary parts of an array of complex numbers.

Program to multiply a row of an array by a scalar using NumPy

In this python numpy program, we will multiply a row of an array by a scalar using NumPy

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Multiply a row of an array by a scalar using Indexing and logic.
  4. Print the output
				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])
print("Original matrix: ",x)
x[0,:] = x[0,:]*2

print("After multiplyting first row by 2: ",x)
				
			

Output :

				
					Original matrix:  [[ 6  5]
 [10  9]
 [ 8  7]]
After multiplyting first row by 2:  [[12 10]
 [10  9]
 [ 8  7]]
				
			

swap rows of a given array.

add a row of a matrix into another row.

Program to swap rows of a given array using NumPy.

In this python numpy program, we will swap rows of a given array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Swap rows of a given array using Indexing and logic.
  4. Print the output.
				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])
print("Original array: ",x)
print("After swapping")
new = print(x[::-1, :])
				
			

Output :

				
					Original array:  [[ 6  5]
 [10  9]
 [ 8  7]]
After swapping
[[ 8  7]
 [10  9]
 [ 6  5]]
				
			

replace all numbers in a given array that is equal or greater than a given number.

multiply a row of an array by a scalar.

Replace all numbers in a given array that is equal or greater than a given number

In this python numpy program, we will replace all numbers in a given array that is equal or greater than a given number

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Replace all numbers in a given array that is equal to or greater than a given number using Indexing and logic.
  4. Print the output,
				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])
print("Original array: ",x)
x[x >= 7] = 0
print("New matrix")
print(x)
				
			

Output :

				
					Original array:  [[ 6  5]
 [10  9]
 [ 8  7]]
New matrix
[[6 5]
 [0 0]
 [0 0]]
				
			

extract all numbers from a given array that are greater than a specified number.

swap rows of a given array using NumPy.