|
| 1 | +--- |
| 2 | +Title: 'mean()' |
| 3 | +Description: 'Computes the arithmetic mean of array elements along a given axis.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Array' |
| 9 | + - 'Data' |
| 10 | + - 'NumPy' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-python-3' |
| 13 | + - 'paths/data-science' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`mean()`** method of a NumPy `ndarray` computes the arithmetic average of the array elements. The calculation can be performed across the entire array or along a specified axis. The result is either a scalar value or an array of means, depending on the chosen axis. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +ndarray.mean(axis=None, dtype=None, out=None, keepdims=False, initial=<no value>, where=True) |
| 22 | +``` |
| 23 | + |
| 24 | +**Parameters:** |
| 25 | + |
| 26 | +- `axis` (optional): The axis or axes along which the mean is computed. If omitted, the mean of all elements is returned. |
| 27 | +- `dtype` (optional): Data type used during the calculation. |
| 28 | +- `out` (optional): Output array for storing the result. |
| 29 | +- `keepdims` (optional): Preserves reduced dimensions when set to `True`. |
| 30 | +- `initial` (optional): Starting value for the sum. |
| 31 | +- `where` (optional): A boolean mask that selects elements included in the mean. |
| 32 | + |
| 33 | +**Return value:** |
| 34 | + |
| 35 | +Returns a scalar or `ndarray` containing the computed arithmetic mean. |
| 36 | + |
| 37 | +## Example 1 |
| 38 | + |
| 39 | +In this example, the mean is computed across both axes: once for the entire array and once along each individual row: |
| 40 | + |
| 41 | +```py |
| 42 | +import numpy as np |
| 43 | + |
| 44 | +arr = np.array([ |
| 45 | + [2, 4, 6], |
| 46 | + [8, 10, 12] |
| 47 | +]) |
| 48 | + |
| 49 | +# Mean of all elements |
| 50 | +overall_mean = arr.mean() |
| 51 | + |
| 52 | +# Mean along each row |
| 53 | +row_mean = arr.mean(axis=1) |
| 54 | + |
| 55 | +print("Overall mean:", overall_mean) |
| 56 | +print("Row-wise mean:", row_mean) |
| 57 | +``` |
| 58 | + |
| 59 | +The output of this code is: |
| 60 | + |
| 61 | +```shell |
| 62 | +Overall mean: 7.0 |
| 63 | +Row-wise mean: [ 4. 10.] |
| 64 | +``` |
| 65 | + |
| 66 | +## Example 2 |
| 67 | + |
| 68 | +In this example, a boolean mask is used with where to compute the mean only across selected elements: |
| 69 | + |
| 70 | +```py |
| 71 | +import numpy as np |
| 72 | + |
| 73 | +arr = np.array([10, 20, 0, 40, 0]) |
| 74 | +mask = arr > 0 # Select only non-zero values |
| 75 | + |
| 76 | +masked_mean = arr.mean(where=mask) |
| 77 | + |
| 78 | +print("Mean of non-zero values:", masked_mean) |
| 79 | +``` |
| 80 | + |
| 81 | +The output of this code is: |
| 82 | + |
| 83 | +```shell |
| 84 | +Mean of non-zero values: 23.333333333333332 |
| 85 | +``` |
| 86 | + |
| 87 | +## Codebyte Example |
| 88 | + |
| 89 | +Use this codebyte to compute the mean along a specific axis in a 2D array: |
| 90 | + |
| 91 | +```codebyte/python |
| 92 | +import numpy as np |
| 93 | +
|
| 94 | +scores = np.array([ |
| 95 | + [80, 90, 85], |
| 96 | + [70, 75, 78], |
| 97 | + [88, 92, 95] |
| 98 | +]) |
| 99 | +
|
| 100 | +# Compute mean of each column |
| 101 | +column_mean = scores.mean(axis=0) |
| 102 | +
|
| 103 | +print("Column-wise mean:", column_mean) |
| 104 | +``` |
| 105 | + |
| 106 | +## Frequently Asked Questions |
| 107 | + |
| 108 | +### 1. What is a NumPy ndarray? |
| 109 | + |
| 110 | +A NumPy `ndarray` is a multidimensional, fixed-size array optimized for numerical computation. It stores elements of the same data type and supports fast vectorized operations, making it the core data structure of NumPy. |
| 111 | + |
| 112 | +### 2. What is NumPy `mean()`? |
| 113 | + |
| 114 | +The `mean()` function computes the arithmetic average of the selected array elements. It supports axes, masks, and type casting, making it suitable for both simple and high-performance statistical calculations. |
| 115 | + |
| 116 | +### 3. How does `mean()` work in Python? |
| 117 | + |
| 118 | +The `mean()` method sums the selected elements of the array and divides by the number of included elements. When an axis is specified, this process is applied along that dimension, returning an array of means. |
0 commit comments