我正在研究自己的班级指标
import numpy as np
class Metric(object):
def __init__(self,*args):
self.min = min(args)
self.max = max(args)
self.median = np.percentile(args,50)
self.avg = sum(args)*1.0/len(args)
self.len = len(args)
self.std = np.std(args)
self.var = np.var(args)
self.IQR = np.percentile(args,75)-np.percentile(args,25)
self.relief_ratio = (self.avg-self.min)*1.0/(self.max-self.min)
我希望嵌套一个函数来计算峰度而不使用from scipy.stats import kurtosis
. 按照这个例子,峰度公式是:
def avg(obs):
return (1. / len(obs)) * np.sum(obs)
def variance(obs):
return (1. / len(obs)) * np.sum((obs - avg(obs)) ** 2)
def kurt(obs):
num = np.sum((obs - avg(obs)) ** 4)/ len(obs)
denom = variance(obs) ** 2 # avoid losing precision with np.sqrt call
return num / denom
我的问题是:
- 哪个是正确的 python 样式,我需要在课堂外还是在课堂内编写
avg
、、variance
和kurt
? - 如果里面哪个是最好的风格?