Change language

numpy.var () in Python

|

numpy.var (arr, axis = None) : calculate the variance of the data (array elements) along the specified axis (if any).

Example:

x = 1 1 1 1 1
Standard Deviation = 0. Variance = 0

y = 9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4

Step 1: Mean of distribution 4 = 7
Step 2: Summation of (x - x. mean ()) * * 2 = 178
Step 3: Finding Mean = 178/20 = 8.9
This Result is Variance.

Parameters :

arr: [array_like] input array.
axis: [int or tuples of int] axis along which we want to calculate the variance. Otherwise, it will consider arr to be flattened (works on all the axis). axis = 0 mean s variance along the column and axis = 1 mean s variance along the row.
out: [ndarray, optional] Different array in which we want to place the result. The array must have the same dimensions as expected output.
dtype: [data-type, optional] Type we desire while computing variance.

Results: Variance of the array (a scalar value if axis is none) or array with variance values ​​along specified axis.

Code # 1:

# Python program illustrating
# numpy.var () method

import numpy as np 

 
# 1D array

arr = [ 20 , 2 , 7 , 1 , 34

 

print ( "arr:" , arr) 

print ( "var of arr:" , np.var (arr)) 

  

print ( "var of arr:" , np.var (arr , dtype = np.float32)) 

print ( "var of arr:" , np.var ( arr, dtype = np.float64)) 

Output:

 arr: [20, 2, 7, 1, 34] var of arr: 158.16 var of arr: 158.16 var of arr: 158.16 

Code # 2:

# Python program illustrating
# numpy.var () method

import numpy as np 

 
# 2D array

arr = [[ 2 , 2 , 2 , 2 , 2 ], 

  [ 15 , 6 , 27 , 8 , 2 ], 

[ 23 , 2 , 54 , 1 , 2 ,], 

[ 11 , 44 , 34 , 7 , 2 ]] 

 

 
# var flattened array

print ( "var of arr, axis = None:" , np.var (arr)) 

  
# var along the axis = 0

print ( "var of arr, axis = 0: " , np.var (arr, axis = 0 )) 

  
# var along axis = 1

print ( "var of arr, axis = 1:" , np.va r (arr, axis = 1 )) 

Output:

 var of arr, axis = None: 236.14000000000004 var of arr, axis = 0: [57.1875 312.75 345.6875 9.25 0.] var of arr, axis = 1: [0. 77.04 421.84 269.04] 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method