题目:
1、将因子数据做标准化处理
2、以2021-01-04的PE数据作为例子数据
概念:
因子标准化:将因子数据按某种标准把数据转化成某个范围内
注:一般要将因子数据去极值后再进行标准化处理
import pandas as pdimport numpy as npfrom sklearn.preprocessing import StandardScalerdf_pe_20210104 = pd.read_csv('./pe_20210104.csv',encoding='utf-8')df_pe_20210104 = df_pe_20210104.loc[:,['secID','tradeDate','closePrice','PE']]df_pe_20210104.head()
# 自实现3倍中位数绝对偏差去极值def mad(factor): me = np.median(factor) mad = np.median(abs(factor-me)) # 求出3倍中位数的上下限制 up = me + (3*1.4826*mad) down = me - (3*1.4826*mad) # 利用3倍中位数的值去极值 factor = np.where(factor>up,up,factor) factor = np.where(factor
std_s = StandardScaler()df_pe_20210104['pe_mad_std_s']=std_s.fit_transform(df_pe_20210104['pe_mad'].to_frame())df_pe_20210104['pe_mad_std_s'].plot()
# 自实现标准化def stand(factor):mean = factor.mean()std = factor.std()return (factor-mean)/stddf_pe_20210104['pe_mad_std'] = stand(df_pe_20210104['pe_mad'])
PS:
所使用的数据
链接:https://pan.baidu.com/s/11GubyLR2ZkuiXhyk5n6MzA
提取码:n14t