Как найти среднее арифметическое матрицы питон

numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<no value>, *, where=<no value>)[source]#

Compute the arithmetic mean along the specified axis.

Returns the average of the array elements. The average is taken over
the flattened array by default, otherwise over the specified axis.
float64 intermediate and return values are used for integer inputs.

Parameters:
aarray_like

Array containing numbers whose mean is desired. If a is not an
array, a conversion is attempted.

axisNone or int or tuple of ints, optional

Axis or axes along which the means are computed. The default is to
compute the mean of the flattened array.

New in version 1.7.0.

If this is a tuple of ints, a mean is performed over multiple axes,
instead of a single axis or all the axes as before.

dtypedata-type, optional

Type to use in computing the mean. For integer inputs, the default
is float64; for floating point inputs, it is the same as the
input dtype.

outndarray, optional

Alternate output array in which to place the result. The default
is None; if provided, it must have the same shape as the
expected output, but the type will be cast if necessary.
See Output type determination for more details.

keepdimsbool, optional

If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the input array.

If the default value is passed, then keepdims will not be
passed through to the mean method of sub-classes of
ndarray, however any non-default value will be. If the
sub-class’ method does not implement keepdims any
exceptions will be raised.

wherearray_like of bool, optional

Elements to include in the mean. See reduce for details.

New in version 1.20.0.

Returns:
mndarray, see dtype parameter above

If out=None, returns a new array containing the mean values,
otherwise a reference to the output array is returned.

Notes

The arithmetic mean is the sum of the elements along the axis divided
by the number of elements.

Note that for floating-point input, the mean is computed using the
same precision the input has. Depending on the input data, this can
cause the results to be inaccurate, especially for float32 (see
example below). Specifying a higher-precision accumulator using the
dtype keyword can alleviate this issue.

By default, float16 results are computed using float32 intermediates
for extra precision.

Examples

>>> a = np.array([[1, 2], [3, 4]])
>>> np.mean(a)
2.5
>>> np.mean(a, axis=0)
array([2., 3.])
>>> np.mean(a, axis=1)
array([1.5, 3.5])

In single precision, mean can be inaccurate:

>>> a = np.zeros((2, 512*512), dtype=np.float32)
>>> a[0, :] = 1.0
>>> a[1, :] = 0.1
>>> np.mean(a)
0.54999924

Computing the mean in float64 is more accurate:

>>> np.mean(a, dtype=np.float64)
0.55000000074505806 # may vary

Specifying a where argument:

>>> a = np.array([[5, 9, 13], [14, 10, 12], [11, 15, 19]])
>>> np.mean(a)
12.0
>>> np.mean(a, where=[[True], [False], [False]])
9.0

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    Like Article

    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:

    import numpy as np

    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:

    import numpy as np

    arr = [[14, 17, 12, 33, 44],  

           [15, 6, 27, 8, 19], 

           [23, 2, 54, 1, 4, ]] 

    print("nmean of arr, axis = None : ", np.mean(arr)) 

    print("nmean of arr, axis = 0 : ", np.mean(arr, axis = 0)) 

    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 :
    28 Nov, 2018

    Like Article

    Save Article

    This tutorial will show you how to use the NumPy mean function, which you’ll often see in code as numpy.mean or np.mean.

    It will teach you how the NumPy mean function works at a high level and it will also show you some of the details.

    So, you’ll learn about the syntax of np.mean, including how the parameters work.

    This post will also show you clear and simple examples of how to use the NumPy mean function. Those examples will explain everything and walk you through the code.

    Let’s get started by first talking about what the NumPy mean function does.

    NumPy mean computes the average of the values in a NumPy array

    NumPy mean calculates the mean of the values within a NumPy array (or an array-like object).

    Let’s take a look at a visual representation of this.

    Imagine we have a NumPy array with six values:

    A visual example of a numpy array, that we will use with numpy mean

    We can use the NumPy mean function to compute the mean value:

    A visual representation of how NumPy mean computes the average of a NumPy array.

    It’s actually somewhat similar to some other NumPy functions like NumPy sum (which computes the sum on a NumPy array), NumPy median, and a few others. These are similar in that they compute summary statistics on NumPy arrays.

    Further down in this tutorial, I’ll show you exactly how the numpy.mean function works by walking you through concrete examples with real code.

    But before I do that, let’s take a look at the syntax of the NumPy mean function so you know how it works in general.

    The syntax of numpy mean

    Syntactically, the numpy.mean function is fairly simple.

    There’s the name of the function – np.mean() – and then several parameters inside of the function that enable you to control it.

    An explanation of the syntax of NumPy sum.

    In the image above, I’ve only shown 3 parameters – a, axis, and dtype.

    There are actually a few other parameters that you can use to control the np.mean function.

    Let’s look at all of the parameters now to better understand how they work and what they do.

    The parameters of numpy mean

    The np.mean function has five parameters:

    • a
    • axis
    • dtype
    • out
    • keepdims

    Let’s quickly discuss each parameter and what it does.

    a (required)
    The a = parameter enables you to specify the exact NumPy array that you want numpy.mean to operate on.

    This parameter is required. You need to give the NumPy mean something to operate on.

    Having said that, it’s actually a bit flexible. You can give it any array like object. That means that you can pass the np.mean() function a proper NumPy array. But you can also give it things that are structurally similar to arrays like Python lists, tuples, and other objects.

    axis (optional)
    Technically, the axis is the dimension on which you perform the calculation.

    On the other hand, saying it that way confuses many beginners. So another way to think of this is that the axis parameter enables you to calculate the mean of the rows or columns.

    The reason for this is that NumPy arrays have axes. What is an axis? An “axis” is like a dimension along a NumPy array.

    Think of axes like the directions in a Cartesian coordinate system. In Cartesian coordinates, you can move in different directions. We typically call those directions “x” and “y.”

    Similarly, you can move along a NumPy array in different directions. You can move down the rows and across the columns. In NumPy, we call these “directions” axes.

    Specifically, in a 2-dimensional array, “axis 0” is the direction that points vertically down the rows and “axis 1” is the direction that points horizontally across the columns.

    A visual explanation of NumPy array axes.

    So how does this relate to NumPy mean?

    When you have a multi dimensional NumPy array object, it’s possible to compute the mean of a set of values down along the rows or across the columns. In these cases, NumPy produces a new array object that holds the computed means for the rows or the columns respectively.

    This probably sounds a little abstract and confusing, so I’ll show you solid examples of how to do this later in this blog post.

    Additionally, if you’re still a little confused about them, you should read our tutorial that explains how to think about NumPy axes.

    dtype (optional)
    The dtype parameter enables you to specify the exact data type that will be used when computing the mean.

    By default, if the values in the input array are integers, NumPy will actually treat them as floating point numbers (float64 to be exact). And if the numbers in the input are floats, it will keep them as the same kind of float; so if the inputs are float32, the output of np.mean will be float32. If the inputs are float64, the output will be float64.

    Keep in mind that the data type can really matter when you’re calculating the mean; for floating point numbers, the output will have the same precision as the input. If the input is a data type with relatively lower precision (like float16 or float32) the output may be inaccurate due to the lower precision. To fix this, you can use the dtype parameter to specify that the output should be a higher precision float. (See the examples below.)

    out (optional)
    The out parameter enables you to specify a NumPy array that will accept the output of np.mean().

    If you use this parameter, the output array that you specify needs to have the same shape as the output that the mean function computes.

    keepdims (optional)
    The keepdims parameter enables you keep the dimensions of the output the same as the dimensions of the input.

    This confuses many people, so let me explain.

    The NumPy mean function summarizes data. It takes a large number of values and summarizes them. So if you want to compute the mean of 5 numbers, the NumPy mean function will summarize those 5 values into a single value, the mean.

    A visual representation of how NumPy mean computes the average of a NumPy array.

    When it does this, it is effectively reducing the dimensions. If we summarize a 1-dimensional array down to a single scalar value, the dimensions of the output (a scalar) are lower than the dimensions of the input (a 1-dimensional array). The same thing happens if we use the np.mean function on a 2-d array to calculate the mean of the rows or the mean of the columns. When we compute those means, the output will have a reduced number of dimensions.

    Sometimes, we don’t want that. There will be times where we want the output to have the exact same number of dimensions as the input. For example, a 2-d array goes in, and a 2-d array comes out.

    To make this happen, we need to use the keepdims parameter.

    By setting keepdims = True, we will cause the NumPy mean function to produce an output that keeps the dimensions of the output the same as the dimensions of the input.

    This confuses many people, so there will be a concrete example below that will show you how this works.

    Note that by default, keepdims is set to keepdims = False. So the natural behavior of the function is to reduce the number of dimensions when computing means on a NumPy array.

    Now that we’ve taken a look at the syntax and the parameters of the NumPy mean function, let’s look at some examples of how to use the NumPy mean function to calculate averages.

    Before I show you these examples, I want to make note of an important learning principle. When you’re trying to learn and master data science code, you should study and practice simple examples. Simple examples are examples that can help you intuitively understand how the syntax works. Simple examples are also things that you can practice and memorize. Mastering syntax (like mastering any skill) requires study, practice, and repetition.

    And by the way, before you run these examples, you need to make sure that you’ve imported NumPy properly into your Python environment. To do that, you’ll need to run the following code:

    import numpy as np
    

    Ok, now let’s move on to the code.

    Compute the mean of the elements of a 1-d array with np.mean

    Here, we’ll start with something very simple. We’re going to calculate the mean of the values in a single 1-dimensional array.

    To do this, we’ll first create an array of six values by using the np.array function.

    np_array_1d = np.array([0,20,40,60,80,100])
    

    Let’s quickly examine the contents of the array by using the print() function.

    print(np_array_1d)
    

    Which produces the following output:

    [0  20  40  60  80 100]
    

    As you can see, the new array, np_array_1d, contains six values between 0 and 100.

    Now, let’s calculate the mean of the data. Here, we’re just going to call the np.mean function. The only argument to the function will be the name of the array, np_array_1d.

    np.mean(np_array_1d)
    

    This code will produce the mean of the values:

    50
    

    Visually though, we can think of this as follows.

    A visual representation of NumPy mean calculating the mean of a 1-d NumPy array.

    The NumPy mean function is taking the values in the NumPy array and computing the average.

    Keep in mind that the array itself is a 1-dimensional structure, but the result is a single scalar value. In a sense, the mean() function has reduced the number of dimensions. The output has a lower number of dimensions than the input. This will be important to understand when we start using the keepdims parameter later in this tutorial.

    Compute the mean of the elements of a 2-d array with np.mean

    Next, let’s compute the mean of the values in a 2-dimensional NumPy array.

    To do this, we first need to create a 2-d array. We can do that by using the np.arange function. We’ll also use the reshape method to reshape the array into a 2-dimensional array object.

    np_array_2x3 = np.arange(start = 0, stop = 21, step = 4).reshape((2,3))
    

    Let’s quickly look at the contents of the array by using the code print(np_array_2x3):

    [[ 0  4  8]
     [12 16 20]]
    

    As you can see, this is a 2-dimensional object with six values: 0, 4, 8, 12, 16, 20. By using the reshape() function, these values have been re-arranged into an array with 2 rows and 3 columns.

    Now, let’s compute the mean of these values.

    To do this, we’ll use the NumPy mean function just like we did in the prior example. We’ll call the function and the argument to the function will simply be the name of this 2-d array.

    np.mean(np_array_2x3)
    

    Which produces the following result:

    10.0
    

    Here, we’re working with a 2-dimensional array, but the mean() function has still produced a single value.

    A visual representation of how numpy mean will compute the mean of the values in a 2-d numpy array.

    When you use the NumPy mean function on a 2-d array (or an array of higher dimensions) the default behavior is to compute the mean of all of the values.

    Having said that, you can also use the NumPy mean function to compute the mean value in every row or the mean value in every column of a NumPy array.

    Let’s take a look at how to do that.

    Compute the column means of a 2-d array

    Here, we’ll look at how to calculate the column mean.

    To understand how to do this, you need to know how axes work in NumPy.

    Recall earlier in this tutorial, I explained that NumPy arrays have what we call axes. Again, axes are like directions along the array.

    Axis 0 refers to the row direction. Axis 1 refers to the column direction.

    A visual representation of numpy axes.

    You really need to know this in order to use the axis parameter of NumPy mean. There’s not really a great way to learn this, so I recommend that you just memorize it … the row-direction is axis 0 and the column direction is axis 1.

    Having explained axes again, let’s take a look at how we can use this information in conjunction with the axis parameter.

    The axis parameter specifies which axis you want to summarize

    Using the axis parameter is confusing to many people, because the way that it is used is a little counter intuitive. With that in mind, let me explain this in a way that might improve your intuition.

    When we use the axis parameter, we are specifying which axis we want to summarize. Said differently, we are specifying which axis we want to collapse.

    So when we specify axis = 0, that means that we want to collapse axis 0. Remember, axis 0 is the row axis, so this means that we want to collapse or summarize the rows, but keep the columns intact.

    Let me show you an example to help this make sense.

    Compute a mean with axis = 0

    Let’s first create a 2-dimensional NumPy array. (Note: we used this code earlier in the tutorial, so if you’ve already run it, you don’t need to run it again.)

    np_array_2x3 = np.arange(start = 0, stop = 21, step = 4).reshape((2,3))
    

    Ok. Let’s quickly examine the contents by using the code print(np_array_2x3):

    [[ 0  4  8]
     [12 16 20]]
    

    As you can see, this is a 2-dimensional array with 2 rows and 3 columns.

    Now that we have our NumPy array, let’s calculate the mean and set axis = 0.

    np.mean(np_array_2x3, axis = 0)
    

    Which produces the following output:

    array([  6.,  10.,  14.])
    

    What happened here?

    Essentially, the np.mean function has produced a new array. But notice what happened here. Instead of calculating the mean of all of the values, it created a summary (the mean) along the “axis-0 direction.” Said differently, it collapsed the data along the axis-0 direction, computing the mean of the values along that direction.

    Visual explanation of how NumPy mean works when we set axis = 0.

    Why?

    Remember, axis 0 is the row axis. So when we set axis = 0 inside of the np.mean function, we’re basically indicating that we want NumPy to calculate the mean down axis 0; calculate the mean down the row-direction; calculate row-wise.

    This is a little confusing to beginners, so I think it’s important to think of this in terms of directions. Along which direction should the mean function operate? When we set axis = 0, we’re indicating that the mean function should move along the 0th axis … the direction of axis 0.

    If that doesn’t make sense, look again at the picture immediately above and pay attention to the direction along which the mean is being calculated.

    Compute the row means of a 2-d array

    Similarly, we can compute row means of a NumPy array.

    In this example, we’re going to use the NumPy array that we created earlier with the following code:

    np_array_2x3 = np.arange(start = 0, stop = 21, step = 4).reshape((2,3))
    

    This code creates the following array:

    [[ 0  4  8]
     [12 16 20]]
    

    It is a 2-dimensional array. As you can see, it has 3 columns and 2 rows.

    Now, we’re going to calculate the mean while setting axis = 1.

    np.mean(np_array_2x3, axis = 1)
    

    Which gives us the output:

    array([  4.,  16.])
    

    So let’s talk about what happened here.

    First remember that axis 1 is the column direction; the direction that sweeps across the columns.

    A visual representation of axis 1 of a 2-dimensional NumPy array.

    When we set axis = 1 inside of the NumPy mean function, we’re telling np.mean that we want to calculate the mean such that we summarize the data in that direction.

    Again, said differently, we are collapsing the axis-1 direction and computing our summary statistic in that direction (i.e., the mean).

    A visual representation of calculating the mean down axis = 1 with NumPy mean.

    Do you see now?

    Axis 1 is the column direction; the direction that sweeps across the columns.

    When we set axis = 1, we are indicating that we want NumPy to operate along this direction. It will therefore compute the mean of the values along that direction (axis 1), and produce an array that contains those mean values: [4., 16.].

    How to use the keepdims parameter with np.mean

    Ok. Now that you’ve learned about how to use the axis parameter, let’s talk about how to use the keepdims parameter.

    The keepdims parameter of NumPy mean enables you to control the dimensions of the output. Specifically, it enables you to make the dimensions of the output exactly the same as the dimensions of the input array.

    To understand this, let’s first take a look at a few of our prior examples.

    Earlier in this blog post, we calculated the mean of a 1-dimensional array with the code np.mean(np_array_1d), which produced the mean value, 50.

    There’s something subtle here though that you might have missed. The dimensions of the output are not the same as the input.

    To see this, let’s take a look first at the dimensions of the input array. We can do this by examining the ndim attribute, which tells us the number of dimensions:

    np_array_1d.ndim
    

    When you run this code, it will produce the following output: 1. The array np_array_1d is a 1-dimensional array.

    Now let’s take a look at the number of dimensions of the output of np.mean() when we use it on np_array_1d.

    Again, we can do this by using the ndim parameter:

    np.mean(np_array_1d).ndim
    

    Which produces the following output: 0.

    So the input (np_array_1d) has 1 dimension, but the output of np.sum has 0 dimensions … the output is a scalar. In some sense, the output of np.sum has a reduced number of dimensions as the input.

    This is relevant to the keepdims parameter, so bear with me as we take a look at another example.

    Let’s look at the dimensions of the 2-d array that we used earlier in this blog post:

    np_array_2x3.ndim
    

    When you run this code, the output will tell you that np_array_2x3 is a 2-dimensional array.

    What about the output of np.sum?

    If we don’t specify an axis, the output of np.sum() on this array will have 0 dimensions. You can check it with this code:

    np.mean(np_array_2x3).dim
    

    Which produces the following output: 0. When we use np.mean on a 2-d array, it calculates the mean. The mean value is a scalar, which has 0 dimensions. In this case, the output of np.mean has a different number of dimensions than the input.

    What if we set an axis? Remember, if we use np.mean and set axis = 0, it will produce an array of means. Run this code:

    np.mean(np_array_2x3, axis = 0)
    

    Which produces the output array([ 6., 10., 14.]).

    And how many dimensions does this output have? We can check by using the ndim attribute:

    np.mean(np_array_2x3, axis = 0).ndim
    

    Which tells us that the output of np.mean in this case, when we set axis set to 0, is a 1-dimensional object.

    The input had 2 dimensions and the output has 1 dimension.

    Again, the output has a different number of dimensions than the input.

    Ok, now that we’ve looked at some examples showing number of dimensions of inputs vs. outputs, we’re ready to talk about the keepdims parameter.

    The keepdims parameter keeps the dimensions of the output the same as the dimensions of the input

    The keepdims parameter enables you to set the dimensions of the output to be the same as the dimensions of the input.

    keepdims takes a logical argument … meaning that you can set it to True or False.

    By default, the parameter is set as keepdims = False. This means that the mean() function will not keep the dimensions the same. By default, the dimensions of the output will not be the same as the dimensions of the input. And that’s exactly what we just saw in the last few examples in this section!

    On the other hand, if we set keepdims = True, this will cause the number of dimensions of the output to be exactly the same as the dimensions of the input.

    Set keepdims equal to true (and keep the same dimensions)

    Let’s take a look.

    Once again, we’re going to operate on our NumPy array np_array_2x3. Remember, this is a 2-dimensional object, which we saw by examining the ndim attribute.

    Now, let’s once again examine the dimensions of the np.mean function when we calculate with axis = 0.

    np.mean(np_array_2x3, axis = 0).ndim
    

    This code indicates that the output of np.mean in this case has 1-dimension. Why? Because we didn’t specify anything for keepdims so it defaulted to keepdims = False. This code does not deep the dimensions of the output the same as the dimensions of the input.

    Now, let’s explicitly use the keepdims parameter and set keepdims = True.

    np.mean(np_array_2x3, axis = 0, keepdims = True).ndim
    

    Which produces the following output:

    2
    

    When we use np.mean on a 2-d array and set keepdims = True, the output will also be a 2-d array.

    When we set keepdims = True, the dimensions of the output will be the same as the dimensions of the input.

    I’m not going to explain when and why you might need to do this ….

    Just understand that when you need to dimensions of the output to be the same, you can force this behavior by setting keepdims = True.

    How to use the dtype parameter

    Ok, one last example.

    Let’s look at how to specify the output datatype by using the dtype parameter.

    As I mentioned earlier, if the values in your input array are integers the output will be of the float64 data type. If the values in the input array are floats, then the output will be the same type of float. So if the inputs are float32, the outputs will be float32, etc.

    But what if you want to specify another data type for the output?

    You can do this with the dtype parameter.

    Let’s take a look at a simple example.

    Here, we’ll create a simple 1-dimensional NumPy array of integers by using the NumPy numpy arange function.

    np_array_1d_int = np.array([1,3,4,7,11])
    

    And we can check the data type of the values in this array by using the dtype attribute:

    np_array_1d_int.dtype
    

    When you run that code, you’ll find that the values are being stored as integers; int64 to be precise.

    Now let’s use numpy mean to calculate the mean of the numbers:

    mean_output = np.mean(np_array_1d_int)
    

    Now, we can check the data type of the output, mean_output.

    mean_output.dtype
    

    Which tells us that the datatype is float64.

    This is exactly the behavior we should expect. As I mentioned earlier, by default, NumPy produces output with the float64 data type.

    Setting the data type with the dtype parameter

    So now that we’ve looked at the default behavior, let’s change it by explicitly setting the dtype parameter.

    mean_output_alternate = np.mean(np_array_1d_int, dtype = 'float32')
    

    The object mean_output_alternate contains the calculated mean, which is 5.1999998.

    Now, let’s check the datatype of mean_output_alternate.

    mean_output_alternate.dtype
    

    When you run this, you can see that mean_output_alternate contains values of the float32 data type. This is exactly what we’d expect, because we set dtype = 'float32'.

    Be careful when you use the dtype parameter

    As I mentioned earlier, you need to be careful when you use the dtype parameter.

    If you need the output of np.mean to have high precision, you need to be sure to select a data type with high precision. For example, if you need the result to have high precision, you might select float64.

    If you select a data type with low precision (like int), the result may be inaccurate or imprecise.

    If you want to learn data science in Python, learn NumPy

    You’ve probably heard that 80% of data science work is just data manipulation. That’s mostly true.

    If you want to be great at data science in Python, you need to know how to manipulate data in Python.

    And one of the primary toolkits for manipulating data in Python is the NumPy module.

    In this post, I’ve shown you how to use the NumPy mean function, but we also have several other tuturials about other NumPy topics, like how to create a numpy array, how to reshape a numpy array, how to create an array with all zeros, and many more.

    If you’re interested in learning NumPy, definitely check those out.

    For more Python data science tutorials, sign up for our email list

    More broadly though, if you’re interested in learning (and mastering) data science in Python, or data science generally, you should sign up for our email list right now.

    Here at the Sharp Sight blog, we regularly post tutorials about a variety of data science topics … in particular, about NumPy.

    If you want to learn NumPy and data science in Python, sign up for our email list.

    If you sign up for our email list, you’ll receive Python data science tutorials delivered to your inbox.

    You’ll get free tutorials on:

    • NumPy
    • Pandas
    • Base Python
    • Scikit learn
    • Machine learning
    • Deep learning
    • … and more.

    Want to learn data science in Python? Sign up now.

    I am currently working on this project,

     import numpy as np
    
     COLUMN_NUM = 4
     datos = np.genfromtxt('a.csv', delimiter=',')[1:]
    
     #data = np.pad(data, (0, COLUMN_NUM - len(data) % COLUMN_NUM), 'constant')
     ds= datos.reshape((-1, COLUMN_NUM))
    
     print ds
    

    I am gettings this results:

    [[  4.    33.29  33.23  33.23]
     [ 33.29  33.23  33.29  33.29]
     [ 33.29  33.33  33.29  33.33]
     [ 33.29  33.33  33.33  33.37]
     [ 33.33  33.33  33.33  33.33]
     [ 33.37  33.37  33.37  33.37]]
    

    I been trying to take out the mean from each ROW indiviadually but am having a lot of trouble. Can any lease help on figuring this out? Thank you. I am really a new at python.

    Table of Contents
    Hide
    1. Syntax
    2. Parameters
    3. Return Value
    4. Example 1: Using the numpy.mean() method on a flattened array
    5. Example 2: Compute the mean of the array column-wise using numpy.mean()
    6. Example 3: Compute the mean of the array row-wise using numpy.mean()
    7. Example 4: Compute the mean of the array as flattened
    8. Example 5: Compute the mean of the array with column and row majors together
    9. Example 6: Computing the mean of the array with where() parameter
    10. Conclusion

    The numpy.mean() is a function in the NumPy library that is used to calculate the mean of an array of elements along with an axis.

    In NumPy array library or Mathematics, the arithmetic mean is the sum of the given elements along with an axis divided by the total number of elements. It is the average of the given numbers.

    For Example – Imagine we have a NumPy array of five values as shown.

    Image 11

    NumPy Array

    We can use the NumPy np.mean() function to compute the mean of the Array as shown below.

    Image 12

    Compute the mean using np.mean() method

    Syntax

    The Syntax of numpy.mean() method is:

    numpy.mean(array_name, axis=None, dtype=None, out=None, keepdims=<no value>, *, where=<no value>)

    Parameters

    • array_name: the name of the input array containing input numbers for which the mean needs to be computed.
    • axis: None or int or tuple of ints (optional) – the alignment or axis of the mean to be computed. If None, the default is to compute the mean of the flattened array. If int or a tuple of ints, a mean is performed over multiple axes, instead of a single axis or all the axes as before.
    • dtype: [int, float64] (optional) – Data Type to use in computing the mean, use ‘int’ for integer and ‘float64’ for floating point value.
    • out: nd_array (optional) – Alternate output array in which the results are to be placed. The default is None
    • keepdims: bool (optional) – If the value of keepdims is set to True, then the axes that are reduced will be left in the result as dimensions with size one.
    • where: array_like of bool (optional) – Elements to be included in the mean.

    Return Value

    The numpy.mean() method will return a new array with mean values as the elements if out = None, else a reference to the output NumPy array is returned.

    Example 1: Using the numpy.mean() method on a flattened array

    In the below example the numpy.mean() function will compute the weighted mean of a NumPy n-dimensional array array_in input array.

    # import the numpy module
    import numpy as np
    
    # initialize the input array
    array_in = [[15, 25, 30], [35, 55, 70]]
    
    print("The input numpy array is:n", array_in)
    
    # the mean of the array
    print("The mean of the array in flattened is:n", np.mean(array_in, axis=None))
    

    Output

    The input numpy array is:
    
     [[15, 25, 30], [35, 55, 70]]
    
    The mean of the array in flattened is:
     38.333333333333336

    Example 2: Compute the mean of the array column-wise using numpy.mean()

    When the axis parameter is specified as axis = 0, then the mean is computed on the column-major elements as shown in the below example.

    # import the numpy module
    import numpy as np
    
    # initialize the input array
    array_in = [[15, 25, 30], [35, 55, 70]]
    
    print("The input numpy array is:n", array_in)
    
    # the mean of the array as
    print("The mean of the array in column-major is:n", np.mean(array_in, axis=0))
    

    Output

    The input numpy array is:
     [[15, 25, 30], [35, 55, 70]]
    
    The mean of the array in column-major is:
     [25. 40. 50.]

    Example 3: Compute the mean of the array row-wise using numpy.mean()

    When the axis parameter is specified as axis = 1, then the mean is computed on the row-major elements as shown in the below example.

    # import the numpy module
    import numpy as np
    
    # initialize the input array
    array_in = [[15, 25, 30], [35, 55, 70]]
    
    print("The input numpy array is:n", array_in)
    
    # the mean of the array
    print("The mean of the array in row-major is:n", np.mean(array_in, axis=1))
    

    Output

    The input numpy array is:
     [[15, 25, 30], [35, 55, 70]]
    
    The mean of the array in row-major is:
     [23.33333333 53.33333333]

    Example 4: Compute the mean of the array as flattened

    When the axis parameter is specified as axis = None, then the array will be operated as flattened for calculating the mean of the elements.

    import numpy as np
    
    # initialize input array
    array_in = [[1.0, 0.1], [1.5, 0.5]]
    
    print("The input numpy array is:n", array_in)
    
    # the mean of the array as default float64 data_type
    print("The array of means of the input array is:n", np.mean(array_in, axis=1))
    
    # using output data_type as float64
    print(
        "The array of means of the input array in float64 is:n",
        np.mean(array_in, axis=1, dtype=np.float64),
    )
    
    # using output data_type as float32
    print(
        "The array of means of the input array in float32 is:n",
        np.mean(array_in, axis=1, dtype=np.float32),
    )
    
    # using output data_type as int
    print(
        "The array of means of the input array in integer is:n",
        np.mean(array_in, axis=1, dtype=int),
    )
    

    Output

    The input numpy array is:
     [[1.0, 0.1], [1.5, 0.5]]
    
    The array of means of the input array is:
     [0.55 1.  ]
    
    The array of means of the input array in float64 is:
     [0.55 1.  ]
    
    The array of means of the input array in float32 is:
     [0.55 1.  ]
    
    The array of means of the input array in integer is:
     [0 0]
    

    Example 5: Compute the mean of the array with column and row majors together

    When the axis parameter is specified as axis=(0,1), then the mean is computed along both the column and row axis as shown in the example.

    import numpy as np
    
    array_in = [[15, 25, 30], [35, 55, 70]]
    
    print("The input numpy array is:n", array_in)
    
    # the mean of the array both row-major and column-major
    array_out = np.mean(array_in, axis=(0, 1))
    print(
        "The mean of the array in both row-major and column-major and type of the output respectively are:n",
        array_out,
    )
    print(type(array_out))
    

    Output

    The input numpy array is:
     [[15, 25, 30], [35, 55, 70]]
    
    The mean of the array in both row-major and column-major and type of the output respectively are:
     38.333333333333336
    
    <class 'numpy.float64'>

    Example 6: Computing the mean of the array with where() parameter

    If we provide the parameter where = [[True],[False],[False]] then the numpy.mean() function will check the boolean array [[True],[False],[False]], if the value of the element in the boolean array is True, then that row is selected. That is only the first row will get selected for the mean.

    import numpy as np
    
    # initialize the input array
    array_in = np.array([[5, 9, 13], [14, 10, 12], [11, 15, 19]])
    
    print("The input nd_array is:nn", array_in)
    
    # flattened average of the input array
    print("nmean with every row is selected :nn", np.mean(array_in, axis=None))
    
    # every dimension is selected
    print(
        "nmean with every row is selected :nn",
        np.mean(array_in, axis=None, where=[[True], [True], [True]]),
    )
    
    # only the first row is selected for average
    print(
        "nmean with only the first row is selected :nn",
        np.mean(array_in, axis=None, where=[[True], [False], [False]]),
    )
    
    # only the last row is selected for average
    print(
        "nmean with only the last row is selected :nn",
        np.mean(array_in, axis=None, where=[[False], [False], [True]]),
    )
    
    # only the first and second row is selected for average
    print(
        "naverage with only the first and second row is selected :nn",
        np.mean(array_in, axis=None, where=[[True], [True], [False]]),
    )
    

    Output

    The input nd_array is:
     [[ 5  9 13]
     [14 10 12]
     [11 15 19]]
    
    mean with every row is selected :
     12.0
    
    mean with every row is selected :
     12.0
    
    mean with only the first row is selected :
     9.0
    
    mean with only the last row is selected :
     15.0
    
    average with only the first and second row is selected :
     10.5

    Conclusion

    The numpy.mean() function is used to compute the arithmetic mean along the specified axis. The mean() function returns the new array with mean values as the elements if out = None, else a reference to the output NumPy array is returned.

    Reference: NumPy Library

    Понравилась статья? Поделить с друзьями:
  • Как найти по таблице корни функции
  • Ассасин крид блэк флаг как найти чертежи
  • Как в изложении найти микротемы
  • Как найти ангела с фото
  • Как исправить оправу в очках для зрения