---
title: average()
url: https://docs.vultr.com/python/third-party/numpy/average
description: Compute Mean Value
publish_date: 2024-11-11T15:20:00.081178Z
last_updated: 2024-11-11T15:20:00.081178Z
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/collection-items/742/412c3892-bec6-4dea-b524-5a5acd73a7bc.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Python Numpy average() - Compute Mean Value

## Introduction

The `average()` function from the NumPy library is essential for computing the mean value of data elements in an array or along a specific axis of a multidimensional array. This function not only simplifies the calculation of averages but also allows for weighting of data points, providing a more nuanced statistical analysis.

In this article, you will learn how to harness the power of the `average()` function in various scenarios like handling single-dimensional arrays, multi-dimensional arrays, and using weights to influence the average calculation. Exploiting these functionalities can optimize and enhance data analysis tasks in your Python projects.

## Calculating Average in Single-Dimensional Arrays

### Simple Average Calculation

1. Import the NumPy library.
1. Create a single-dimensional array.
1. Compute the average using the `average()` function.

    ```python
    import numpy as np
    data = np.array([10, 20, 30, 40, 50])
    mean_value = np.average(data)
    print(mean_value)
    ```

    This script calculates the average of the values in the `data` array. The output will be the mean of these numbers.

### Average with Weights

1. Define weights for each element in the array.
1. Compute the weighted average using the `average()` function.

    ```python
    import numpy as np
    data = np.array([10, 20, 30, 40, 50])
    weights = np.array([1, 2, 3, 4, 5])
    weighted_mean = np.average(data, weights=weights)
    print(weighted_mean)
    ```

    The `weights` array makes the function consider some elements more heavily than others in the calculation, thus affecting the final average.

## Utilizing average() in Multi-Dimensional Arrays

### Average Along a Specific Axis

1. Create a multi-dimensional array.
1. Compute the average across a defined axis using the `average()` function.

    ```python
    import numpy as np
    data = np.array([[10, 20, 30], [40, 50, 60]])
    mean_value_axis0 = np.average(data, axis=0)
    mean_value_axis1 = np.average(data, axis=1)
    print("Average Along Axis 0:", mean_value_axis0)
    print("Average Along Axis 1:", mean_value_axis1)
    ```

    Setting `axis=0` calculates the average across the first dimension (columns), and `axis=1` calculates it across the second dimension (rows).

## Conclusion

The `average()` function in NumPy is a versatile tool for computing mean values, indispensable in data processing and analysis. By using this function, you can easily calculate simple or weighted averages for single-dimensional data, as well as perform more complex average calculations across various dimensions of multi-dimensional arrays. Incorporating these practices will significantly improve the efficiency of your data analysis workflows, thereby enhancing the robustness and accuracy of your results.