<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/ops/numpy.py#L1341-L1396) |

Compute the weighted average along the specified axis.

#### View aliases


**Main aliases**

[`tf.keras.ops.numpy.average`](https://www.tensorflow.org/api_docs/python/tf/keras/ops/average)

<br />

    tf.keras.ops.average(
        x, axis=None, weights=None
    )

<br />

<br />

<br />

| ## Args ||
|---|---|
| `x` | Input tensor. |
| `axis` | Integer along which to average `x`. The default, `axis=None`, will average over all of the elements of the input tensor. If axis is negative it counts from the last to the first axis. |
| `weights` | Tensor of weights associated with the values in `x`. Each value in `x` contributes to the average according to its associated weight. The weights array can either be 1-D (in which case its length must be the size of a along the given axis) or of the same shape as `x`. If `weights=None` (default), then all data in `x` are assumed to have a weight equal to one. <br /> The 1-D calculation is: `avg = sum(a * weights) / sum(weights)`. The only constraint on weights is that `sum(weights)` must not be 0. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| Return the average along the specified axis. ||

<br />

#### Examples:

    data = keras.ops.arange(1, 5)
    data
    array([1, 2, 3, 4], dtype=int32)
    keras.ops.average(data)
    array(2.5, dtype=float32)
    keras.ops.average(
        keras.ops.arange(1, 11),
        weights=keras.ops.arange(10, 0, -1)
    )
    array(4., dtype=float32)

    data = keras.ops.arange(6).reshape((3, 2))
    data
    array([[0, 1],
           [2, 3],
           [4, 5]], dtype=int32)
    keras.ops.average(
        data,
        axis=1,
        weights=keras.ops.array([1./4, 3./4])
    )
    array([0.75, 2.75, 4.75], dtype=float32)
    keras.ops.average(
        data,
        weights=keras.ops.array([1./4, 3./4])
    )
    Traceback (most recent call last):

    ValueError: Axis must be specified when shapes of a and weights differ.