# numpy.mean() in Python

## numpy.mean() in Python

`numpy.mean(arr, axis = None)` : Compute the arithmetic mean (average) of the given data (array elements) along the specified axis.

Parameters :
arr : [array_like]input array.
axis : [int or tuples of int]axis along which we want to calculate the arithmetic mean. Otherwise, it will consider arr to be flattened(works on all
the axis). axis = 0 means along the column and axis = 1 means working along the row.
out : [ndarray, optional]Different array in which we want to place the result. The array must have the same dimensions as expected output.
dtype : [data-type, optional]Type we desire while computing mean.

Results : Arithmetic mean of the array (a scalar value if axis is none) or array with mean values along specified axis.

Code #1:

 `# Python Program illustrating ` `# numpy.mean() method ` `import` `numpy as np` `   ` `# 1D array ` `arr ``=` `[``20``, ``2``, ``7``, ``1``, ``34``]` ` ` `print``(``"arr : "``, arr) ` `print``(``"mean of arr : "``, np.mean(arr))` `  `

Output :

```arr :  [20, 2, 7, 1, 34]
mean of arr :  12.8
```

Code #2:

 `# Python Program illustrating ` `# numpy.mean() method   ` `import` `numpy as np` `   ` ` ` `# 2D array ` `arr ``=` `[[``14``, ``17``, ``12``, ``33``, ``44``],  ` `       ``[``15``, ``6``, ``27``, ``8``, ``19``], ` `       ``[``23``, ``2``, ``54``, ``1``, ``4``, ]] ` `   ` `# mean of the flattened array ` `print``(``"\nmean of arr, axis = None : "``, np.mean(arr)) ` `   ` `# mean along the axis = 0 ` `print``(``"\nmean of arr, axis = 0 : "``, np.mean(arr, axis ``=` `0``)) ` `  ` `# mean along the axis = 1 ` `print``(``"\nmean of arr, axis = 1 : "``, np.mean(arr, axis ``=` `1``))` ` ` `out_arr ``=` `np.arange(``3``)` `print``(``"\nout_arr : "``, out_arr) ` `print``(``"mean of arr, axis = 1 : "``, ` `      ``np.mean(arr, axis ``=` `1``, out ``=` `out_arr))`

Output :

```mean of arr, axis = None :  18.6

mean of arr, axis = 0 :  [17.33333333  8.33333333 31.         14.         22.33333333]

mean of arr, axis = 1 :  [24.  15.  16.8]

out_arr :  [0 1 2]
mean of arr, axis = 1 :  [24 15 16]
```

Last Updated on November 1, 2021 by admin

## Convert Python List to numpy ArraysConvert Python List to numpy Arrays

Convert Python List to numpy Arrays A list in Python is a linear data structure that can hold heterogeneous elements they do not require to be declared and are flexible to shrink and grow. On the other hand, an array

## Numpy size() function | PythonNumpy size() function | Python

Numpy size() function | Python In Python, numpy.size() function count the number of elements along a given axis. Syntax: numpy.size(arr, axis=None) Parameters: arr: [array_like] Input data. axis: [int, optional] Axis(x,y,z) along which the elements(rows or columns) are counted. By default, give the total

## Multiplication of two Matrices in Single line using Numpy in PythonMultiplication of two Matrices in Single line using Numpy in Python

Multiplication of two Matrices in Single line using Numpy in Python Matrix multiplication is an operation that takes two matrices as input and produces single matrix by multiplying rows of the first matrix to the column of the second matrix.In

## How to create a vector in Python using NumPyHow to create a vector in Python using NumPy

How to create a vector in Python using NumPy NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Numpy is

## Implement sigmoid function using NumpyImplement sigmoid function using Numpy

Implement sigmoid function using Numpy With the help of Sigmoid activation function, we are able to reduce the loss during the time of training because it eliminates the gradient problem in machine learning model while training. # Import matplotlib, numpy and math

## numpy.log() in Pythonnumpy.log() in Python

numpy.log() in Python The numpy.log() is a mathematical function that helps user to calculate Natural logarithm of x where x belongs to all the input array elements. Natural logarithm log is the inverse of the exp(), so that log(exp(x)) = x. The natural logarithm is log

## numpy.sum() in Pythonnumpy.sum() in Python

numpy.sum() in Python numpy.sum(arr, axis, dtype, out) : This function returns the sum of array elements over the specified axis. Parameters : arr : input array. axis : axis along which we want to calculate the sum value. Otherwise, it will consider arr to

## numpy.argmax() in Pythonnumpy.argmax() in Python

numpy.argmax() in Python The numpy.argmax() function returns indices of the max element of the array in a particular axis. Syntax :  numpy.argmax(array, axis = None, out = None) Parameters :  array : Input array to work on axis : [int, optional]Along a