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.

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.