Convert 2 arrays as columns of the matrix using NumPy

In this python numpy program, we will convert 2 arrays as columns of the matrix using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create two 1-dimensional arrays using np.array().
  3. Now convert those two 1-dimensional arrays as columns of the matrix using np.column_stack().
  4. Print the output.
				
					import numpy as np
x = np.array((3,8,5))
y = np.array((4,0,7))
z = np.column_stack((x,y))
print(z)
				
			

Output :

				
					[[3 4]
 [8 0]
 [5 7]]
				
			

remove single-dimensional entries from a specified shape.

split an array of 10 elements into 3 arrays, each of which has 2, 3, and 5 elements.

Remove single-dimensional entries from a specified shape using NumPy

In this python numpy program, we will remove single-dimensional entries from a specified shape using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Specify the shape using np.zeros().
  3. Now remove single-dimensional entries from a specified shape using np.squeeze().
  4. Print the output.
				
					import numpy as np
x = np.zeros((2,1,3))
print("After removing single-dimensional entries: ",
      np.squeeze(x).shape)
				
			

Output :

				
					After removing single-dimensional entries:  (2, 3)
				
			

find the ith element of an array.

convert 1-D arrays as columns into a 2-D array.

Find the ith element of an array using NumPy

In this python numpy program, we will find the ith element 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 the ith element of an array using indexing and logic.
  4. Print the output.
				
					import numpy as np
x = np.array([[4,8,7],[2,3,6]])
print("Fifth element: ",x[1,1])
				
			

Output :

				
					Fifth element:  3
				
			

change the data type of an array.

remove single-dimensional entries from a specified shape.

Change the data type of an array using NumPy

In this python numpy program, we will change the data type 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. Change the data type of an array using astype(float) and store the result in another variable.
  4. Print the output.
				
					import numpy as np
x = np.array([[4,8,7],[2,3,6]])
print("Old: \n",x)
y = x.astype(float)
print("New: \n",y)
				
			

Output :

				
					Old: 
 [[4 8 7]
 [2 3 6]]
New: 
 [[4. 8. 7.]
 [2. 3. 6.]]
				
			

find the indices of the minimum value of an array.

find the ith element of an array.

Find the indices of the minimum value of an array using NumPy

In this python numpy program, we will find the indices of the minimum value 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 the indices of the minimum value of the array using np.argmin().
  4. Print the output.
				
					import numpy as np
x = np.array([76,36,56,90])

print("Index of minimum Values: ",np.argmin(x))
				
			

Output :

				
					Index of manimum Values:  1
				
			

find the indices of the maximum value of an array.

change the data type of an array.

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.