繼續統計演算法,這次也沒什麼特別的,還沒到那麼深入,也是比較基礎的1、方差-樣本2、協方差(標準差)-樣本3、變異繫數4、相關係數 依然是先造個list,這次把這個功能寫個函數,方便以後調用,另外上一篇寫過的函數這次也會繼承def create_rand_list(min_num,max_num,co ...
繼續統計演算法,這次也沒什麼特別的,還沒到那麼深入,也是比較基礎的
1、方差-樣本
2、協方差(標準差)-樣本
3、變異繫數
4、相關係數
依然是先造個list,這次把這個功能寫個函數,方便以後調用,另外上一篇寫過的函數這次也會繼承
def create_rand_list(min_num,max_num,count_list):
case_list = []
while len(case_list) < count_list:
rand_float = random.uniform(min_num,max_num)
if rand_float in case_list:
continue
case_list.append(rand_float)
case_list = [round(case,2) for case in case_list]
return case_list
下麵是歷史函數
sum_fun() #累加
len_fun() #統計個數
multiply_fun() #累乘
sum_mean_fun() #算數平均數
sum_mean_rate() #算數平均數計算回報
median_fun() #中位數
modes_fun() #眾數
ext_minus_fun() #極差
geom_mean_fun() #幾何平均數
geom_mean_rate() #幾何平均回報
新函數代碼
import random # 先生成一個隨機list,已有函數,不贅述 rand_list = [15.79, 6.83, 12.83, 22.32, 17.92, 6.29, 10.19, 10.13, 24.23, 25.56] # 1、方差-樣本S^2,list中的每個元素減整個list的平均數的平方累加,結果比個數-1,方差總量不-1 def var_fun(rand_list): mean_num = sum_mean_fun(rand_list) #計算平均數
len_num = len_fun(rand_list) #計算總量
var_list = [(x-mean_num)**2 for x in rand_list]
var_sum = sum_fun(var_list)
var_num = var_sum/(len_num - 1)
return var_num # 2、協方差(標準差)-樣本S,這個簡單,用方差開平方就可以了 def covar_fun(rand_list): var_num = var_fun(rand_list)
covar_num = var_num ** 0.5
return covar_num # 3、變異繫數CV,變異程度度量,協方差/算數平均數*100% # 說明(百度百科):在進行數據統計分析時,如果變異繫數大於15%,則要考慮該數據可能不正常,應該剔除 def trans_coef_fun(rand_list): covar_num = covar_fun(rand_list)
mean_num = sum_mean_fun(rand_list)
trans_coef_num = covar_num / mean_num
return trans_coef_num # 4、相關係數-樣本r,表示兩個維之間的線性關係,-1 < r < 1,越接近1關係維間的關係越強 # 因為是兩個維,因此需要輸入兩維的list,演算法比較麻煩 ''' ((x1-mean(x))(y1-mean(y))+(x2-mean(x))(y2-mean(y))+...(xn-mean(x))(yn-mean(y))) /((x1-mean(x))^2+(x2-mean(x))^2+...(xn-mean(x))^2)^0.5*((y1-mean(y))^2+(y2-mean(y))^2+...(yn-mean(y))^2)^0.5 ''' x_list = rand_list y_list = [4.39, 13.84, 9.21, 9.91, 15.69, 14.92, 25.77, 23.99, 8.15, 25.07] def pearson_fun(x_list,y_list): x_mean = sum_mean_fun(x_list)
y_mean = sum_mean_fun(y_list)
len_num = len_fun(x_list)
if len_num == len_fun(y_list):
xy_multiply_list = [(x_list[i]-x_mean)*(y_list[i]-y_mean) for i in range(len_num)]
xy_multiply_num = sum_fun(xy_multiply_list)
else:
print 'input list wrong,another input try'
return None
x_covar_son_list = [(x-x_mean)**2 for x in x_list]
y_covar_son_list = [(y-y_mean)**2 for y in y_list]
x_covar_son_num = sum_fun(x_covar_son_list)
y_covar_son_num = sum_fun(y_covar_son_list)
xy_covar_son_multiply_num = (x_covar_son_num ** 0.5) * (y_covar_son_num ** 0.5)
pearson_num = xy_multiply_num / xy_covar_son_multiply_num
return pearson_num