Calculate the variance of an array using NumPy

In this python numpy program, we will calculate the variance of an array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Calculate the variance of the array using np.var().
  4. Print the output.
				
					import numpy as np
age = np.array([20,25,21,23,26,27,26,30,24,26]) 

print("Variance: ",np.var(age))
				
			

Output :

				
					Variance:  7.76
				
			

calculate the standard deviation of an array.

calculate the cos angle of an array.

Calculate the standard deviation of an array using NumPy

In this python numpy program, we will calculate the standard deviation of an array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Calculate the standard deviation of the array using np.std().
  4. Print the output.
				
					import numpy as np
age = np.array([20,25,21,23,26,27,26,30,24,26]) 

print("Standard Deviation: ",np.std(age))
				
			

Output :

				
					Standard Deviation:  2.7856776554368237
				
			

calculate the sin angle of an array.

calculate the variance of an array.

Calculate the sin angle of an array using NumPy

In this python numpy program, we will calculate the sin angle of an array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Calculate the sin angle of the array using np.sin().
  4. Print the output.
				
					import numpy as np
angle = np.array([45,13])

print(f"Sin of {angle}: ",np.sin(angle))
				
			

Output :

				
					Sin of [45 13]:  [0.85090352 0.42016704]
				
			

check whether the dimensions of two arrays are the same or not.

calculate the standard deviation of an array.

Program to check whether the dimensions of two arrays are the same or not

In this python numpy program, we will check whether the dimensions of two arrays are the same or not.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create two arrays using np.array().
  3. Use an if-else statement with shape to check whether the dimensions of two arrays are the same or not.
  4. Print the output.
				
					import numpy as np
x = np.arange(15).reshape(3,5)
y = np.arange(20).reshape(4,5)

if x.shape == y.shape:
    print("Same dimensions")
else:
    print("Different dimensions")
				
			

Output :

				
					Different dimensions
				
			

Create an array and reshape it.

calculate the sin angle of an array.

Program to create a NumPy array and reshape it

In this python numpy program, we will create a NumPy array and reshape it.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Reshape the given array to 5 rows and 6 columns using reshape.
  4. Print the output.
				
					import numpy as np
x = np.arange(30)
y = x.reshape(5,6)

print("Array: \n",y)
				
			

Output :

				
					Array: 
 [[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]
 [24 25 26 27 28 29]]
				
			

convert an array to a data frame with headers.

check whether the dimensions of two arrays are the same or not.

Convert a NumPy array to a data frame with headers

In this python numpy program, we will convert a NumPy array to a data frame with headers.

Steps to solve the program
  1. Import the numpy library as np.
  2. Import pandas library as pd.
  3. Create an array using np.array().
  4. Convert an array to a data-frame with headers using pd.DataFrame() and set columns=[‘A’,’B’,’C’].
  5. Print the output.
				
					import numpy as np
import pandas as pd
x = np.random.rand(6,3)

df = pd.DataFrame(x,columns=['A','B','C'])
print("\nPandas DataFrame: ")
print(df)
				
			

Output :

				
					Pandas DataFrame: 
          A         B         C
0  0.307619  0.526206  0.982625
1  0.865438  0.248926  0.248677
2  0.080811  0.619051  0.774105
3  0.716698  0.419266  0.181618
4  0.223379  0.055573  0.168672
5  0.106160  0.929609  0.886170
				
			

create an array and reshape it.

Program to merge two given NumPy arrays of identical shape

In this python numpy program, we will merge two given NumPy arrays of identical shape.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create two identical arrays using np.array().
  3. Merge two given NumPy arrays using np.concatenate().
  4. Print the output.
				
					import numpy as np
x = np.array([1,2,3])
y = np.array([4,5,6])

print("Original arrays:")
print(x)
print(y)

output = np.concatenate((x, y))
print("\nAfter concatenate:")
print(output) 
				
			

Output :

				
					Original arrays:
[1 2 3]
[4 5 6]

After concatenate:
[1 2 3 4 5 6]
				
			

calculate averages without NaNs along a row.

Calculate averages without NaNs along a row of an array

In this python numpy program, we will calculate averages without NaNs along a row of an array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. First, remove the NaN values using np.ma.masked_array()
  4. Calculate averages without NaN values along the rows using np.average() and set the axis equal to 1.
  5. Print the output.
				
					import numpy as np
x = np.array([[6, 5, np.nan],[np.nan, 9, 1],[7, 8, 2]])

temp = np.ma.masked_array(x,np.isnan(x))
print("Array after removing nan values: \n",temp)
print("\nAverages without NaNs along the said array:")
print(np.average(temp,axis=1))
				
			

Output :

				
					Array after removing nan values: 
 [[6.0 5.0 --]
 [-- 9.0 1.0]
 [7.0 8.0 2.0]]

Averages without NaNs along the said array:
[5.5 5.0 5.666666666666667]
				
			

calculate the sum of all columns of an array

merge two given arrays of identical shape.

Program to calculate the sum of all columns of the NumPy array

In this python numpy program, we will calculate the sum of all columns of the NumPy array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Calculate the sum of all columns of the given array using sum() and set axis equal to 0.
  4. Print the output.
				
					import numpy as np
x = np.array([[6, 5, 7],[10, 9, 1],[7, 8, 2]])

print("Sum of all columns: ",x.sum(axis=0))
				
			

Output :

				
					Sum of all columns:  [23 22 10]
				
			

copy data from a given array to another array.

calculate averages without NaNs along a row.

Program to copy data from a given array to another array

In this python numpy program, we will copy data from a given array to another array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Create an empty array of the same size as the given array.
  4. Use for loop to iterate over the given array and add all elements to the empty array.
  5. Print both arrays to see the output.
				
					import numpy as np
x = np.array([[6, 5, 7],[10, 9, 1],[7, 8, 2]])
y = [None] * len(x)
print(y)

for i in range(0,len(x)):
    y[i] = x[i]
    
print("Elements of original array: ");    
for i in range(0, len(x)):    
    print(x[i])    
     
print("Elements of new array: ");    
for i in range(0, len(y)):    
     print(y[i])  
				
			

Output :

				
					[None, None, None]
Elements of original array: 
[6 5 7]
[10  9  1]
[7 8 2]
Elements of new array: 
[6 5 7]
[10  9  1]
[7 8 2]
				
			

calculate the sum of all columns of an array