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.

Program to multiply two vectors using NumPy.

In this python numpy program, we will multiply two vectors using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create two arrays using np.array().
  3. Multiply two arrays using .
  4. Print the output.
				
					import numpy as np
x = np.array([4,2,8])
y = np.array([7,3,5])

print(x*y)
				
			

Output :

				
					[28  6 40]
				
			

create a vector with values from 1 to 15 and change the sign of the numbers in the range from 6 to 10.

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

Program to create a vector with values and change the sign of the numbers.

In this python numpy program, we will create a vector with values and change the sign of the numbers

Steps to solve the program
  1. Import the numpy library as np.
  2. Create a vector with values from 1-15 using np.arange().
  3. Change the sign of the numbers from 6-10 using logic and indexing.

  4. Print the output.
				
					import numpy as np
x = np.arange(1,16)
x[(x >= 6) & (x <= 10)] *= -1

print(x)
				
			

Output :

				
					[  1   2   3   4   5  -6  -7  -8  -9 -10  11  12  13  14  15]
				
			

create a 2×2 matrix.

multiply two vectors.