Instagram
youtube
Facebook
Twitter

Mean in Numpy

Mean in Numpy

  • Mean in Statistics is referred to as the average.

  • Mean is a useful measurement to get the center of the dataset.

  • In Numpy we use built-in function np.mean() to calculate the average or mean of an array. 

Example:

import numpy as np

arr1 = np.array([2, 5, 8, 3, 4, 10, 15, 5])

arr2 = np.array([3, 17, 18,  9,  2, 14, 10])

arr3 = np.array([7, 5, 4, 3, 2, 7, 7])

arr1_avg = np.mean(arr1)

arr2_avg = np.mean(arr2)

arr3_avg = np.mean(arr3)

print(arr1_avg)

print(arr2_avg)

print(arr3_avg)

Output:


6.5
10.4285714286
5.0

Logical Operations

  • In Numpy we can use the mean to calculate the percentage of array elements to check certain properties.

  • The logical operation will check a specific condition and it evaluates it as True and assigns 1 or False and assigns 0 if it doesn't match.

  • After applying the logical operation the mean function will calculate the mean based on the elements which are True and divide them by the length of the array, resulting in the percentage of elements that satisfies the logical operations.

Example:

Here, in this example, we are demonstrating a dataset containing years and we are going to calculate the percentage of years greater than 2006.

import numpy as np

class_year = np.array([1967, 1949, 2004, 1997, 1953, 1950, 1958, 1974, 1987, 2006, 2013, 1978, 1951, 1998, 1996, 1952, 2005, 2007, 2003, 1955, 1963, 1978, 2001, 2012, 2014, 1948, 1970, 2011, 1962, 1966, 1978, 1988, 2006, 1971, 1994, 1978, 1977, 1960, 2008, 1965, 1990, 2011, 1962, 1995, 2004, 1991, 1952, 2013, 1983, 1955, 1957, 1947, 1994, 1978, 1957, 2016, 1969, 1996, 1958, 1994, 1958, 2008, 1988, 1977, 1991, 1997, 2009, 1976, 1999, 1975, 1949, 1985, 2001, 1952, 1953, 1949, 2015, 2006, 1996, 2015, 2009, 1949, 2004, 2010, 2011, 2001, 1998, 1967, 1994, 1966, 1994, 1986, 1963, 1954, 1963, 1987, 1992, 2008, 1979, 1987])

millennials = np.mean(class_year>=2006)

print(millennials) #prints 0.2

Mean of 2-D Arrays

  • In 2-D arrays, we can calculate the mean of a larger array as well as the interior values.

  • Here, we name the array and axis as parameters.

  • To calculate the mean of the interior values we use axis = 1.

  • To calculate the mean at columns we use axis = 0.

Example:

import numpy as np

arr = np.array([[1, 0, 0], [0, 0, 1], [1, 0, 1]])

print(np.mean(arr)) #prints 0.444

print(np.mean(arr, axis = 1)) #prints array([ 0.33333333,  0.33333333,  0.66666667])

print(np.mean(arr, axis = 0)) #prints [ 0.66666667,  0,  0.66666667]