Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

numpy average

import numpy as np 
a = np.arange(6).reshape(3,2) 
average = np.average(a)
Comment

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 :: resample ohlc pandas 
Python :: np sum 
Python :: list methods append in python 
Python :: how to take space separated input in python 
Python :: python random array 
Python :: python create dictionary from csv 
Python :: Setting Up Stylesheet Django 
Python :: looping on string with python 
Python :: pandas row sum 
Python :: how to append panda columns using loop 
Python :: Copying a list using deepcopy() in python 
Python :: update_or_create django 
Python :: rgb color python 
Python :: spark df to pandas df 
Python :: how to get a random number in python 
Python :: or statement python 
Python :: list comprehesion python 
Python :: fibonacci sequence in python using whileloop 
Python :: add readme cmd 
Python :: python jwt 
Python :: pandas dataframe first rows 
Python :: python string cut to length 
Python :: how to make a python terminal 
Python :: how to run shell command ctrl + c in python script 
Python :: use map in python to take input 
Python :: create numpy array with ones 
Python :: seaborn plot histogram for all columns 
Python :: global in python 
Python :: python list directories only 
Python :: creating django project 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =