Extract all numbers from a given array using NumPy

In this python numpy program, we will extract all numbers from 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. Extract all numbers from a given array that are greater than a specified number using Indexing and logic.
  4. Print the output.
				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])
print("Array: ",x)
print("Values bigger than 7 =", x[x>7])
				
			

Output :

				
					Array:  [[ 6  5]
 [10  9]
 [ 8  7]]
Values bigger than 7 = [10  9  8]
				
			

sort a given array by column in ascending order.

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

Sort an array by column in ascending order using NumPy.

In this python numpy program, we will sort an array by column in ascending order using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Sort an array by column in ascending order using np.sort() and set the axis equal to 0.
  4. Print the output.
				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])

print("Sorting matrix by column: ")
print(np.sort(x,axis=0))
				
			

Output :

				
					Sorting matrix by column: 
[[ 6  5]
 [ 8  7]
 [10  9]]
				
			

sort an array by row in ascending order.

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

Sort an array by row in ascending order using NumPy

In this python numpy program, we will sort an array by row in ascending order using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Sort an array by row in ascending order using np.sort().
  4. Print the output.
				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])

print("Sorting matrix by row: ")
print(np.sort(x))
				
			

Output :

				
					Sorting matrix by row: 
[[ 5  6]
 [ 9 10]
 [ 7  8]]
				
			

interchange the rows of a matrix.

sort a given array by column in ascending order.

Program to interchange the rows of a matrix using NumPy.

In this python numpy program, we will interchange the rows of a matrix using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create a matrix using np.array().
  3. Interchange the rows of a matrix using transpose().
  4. Print the output.
				
					import numpy as np
x = np.array([[5, 6],
              [7, 8],
              [9, 10]])
print(x.transpose())
				
			

Output :

				
					[[ 5  7  9]
 [ 6  8 10]]
				
			

create a one-dimensional array.

sort an array by row in ascending order.

Program to create a one-dimensional array using NumPy.

In this python numpy program, we will create a one-dimensional array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create a one-dimensional array using np.array().
  3. Print the output.
				
					import numpy as np
x = np.array([65,90,1,3,5])

print(x)
				
			

Output :

				
					[65 90  1  3  5]
				
			

check whether two arrays are element-wise equal.

interchange the rows of a matrix.

Check whether two arrays are element-wise equal using NumPy.

In this python numpy program, we will check whether two arrays are element-wise equal using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create two arrays using np.array().
  3. Check whether two arrays are element-wise equal using equal to sign ( == ).
  4. Print the output.
				
					import numpy as np
x = np.array([5, 3])
y = np.array([7,4])

print("Is two arrays element wise equal? : ",x == y)
				
			

Output :

				
					Is two arrays element wise equal? :  [False False]
				
			

find the missing data in a given array.

create a one-dimensional array.

Program to find the missing data in an array using NumPy.

In this python numpy program, we will find the missing data in 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 missing data in a given array using np.isnan().
  4. It will return True if the value in an array is missing (i.e. nan- not a number) else False.
  5. Print the output.
				
					import numpy as np
x = np.array([[1, 5, np.nan],
              [8, np.nan, 9]])

print(np.isnan(x))
				
			

Output :

				
					[[False False  True]
 [False  True False]]
				
			

convert an array into a list.

check whether two arrays are element-wise equal.

Program to convert an array into a list using NumPy.

In this python numpy program, we will convert an array into a list using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Convert an array into a list using tolist() and store the result in a variable.
  4. Print the output.
				
					import numpy as np
x = np.array([[3,0],
             [6,4]])
print("Matrix: \n",x)
y = x.tolist()
print("List: ",y)
				
			

Output :

				
					Matrix: 
 [[3 0]
 [6 4]]
List:  [[3, 0], [6, 4]]
				
			

convert the list into an array.

find the missing data in a given array.

Program to convert the list into an array using NumPy.

In this python numpy program, we will convert the list into an array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Convert the list into an array using np.asarray().
  3. Print the output.
				
					import numpy as np
x = np.asarray([[3,0],[6,4]])
print(x)
				
			

Output :

				
					[[3 0]
 [6 4]]
				
			

add a vector to each row of a matrix.

convert an array into a list.

Add a vector to each row of a matrix using NumPy.

In this python numpy program, we willl add a vector to each row of a matrix using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create a matrix and a vector using np.array().
  3. Use for loop to iterate over the rows of the matrix.
  4. During iteration add the vector to the row using indexing and some logic.
  5. Print the output.
				
					import numpy as np
x = np.array([[5, 3],
              [7, 4]])
print("Original matrix: \n",x)
vector = np.array([2,5])
print("Vector to be added: \n",vector)

for i in range(len(x)):
    x[i,:] = x[i,:]+vector
    
print("Matrix after adding a vector: \n",x)
				
			

Output :

				
					Original matrix: 
 [[5 3]
 [7 4]]
Vector to be added: 
 [2 5]
Matrix after adding a vector: 
 [[7 8]
 [9 9]]
				
			

calculate the inner/dot product of two vectors.

convert the list into an array.