Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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]
  
Comment

numpy mean

>> import numpy as np 
>> a=[1,2,3,4,5]
>> np.mean(a)
3.0
Comment

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.]] 
Comment

numpy mean

import numpy as np


speed = [10, 20, 30, 40]

# mean of an array - sum(speed) / len(speed)
x = np.mean(speed)
print(x)
# output 25.0

# return the median number - If there are two numbers in the middle, divide the sum of those numbers by two.
x = np.median(speed)
print(x)
# output 25.0

# return standard deviation - the lower the number return the closer the data is related
x = np.std(speed)
print(x)
# output 11.180339887498949

# return Variance of array - show how spread out the data is. The smaller the number the closer the data is related
x = np.var(speed)
print(x)
# output 125.0

# returns percentile of an array.
x = np.percentile(speed, 20)
print(f"20 percent of speed is {x} or lower")
# output 20 percent of speed is 16.0 or lower

x = np.percentile(speed, 90)
print(f"90 percent of speed is {x} or lower")
# output 90 percent of speed is 37.0 or lower

# We specify that the mean value is 5.0, and the standard deviation is .2.
# the lower the scale the closer the random numbers are to the loc number
# returns size of 100 floats in array
# normal distribution
x = np.random.normal(loc=5.0, scale=.2, size=100)
print(x)

# create array
arr = np.array([10, 20, 20, 30, 30, 20])
print("Original array:")
print(arr)

print("Mode: Most frequent value in the above array:")
print(np.bincount(arr).argmax())
# output
# Most frequent value in the above array:
# 20
# returns the least common multiple
x = np.lcm(3, 4)
print(x)
# output 12


# returns the lowest common multiple of items in array
arr = np.array([3, 6, 9])
x = np.lcm.reduce(arr)
print(x)
# 18

# returns the greatest common multiple of 2 numbers
x = np.gcd(3, 4)
print(x)
# output 1

# return the highest common multiple of items in array
arr = np.array([20, 8, 32, 36, 16])
x = np.gcd.reduce(arr)
print(x)
# output 4
Comment

PREVIOUS NEXT
Code Example
Python :: Video to text convertor in python 
Python :: checksum python 
Python :: manage.py startapp not working in django 
Python :: open word document python 
Python :: how to alight and place ipywidgets 
Python :: python get the intersection of two lists 
Python :: combine two columns pandas 
Python :: python join dict 
Python :: numpy merge 
Python :: python regex get word after string 
Python :: get list with random numbers python 
Python :: how to get a int from string python 
Python :: dfs in python 
Python :: python string trim 
Python :: python shuffle 
Python :: transform data frame in list 
Python :: cumulative frequency for python dataframe 
Python :: virtual mic with python 
Python :: how to make a game in python 
Python :: python private method 
Python :: model.fit(X_train, Y_train, batch_size=80, epochs=2, validation_split=0.1) 
Python :: thread syntax in python 
Python :: python disable logging on unittest 
Python :: export some columns to csv pandas 
Python :: Python Overloading the + Operator 
Python :: python - how many letters are capital in a string 
Python :: range(len()) in python 
Python :: tkinter copy paste 
Python :: linking bootstrap in flask 
Python :: pandas dataframe sort by column 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =