“np.mean” Kod odpowiedzi

np.mean

import numpy as np

import numpy as np

array1D = np.array([1,2,3,4,5])

print(f'Axis = -1 --> {array1D.mean(axis=-1)}')
print(f'Axis = 0  --> {array1D.mean(axis=0)}')

#### Output ####
Axis = -1 --> 3.0
Axis = 0  --> 3.0


array2D = np.array([[14, 17, 12, 33, 44],  
                     [15, 6, 27, 8, 19], 
                     [23, 2, 54, 1, 4]] )

print(f'Axis = -1 {array2D.mean(axis=-1)}')
print(f'Axis = 0 {array2D.mean(axis=0)}')
print(f'Axis = 1 {array2D.mean(axis=1)}')

#### Output ####
Axis = -1 [24.  15.  16.8]
Axis = 0 [17.33333333  8.33333333 31.         14.         22.33333333]
Axis = 1 [24.  15.  16.8]

# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
import numpy as np

array3D = np.array([[[1, 2, 3, 4, 5],  
                     [1, 2, 3, 4, 5], 
                     [1, 2, 3, 4, 5]],
                     [[1, 2, 3, 4, 5],  
                     [1, 2, 3, 4, 5], 
                     [1, 2, 3, 4, 5]]])

print(f'Axis = -1 --> {array3D.mean(axis=-1)}')
print(f'Axis = 0  --> {array3D.mean(axis=0)}')
print(f'Axis = 1  --> {array3D.mean(axis=1)}')
print(f'Axis = 2  --> {array3D.mean(axis=2)}')

#### Output ####
Axis = -1 --> [[3. 3. 3.]
              [3. 3. 3.]]
            
Axis = 0  --> [[1. 2. 3. 4. 5.]
               [1. 2. 3. 4. 5.]
               [1. 2. 3. 4. 5.]]
               
Axis = 1  --> [[1. 2. 3. 4. 5.]
              [1. 2. 3. 4. 5.]]
              
Axis = 2  --> [[3. 3. 3.]
              [3. 3. 3.]] 
Fancy Falcon

NP.Array Średni rząd

>>> 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])
Cheerful Cormorant

np.mean

import numpy as np

array1D = [20, 2, 7, 1, 34]
print(np.mean(array1D)) # 12.8 

array2D = [[14, 17, 12, 33, 44],  
           [15, 6, 27, 8, 19], 
           [23, 2, 54, 1, 4]] 
    
# mean of everything in the array, axis = None
print(np.mean(array2D)) # 18.6
    
# mean along the axis = 0 
print(np.mean(array2D, axis = 0)) # [17.333333, 8.333333, 31, 14, 22.333333]
   
# mean along the axis = 1 
print(np.mean(array2D, axis = 1)) # [24, 15, 16.8]
  
zx Tube

Python znaczy ndarray

# define ndarray a
a = np.array([[1, 2], [3, 4]])
# get the mean 
np.mean(a, axis=None)
myname

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu