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.

Program to create a 2×2 matrix using NumPy.

In this python numpy program, we will create a 2×2 matrix using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array of 4 elements using np.array() because 2×2 matrix will have 4 elements.
  3. Now convert the array to create a 2×2 matrix using reshape.
  4. Print the output.
				
					import numpy as np
x = np.arange(4)

x.reshape(2,2)
				
			

Output :

				
					array([[0, 1],
       [2, 3]])
				
			

create a 2×3 matrix.

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

Program to create a 2×3 matrix using NumPy.

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

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array of 6 elements using np.array() because 2×3 matrix will have 6 elements.
  3. Now convert the array to create a 2×3 matrix using reshape.
  4. Print the output.
				
					import numpy as np
x = np.arange(6)

x.reshape(2,3)
				
			

Output :

				
					array([[0, 1, 2],
       [3, 4, 5]])
				
			

create a 3×4 matrix.

create a 2×2 matrix

Program to create a 3×4 matrix using NumPy.

In this python numpy program, we will create a 3×4 matrix using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array of 12 elements using np.array() because 3×4 matrix will have 12 elements.
  3. Now convert the array to create a 3×4 matrix using reshape.
  4. Print the output.
				
					import numpy as np
x = np.arange(1,13)

x.reshape(3,4)
				
			

Output :

				
					array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
				
			

print all the values from the above array except 1.

create a 2×3 matrix.

Python NumPy program to print all the values from the array.

In this python numpy program, we will print all the values from the array except 20 using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array of 1-20 numbers using np.range().
  3. Use for loop to iterate over the numbers in the array.
  4. If the number is not equal to 1 then print that number.
				
					import numpy as np
x = np.arange(1,20)

for val in x:
    if val != 1:
        print(val,end=" ")
				
			

Output :

				
					2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
				
			

print all the values from the array except 20 using NumPy.

create a 3×4 matrix.

Python NumPy program to print all the values from the array.

In this python numpy program, we will print all the values from the array except 20 using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array of 1-20 numbers using np.range().
  3. Use for loop to iterate over the numbers in the array.
  4. If the number is not equal to 20 then print that number.
				
					import numpy as np
x = np.arange(1,20)

for val in x:
    if val != 20:
        print(val,end=" ")
				
			

Output :

				
					1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
				
			

create a vector with values ranging from 1 to 20.

print all the values from the above array except 1 using NumPy.

Create a vector with values ranging from 1 to 20 using NumPy.

In this python numpy program, we will create a vector with values ranging from 1 to 20 using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create a vector with values ranging from 1 to 20 using np.arange().
  3. Print the output.
				
					import numpy as np
x = np.arange(1,21)

print("Array: ",x)
				
			

Output :

				
					Array:  [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]
				
			

generate an array of 5 random numbers from a standard normal distribution.

print all the values from the array except 20 using NumPy.

generate an array of 5 random numbers from a standard normal distribution using NumPy.

In this python numpy program, we will generate an array of 5 random numbers from a standard normal distribution using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Generate an array of 5 random numbers from a standard normal distribution using np.random.normal(size=5).
  3. Print the output.
				
					import numpy as np
x = np.random.normal(size=5)
print(x)
				
			

Output :

				
					[ 0.07450327 -1.26415493  0.61604056  0.61595139 -1.07081685]
				
			

generate random numbers between 1-2.

create a vector with values ranging from 1 to 20.

Generate random numbers between 1-2 using NumPy.

In this python numpy program, we will generate random numbers between 1-2 using NumPy.

Steps to solve the program
  1. From the numpy library import random.
  2. Generate random numbers between 1-2 using random.rand().
  3. Use for loop with the range function to generate 5 numbers.
  4. Print the output.
				
					from numpy import random
for i in range(5):
    rand_num = random.rand()
    print(rand_num)
				
			

Output :

				
					0.9108111865486636
0.6770944961821627
0.4528420505050911
0.7668031795361783
0.4633483035604695
				
			

create a 3×3 identify matrix.

generate an array of 5 random numbers from a standard normal distribution.