代码
# -*- coding: utf-8 -*-
#频率 and 众数
freDict {}
#统计元素数量
def count(l):
for item in l:
if item in freDict.keys():
freDict[item] 1
else:
freDict[item] 1
return
#求元素频率
def transform():
s float(sum(freDict.values()))
for k in freDict.keys():
freDict[k] / s
return
#获取众数
def mode():
max_value list(freDict.values())[0]
max_key list(freDict.keys())[0]
for key,value in freDict.items():
if value max_value:
max_value value
max_key key
return max_key
l [ a , b , c , d , e , a , b , c , d , e , c , d , e ]
count(l)
print( Count for Distinct: ,freDict)
transform()
print( Frequency by precent: ,freDict)
print( Mode: ,mode())
输出结果
代码
# -*- coding: utf-8 -*- from math import * import numpy as np iris np.loadtxt(r E:iris_proc.data ,delimiter , ) rel np.linspace(0,0, 11*5).reshape(11,5) rel[...,0] range(0,101,10) for col in range(1,5): rel[...,col] [np.percentile(iris[...,col-1], p) for p in rel[...,0]] print(rel)
输出结果
代码
# -*- coding: utf-8 -*- import math import numpy as np def mean(x): return sum(x)/ float(len(x)) def median(y): x np.sort(y) if len(x)%2 0: return (x[len(x)//2] x[len(x)//2 1])/2.0 else: return x[len(x)//2] def trimmean(x,p): b np.percentile(x, p//2) t np.percentile(x, 100-p//2) return mean([i for i in x if b i t])



