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.

Calculate the inner/dot product of two vectors using NumPy.

In this python numpy program, we will calculate the inner/dot product of two vectors using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create two vectors using np.array().
  3. Calculate the inner/dot product of two vectors using np.dot().
  4. Print the output.
				
					import numpy as np
x = np.array([5, 3])
y = np.array([7,4])

print("Inner-dot product: ",np.dot(x,y))
				
			

Output :

				
					Inner-dot product:  47
				
			

calculate the sum of each column in an array.

add a vector to each row of a matrix.

Calculate the sum of each column in an array using NumPy.

In this python numpy program, we will calculate the sum of each column 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. Use np.sum() and set the axis equal to 0 to calculate the sum of each column in an array.
  4. Print the output.
				
					import numpy as np
x = np.array([[5, 3],
              [7, 4]])
print("Sum of each column : ",np.sum(x,axis=0))
				
			

Output :

				
					Sum of each column :  [12  7]
				
			

calculate the sum of each row in an array.

calculate the inner/dot product of two vectors.

Calculate the sum of each row in an array using NumPy.

In this python numpy program, we will calculate the sum of each row 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. Use np.sum() and set the axis equal to 1 to calculate the sum of each row in an array.
  4. Print the output.
				
					import numpy as np
x = np.array([[5, 3],
              [7, 4]])
print("Sum of each row : ",np.sum(x,axis=1))
				
			

Output :

				
					Sum of each row :  [ 8 11]
				
			

calculate the sum of all elements in an array.

calculate the sum of each column in an array

Calculate the sum of all elements in an array using NumPy.

In this python numpy program, we will calculate the sum of all elements in an array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Calculate the sum of all elements in an array using np.sum().
  3. Print the output.
				
					import numpy as np
x = np.array([[5, 3],
              [7, 4]])
print("Sum of all the elements : ",np.sum(x))
				
			

Output :

				
					Sum of all the elements :  19
				
			

create a 2×2 zero matrix with elements on the main diagonal equal to 7,8.

calculate the sum of each row in an array.

Create a 2×2 zero matrix with elements on the main diagonal equal to numbers

In this python numpy program, we will create a 2×2 zero matrix with elements on the main diagonal equal to numbers.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create a 2×2 zero matrix with elements on the main diagonal using np.diag().
  3. Print the output.
				
					import numpy as np
x = np.diag([7,8])
print(x)
				
			

Output :

				
					[[7 0]
 [0 8]]
				
			

reshape the 2×3 matrix into a 2×2 matrix.

calculate the sum of all elements in an array.

Reshape the 2×3 matrix into a 2×2 matrix using NumPy.

In this python numpy program, we will reshape the 2×3 matrix into a 2×2 matrix using NumPy.

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

Output :

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

find the shape of the given matrix.

create a 2×2 zero matrix with elements on the main diagonal equal to 7,8.

Program to find the shape of the given matrix using NumPy.

In this python numpy program, we will find the shape of the given matrix using NumPy.

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

print("Shape of the matrix: ",x.shape)
				
			

Output :

				
					Shape of the matrix:  (2, 3)
				
			

create a 2×3 matrix filled with values from 5-10.

reshape the 2×3 matrix into a 2×2 matrix.

Create a 2×3 matrix filled with values from 5-10 using NumPy.

In this python numpy program, we will create a 2×3 matrix filled with values from 5-10 using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array filled with values from 5-10 using np.arange().
  3. Now convert the array to create a 2×3 matrix using reshape.
  4. Print the output.
				
					import numpy as np
x = np.arange(5,11)

x.reshape(2,3)
				
			

Output :

				
					array([[ 5,  6,  7],
       [ 8,  9, 10]])
				
			

multiply two vectors.

find the shape of the given matrix.