Python Program for Numpy average() Function

Those people with a statistical background are highly familiar with the phrase “average.” Data Science and AI practitioners, whether deliberately or unknowingly, use this word in preprocessing procedures.

Numpy average() Function:

In general statistics, the average is defined as the sum of all numbers divided by the sum of their totals. The major purpose of this effort is to assist us to grasp the fundamental value contained inside the dataset.

For Example: 

Let us consider the marks obtained by the 5 students out of 100.

s1 = 80,

s2 = 95

s3 = 35

s4 = 65

s5 = 70

The average of their marks = (80+95+35+65+70)/5 = 345/5 = 69.

Formula:

Average = sum of all Frequencies/ No. of Frequencies

Where Frequencies = values given in the dataset

We learned about the key benefit from the preceding example. The best value is required for the calculation of many parameters. In the actual world, the mean can be used in a variety of contexts.

  • Predicting a state’s average income.
  • Choosing the best market selling price for the items.
  • Normalization of test scores entails calculating the mean.

The values vary greatly, and there are several variations of this term:

Arithmetic mean: In statistics, it is used to examine tabular data.
Regular mean/average: This is a term that is commonly used in common mathematical operations.

We shall now use the second form.

Examples:

Example1:

Input:

Given List = [20, 30, 50, 10, 70]

Output:

The Average of all the list items = 36.0

Example2:

Input:

Given Array = [[10, 8, 4],[4, 15, 65], [7, 16, 2]]

Output:

The given Array average = 14.555555555555555

Program for Numpy average() Function in Python

 

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the sum of all the elements of the given list using the sum() function and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Divide the above-obtained list sum by the length of the given list to get the average of the list items.
  • Store it in another variable.
  • Print the average of all the list items.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [20, 30, 50, 10, 70]
# Calculate the sum of all the elements of the given list using the sum()
# function and store it in a variable.
lst_sum = sum(gvn_lst)
# Calculate the length of the given list using the len() function and
# store it in another variable.
lst_len = len(gvn_lst)
# Divide the above-obtained list sum by the length of the given list to get the
# average of the list items.
# Store it in another variable.
avrge = lst_sum/lst_len
# Print the average of all the list items.
print("The Average of all the list items = ", avrge)

Output:

The Average of all the list items =  36.0

Using Numpy Library:

The average() function in the Numpy module simplifies our task. We are all aware that this API is one of the most well-known libraries for array operations. Several built-in techniques help to minimize our code and make some things easier to implement. It has the NumPy ndarray type.

Approach:

  • Import the numpy library as np using the import keyword.
  • Give the list as static input and store it in a variable.
  • Create an array using the np.array() method by passing the given list as an argument and store it in another variable.
  • Calculate the average of the above array using the np.average()  method and store it in another variable.
  • Print the average of the given array.
  • The Exit of the Program.

Below is the implementation:

# Import the numpy library as np using the import function.
import numpy as np
# Give the list as static input and store it in a variable.
gvn_lst = [22, 12, 3, -1, -3, 98]
# Create an array using the np.array() method by passing the given list as an
# argument and store it in another variable.
rslt_arry = np.array(gvn_lst)
# Calculate the average of the above array using the np.average()  method and
# store it in another variable.
averg = np.average(rslt_arry)
# Print the average of the given array.
print("The given Array average =  ", averg)

Output:

The given Array average = 7.0

For 3 -Dimensional Array

# Import the numpy library as np using the import function.
import numpy as np
# Give the 3-Dimensional list as static input and store it in a variable.
gvn_list = [[10, 8, 4],[4, 15, 65], [7, 16, 2]]
# Create an array using the np.array() method by passing the given list as an
# argument and store it in another variable.
rslt_arry = np.array(gvn_list)
# Calculate the average of the above array using the np.average()  method and
# store it in another variable.
averg = np.average(rslt_arry)
# Print the average of the given array.
print("The given Array average =  ", averg)

Output:

The given Array average = 14.555555555555555

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the sum of all the elements of the given list using the sum() function and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Divide the above-obtained list sum by the length of the given list to get the average of the list items.
  • Store it in another variable.
  • Print the average of all the list items.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(float, input(
   'Enter some random List Elements separated by spaces = ').split()))

# Calculate the sum of all the elements of the given list using the sum()
# function and store it in a variable.
lst_sum = sum(gvn_lst)
# Calculate the length of the given list using the len() function and
# store it in another variable.
lst_len = len(gvn_lst)
# Divide the above-obtained list sum by the length of the given list to get the
# average of the list items.
# Store it in another variable.
avrge = lst_sum/lst_len
# Print the average of all the list items.
print("The Average of all the list items = ", avrge)

Output:

Enter some random List Elements separated by spaces = 32 50 15 44 65
The Average of all the list items = 41.2

Using Numpy Library:

Approach:

  • Import the numpy library as np using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Create an array using the np.array() method by passing the given list as an argument and store it in another variable.
  • Calculate the average of the above array using the np.average()  method and store it in another variable.
  • Print the average of the given array.
  • The Exit of the Program.

Below is the implementation:

# Import the numpy library as np using the import function.
import numpy as np
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(float, input(
   'Enter some random List Elements separated by spaces = ').split()))

# Create an array using the np.array() method by passing the given list as an
# argument and store it in another variable.
rslt_arry = np.array(gvn_lst)
# Calculate the average of the above array using the np.average()  method and
# store it in another variable.
averg = np.average(rslt_arry)
# Print the average of the given array.
print("The given Array average =  ", averg)

Output:

Enter some random List Elements separated by spaces = 32.5 47 85.3 100 50
The given Array average = 62.96