Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

numpy how to calculate variance

import numpy
numbers = [50,66,77,88,99,100]
x = numpy.var(numbers)
print(x)
Comment

numpy variance

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 :: encoding character or string to integer in python 
Python :: python serial COM3 
Python :: scipy.stats.spearmanr 
Python :: how to get left click input in pygame 
Python :: permutation and combination in python 
Python :: explode multiple columns pandas 
Python :: python3 conditional with boolean 
Python :: how can I print all items in a tuple, separated by commas? 
Python :: Exiting from python Command Line 
Python :: 151 - Power Crisis solution in python 
Python :: pd calculations between columns 
Python :: list vs dictionary python 
Python :: channel unhiding command in discord.py 
Python :: get last x elements of list python 
Python :: how to find the no of user for a wifi using python for ubuntu 
Python :: python popen 
Python :: convert plt.show to image to show opencv 
Python :: midpoint circle drawing algorithm 
Python :: assignment 6.5 python for everybody 
Python :: how to create multiple file in python using for loop. 
Python :: plotly create plot 
Python :: how to calculate the google map distance in python 
Python :: binary tree python 
Python :: np.zero 
Python :: python normalized correlation 
Python :: python collection 
Python :: str remove except alphabets 
Python :: how to move the element of an array in python by one step 
Python :: items of list 
Python :: python print 2d array as table 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =