Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
numpy.random.rand() in Python
Next article icon

numpy.var() in Python

Last Updated : 03 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

numpy.var(arr, axis = None) : Compute the variance of the given 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 means variance along the column and axis = 1 means 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("\nvar of arr : ", np.var(arr, dtype = np.float32)) 
print("\nvar 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 of the flattened array 
print("\nvar of arr, axis = None : ", np.var(arr)) 
      
# var along the axis = 0 
print("\nvar of arr, axis = 0 : ", np.var(arr, axis = 0)) 
  
# var along the axis = 1 
print("\nvar of arr, axis = 1 : ", np.var(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]


Next Article
numpy.random.rand() in Python
author
mohit gupta_omg :)
Improve
Article Tags :
  • Python
  • Python numpy-Statistics Functions
  • Python-numpy
Practice Tags :
  • python

Similar Reads

  • Python | Numpy matrix.var()
    With the help of Numpy matrix.var() method, we can find the variance of a matrix by using the matrix.var() method. Syntax : matrix.var() Return : Return variance of a matrix Example #1 : In this example we can see that by using matrix.var() method we are able to find the variance of a given matrix.
    1 min read
  • numpy.std() in Python
    numpy.std() is a function provided by the NumPy library that calculates the standard deviation of an array or a set of values. Standard deviation is a measure of the amount of variation or dispersion of a set of values. [Tex]\text{Standard Deviation} = \sqrt{\text{mean} \left( (x - x.\text{mean}())^
    4 min read
  • numpy.random.rand() in Python
    This article provides an in-depth exploration of the `numpy.random.rand()` function in Python. It covers the function's syntax, and definition, and includes illustrative examples with detailed explanations for better understanding. numpy.random.rand() Function Syntax The numpy.random.rand() function
    3 min read
  • Python NumPy
    Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Besides its obvious scientific uses, Numpy can also be used as an efficient
    6 min read
  • numpy.array_str() in Python
    numpy.array_str()function is used to represent the data of an array as a string. The data in the array is returned as a single string. This function is similar to array_repr, the difference being that array_repr also returns information on the kind of array and its data type. Syntax : numpy.array_st
    2 min read
  • numpy.exp2() in Python
    numpy.exp2(array, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : This mathematical function helps user to calculate 2**x for all x being the array elements. Parameters : array : [array_like]Input array or object whose elements, we need to test. out : [ndarray, optional
    2 min read
  • numpy.array_repr() in Python
    numpy.array_repr()function is used to convert an array to a string. Syntax : numpy.array_repr(arr, max_line_width=None, precision=None, suppress_small=None) Parameters : arr : [array_like] Input array. max_line_width : [int, optional] The maximum number of columns the string should span. Newline cha
    2 min read
  • NumPy Array in Python
    NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C
    2 min read
  • numpy.index() in Python
    numpy.core.defchararray.index(arr, substring, start=0, end=None): Finds the lowest index of the sub-string in the specified range But if substring is not found, it raises ValueError. Parameters: arr : array-like or string to be searched. substring : substring to search for. start, end : [int, option
    1 min read
  • numpy.nanvar() in Python
    numpy.nanvar(arr, axis = None) : Compute the variance of the given data (array elements) along the specified axis(if any), while ignoring NaN values. 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 dist
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences