First of all the main title of this article gives us all a clever idea of what is the use of this function. Some of the persons who are in the statistics background know theÂ averageÂ term very well. Knowingly or unknowingly theÂ Data ScienceÂ andÂ AIÂ practitioners are using this term in theÂ preprocessingÂ methods. Let us know further in more detail.Â

## Introduction

In general statistics,Â the averageÂ is known as the value of all numbers divided by their total numbers. The main work behind this helps us understand the core value that lies within the dataset.Â

**Take an example for this:** We have six different values that slightly differ from each other. They are the salaries of employees of some companies. The task is to find the optimum salary that each person earns per year.

**Solution: **Let us say we have the **LPAs **as:

- 400000
- 450000
- 500000
- 470000
- 460000
- 500000

Then we take the average for this. Below is the formula for this:

So, we calculate the mean as follows:

mean = **(400000 + 450000 + 500000 + 470000 + 460000 + 500000)/6**

The answer comes out to be: 463333.3334 Lakh Rupees Per Annum. This is the average salary each person earns in one year.

## Basic Concepts

From the above example, we came to know about the main advantage. The optimum value is necessary for the calculation of various parameters. There are various applications of mean in the real world.

- Predicting the average income of a state.
- Deciding the best selling price of the goods in the market.
- Normalization of scores in the test scores involves mean calculation.

The values differ a a lot there are various modifications of this term:

**Arithmetic means: In use for the statistics to analyze the tabular data.****Regular mean/average: Mostly in use for the Common mathematical operations.**

Now, we will be using the second form.

## Implementing The Average Function In Python

There are two methods for this.

**Create one average() function for our task.****Use the numpy library’s built in numpy.****average()****function.**

### Create an average() function for our task

The creation of this function is very simple and straightforward. But, we need to take note of one point that in which format should we use the input. So an array will be the correct format for this. Because in an array we can store values in varying counts.

**Code:**

```
# function that takes the average of the elements
def average(value):
avg = sum(value)/len(value)
print('The average is: ', abs(avg))
# main method for the user input
def main():
'For the input arguments'
li = [] # declaring an empty list
a = int(input('Enter the number of values for the array: '))
for i in range(a):
li.append(int(input('Enter the {0} element: '.format(i+1))))
print('The list is: ', li, '\n')
average(li)
main()
```

**Output:**

**Explanation:**

- Declare a function namely average(). Then give the necessary formula of
**mean**inside it. The division of the sum of all values and the sum of several values. - Then the main function has the core working. It takes the array as the input from the user. Then, it takes the number of inputs and prints the array.
- Then we call the average function inside the main function and it takes the same array as an input parameter. Then we get the average of all the elements from it.

### Using the numpy library for the task

The Numpy library’s average() function makes our task a bit easier. We all know that this API is one of the famous libraries for array operations. Several built-in methods reduce our code and make some things easier for the implementation. Its type is NumPy ndarray. We will first declare some of the core steps before we start the use of **the average()** function.

- Import the NumPy module as np.
- Declare an array through np.array() method.
- Then call the average() function and give the array as an input parameter inside it.
- Store all this inside a variable and print the result on the screen.

**Code for 1D array:**

```
import numpy as np
array = np.array([22, 12, 3, -1, -3, 98])
a = np.average(array)
print('The average of the elements in the array is: ', abs(round(a, 3)))
# output:
# The average of the elements in the array is: 21.833
```

**Explanation:**

- Declare an array inside the array() method of the NumPy module.
- Then declare a variable that stores the value of average. Then call the average() function and give the same array inside it as a parameter.
- We use the
**abs()**function to get an absolute value. Then we round it off with up to three decimal places using**the round()**function. - Print the value on the screen.

**Code for 3D arrays:**

```
import numpy as np
array = np.array([[22, 12, 3],[-1, -3, 98], [3, 44, 0]])
a = np.average(array)
print('The average of the elements in the array is: ', abs(round(a, 3)))
# Output:
# The average of the elements in the array is: 19.778
```

We have a 3D array to check whether the function is useful for those arrays. Solving this using pen and paper is a very tedious task. We have got the required result.

## Conclusion

Concluding this topic is not so easy as there are a lot more things to come further. The topic is an interesting one and one can get the core knowledge of various functions Numpy library offers for special mathematical operations. So please view again the code implementations for a better understanding.