abupy.UtilBu package¶
Submodules¶
abupy.UtilBu.ABuDTUtil module¶
通用装饰器工具模块
-
abupy.UtilBu.ABuDTUtil.
arr_to_numpy
(arr)[源代码]¶ 函数装饰器:将可以迭代的序列转换为np.array,支持pd.DataFrame或者pd.Series ,list,dict, list,set,嵌套可迭代序列, 混嵌套可迭代序列
-
abupy.UtilBu.ABuDTUtil.
arr_to_pandas
(arr)[源代码]¶ 函数装饰器:将可以迭代的序列转换为pd.DataFrame或者pd.Series,支持 np.ndarray,list,dict, list,set,嵌套可迭代序列, 混嵌套可迭代序列
-
abupy.UtilBu.ABuDTUtil.
catch_error
(return_val=None, log=True)[源代码]¶ 作用范围:函数装饰器 (模块函数或者类函数) 功能:捕获被装饰的函数中所有异常,即忽略函数中所有的问题,用在函数的执行级别低,且不需要后续处理 :param return_val: 异常后返回的值,
- eg:
- class A:
@ABuDTUtil.catch_error(return_val=100) def a_func(self):
raise ValueError(‘catch_error’) return 100
in: A().a_func() out: 100
参数: log – 是否打印错误日志
-
abupy.UtilBu.ABuDTUtil.
empty_wrapper
(func)[源代码]¶ 作用范围:函数装饰器 (模块函数或者类函数) 功能:空装饰器,为fix版本问题使用,或者分逻辑功能实现使用
-
abupy.UtilBu.ABuDTUtil.
empty_wrapper_with_params
(*p_args, **p_kwargs)[源代码]¶ 作用范围:函数装饰器 (模块函数或者类函数) 功能:带参数空装饰器,为fix版本问题使用,或者分逻辑功能实现使用
-
abupy.UtilBu.ABuDTUtil.
except_debug
(func)[源代码]¶ 作用范围:函数装饰器 (模块函数或者类函数) 功能:debug,调试使用,装饰在有问题函数上,发生问题打出问题后,再运行一次函数,可以用s跟踪问题了
-
abupy.UtilBu.ABuDTUtil.
params_to_numpy
(func)[源代码]¶ 函数装饰器:不定参数装饰器,定参数转换使用ABuScalerUtil中的装饰器arr_to_numpy(func) 将被装饰函数中的参数中所有可以迭代的序列转换为np.array
abupy.UtilBu.ABuDateUtil module¶
时间日期工具模块
-
abupy.UtilBu.ABuDateUtil.
begin_date
(pre_days, date_str=None, split='-', fix=True)[源代码]¶ - 返回date_str日期前pre_days天的日期str对象
- eg:
pre_days = 2 date_str = ‘2017-02-14’ result = ‘2017-02-12’
pre_days = 365 date_str = ‘2016-01-01’ result = ‘2015-01-01’
如果pre_days是负数,则时间向前推: eg:
pre_days = -365 date_str = ‘2016-01-01’ result = ‘2016-12-31’
参数: - pre_days – pre_days天, int
- date_str – date_str, 默认current_str_date()
- split –
- fix – 是否修复日期不规范的写法,eg. 2016-1-1 fix 2016-01-01
返回: str日期时间对象
-
abupy.UtilBu.ABuDateUtil.
current_date_int
()[源代码]¶ 获取当前时间日期 int值 不使用时间api,直接进行字符串解析,执行效率高 :return: 日期int值
-
abupy.UtilBu.ABuDateUtil.
current_str_date
()[源代码]¶ 获取当前时间日期,时间单位只取到天,返回如2016-01-01 :return: 返回如2016-01-01 str对象
-
abupy.UtilBu.ABuDateUtil.
date_str_to_int
(date_str, split='-', fix=True)[源代码]¶ eg. 2016-01-01 -> 20160101 不使用时间api,直接进行字符串解析,执行效率高 :param date_str: %Y-%m-%d形式时间str对象 :param split: 年月日的分割符,默认’-‘ :param fix: 是否修复日期不规范的写法,eg. 2016-1-1 fix 2016-01-01 :return: int类型时间
-
abupy.UtilBu.ABuDateUtil.
datetime_to_str
(dt_obj)[源代码]¶ datetime时间转换为str对象, str_to_datetime函数的逆向 :param dt_obj: datetime.datetime对象 :return: str对象 eg. ‘2016-01-01’
-
abupy.UtilBu.ABuDateUtil.
diff
(start_date, end_date, check_order=True)[源代码]¶ 对两个输入日期计算间隔的天数,如果check_order=False, str日期对象效率最高 :param start_date: str对象或者int对象,如果check_order=True int对象效率最高 :param end_date: str对象或者int对象,如果check_order=True int对象效率最高 :param check_order: 是否纠正参数顺序是否放置正常,默认check :return:
-
abupy.UtilBu.ABuDateUtil.
fix_date
(date_str)[源代码]¶ - 修复日期不规范的写法:
- eg. 2016-1-1 fix 2016-01-01 eg. 2016:01-01 fix 2016-01-01 eg. 2016,01 01 fix 2016-01-01 eg. 2016/01-01 fix 2016-01-01 eg. 2016/01/01 fix 2016-01-01 eg. 2016/1/1 fix 2016-01-01 eg. 2016:1:1 fix 2016-01-01 eg. 2016 1 1 fix 2016-01-01 eg. 2016 01 01 fix 2016-01-01 .............................
不使用时间api,直接进行字符串解析,执行效率高,注意fix_date内部会使用fmt_date :param date_str: 检测需要修复的日期str对象或者int对象 :return: 修复了的日期str对象
-
abupy.UtilBu.ABuDateUtil.
fmt_date
(convert_date)[源代码]¶ 将时间格式如20160101转换为2016-01-01日期格式, 注意没有对如 201611 这样的做fix适配,外部需要明确知道参数的格式,针对特定格式,不使用时间api, 直接进行字符串解析,执行效率高 :param convert_date: 时间格式如20160101所示,int类型或者str类型对象 :return: %Y-%m-%d日期格式str类型对象
-
abupy.UtilBu.ABuDateUtil.
str_to_datetime
(date_str, fmt='%Y-%m-%d', fix=True)[源代码]¶ 将字符串日期格式转换成datetime.datetime对象 eg. ‘2016-01-01’ -> datetime.datetime(2016, 1, 1, 0, 0) :param date_str: %Y-%m-%d 形式str对象,eg. ‘2016-01-01’ :param fmt: 如date_str不是%Y-%m-%d形式,对应的格式str对象 :param fix: 是否修复日期不规范的写法,eg. 2016-1-1 fix 2016-01-01 :return: datetime.datetime对象,eg. datetime.datetime(2016, 1, 1, 0, 0)
-
abupy.UtilBu.ABuDateUtil.
str_to_datetime_fast
(date_str, split='-', fix=True)[源代码]¶ 不使用datetime api直接进行字符串分解,相对str_to_datetime要快很多,大概2倍的时间,大量时间转换,且格式确定使用 将字符串日期格式转换成datetime.datetime对象 eg. ‘2016-01-01’ -> datetime.datetime(2016, 1, 1, 0, 0) :param date_str: 如date_str不是%Y-%m-%d形式,对应的格式str对象 :param split: 年月日的分割符,默认’-‘ :param fix: 是否修复日期不规范的写法,eg. 2016-1-1 fix 2016-01-01 :return: datetime.datetime对象,eg. datetime.datetime(2016, 1, 1, 0, 0)
-
abupy.UtilBu.ABuDateUtil.
time_seconds
()[源代码]¶ 获取当前时间seconds级计时值 :return: float值 eg. 1498381468.38095
abupy.UtilBu.ABuDelegateUtil module¶
委托工具模块
-
abupy.UtilBu.ABuDelegateUtil.
first_delegate_has_method
(delegate, check_params=True)[源代码]¶ 装饰在类函数上,如果delegate有定义对应名称方法,优先使用delegate中的方法,否则使用被装饰的方法
eg:
class A: def a_func(self):
print(‘a.a_func’)- class B:
- def __init__(self):
- self.a = A()
@ABuDelegateUtil.first_delegate_has_method(‘a’) def a_func(self):
print(‘b.a_func’)
in: B().a_func() out: a.a_func
参数: - delegate – str对象,被委托的类属性对象名称,从被装饰的方法的类成员变量中寻找对应名字的对象
- check_params – 是否检测方法签名是否相同,默认检测
-
abupy.UtilBu.ABuDelegateUtil.
if_delegate_has_method
(delegate)[源代码]¶ 摘自sklearn中metaestimators.py if_delegate_has_method 如果delegate有定义对应方法,才实际定义方法,否则被装饰的方法撤销
-
abupy.UtilBu.ABuDelegateUtil.
replace_word_delegate_has_method
(delegate, key_word, replace_word, check_params=True)[源代码]¶ 不在delegate中寻找完全一样的方法名字,在被装饰的方法名字中的key_word替换为replace_word后再在delegate中寻找,找到优先使用 否则继续使用被装饰的方法
- eg:
- class A:
- def a_func(self):
- print(‘a.a_func’)
- class B:
- def __init__(self):
- self.a = A()
@ABuDelegateUtil.replace_word_delegate_has_method(‘a’, key_word=’b’, replace_word=’a’) def b_func(self):
print(‘b.b_func’)
in: B().b_func() out: a.a_func
参数: - delegate – str对象,被委托的类属性对象名称,从被装饰的方法的类成员变量中寻找对应名字的对象
- key_word – 被装饰的函数名称中将被replace_word替换的key_word,str对象
- replace_word – 替换key_word形成要寻找的在被委托函数中的名字,str对象
- check_params – 是否检测方法签名是否相同,默认检测
abupy.UtilBu.ABuFileUtil module¶
文件处理读取写入
-
abupy.UtilBu.ABuFileUtil.
HDF5_COMP_LEVEL
= 4¶ HDF5_COMP_LIB: 使用的压缩库:’blosc’, ‘bzip2’, ‘lzo’, ‘zlib’, 如果修改了压缩库,需要删除之前的物理文件
-
abupy.UtilBu.ABuFileUtil.
HDF5_COMP_LIB
= 'blosc'¶ HDF5内部存贮依然会使用pickle,即python版本切换,本地文件会有协议冲突:使用所支持的最高协议进行dump
-
abupy.UtilBu.ABuFileUtil.
K_SET_PICKLE_HIGHEST_PROTOCOL
= False¶ HDF5内部存贮依然会使用pickle,即python版本切换,本地文件会有协议冲突:python2, python3协议兼容模式,使用protocol=0
-
abupy.UtilBu.ABuFileUtil.
batch_ctx_h5s
(h5_fn, mode='a')[源代码]¶ 使用上下文管理器方式对hdf5操作进行批量处理,与装饰器模式batch_h5s 功能相同,为外部不方便封装为具体操作函数时使用
- eg:
- with batch_ctx_h5s(h5s_fn):
- for df_dict in df_dicts:
# 每一个df_dict是一个并行的序列返回的数据 for ind, (key_tuple, df) in enumerate(df_dict.values()):
# (key_tuple, df)是保存kl需要的数据, 迭代后直接使用save_kline_df save_kline_df(df, *key_tuple) if df is not None:
- print(“save kl {}_{}_{} {}/{}”.format(key_tuple[0].value, key_tuple[1], key_tuple[2], ind,
- df.shape[0]))
# 完成一层循环一次,即批量保存完一个并行的序列返回的数据后,进行清屏 do_clear_output()
参数: - h5_fn – hdf5文件路径名, 如果为None。即忽略整个批处理流程
- mode – 打开hdf5文件模式。eg:w, r, a
返回:
-
abupy.UtilBu.ABuFileUtil.
batch_h5s
(h5_fn, mode='a')[源代码]¶ - 使用装饰器方式对hdf5操作进行批量处理,外部使用:
- eg: 详见ABuSymbolPd.py
@batch_h5s(h5s_fn) def _batch_save():
- for df_dict in df_dicts:
# 每一个df_dict是一个并行的序列返回的数据 for ind, (key_tuple, df) in enumerate(df_dict.values()):
# (key_tuple, df)是保存kl需要的数据, 迭代后直接使用save_kline_df save_kline_df(df, *key_tuple) if df is not None:
- print(“save kl {}_{}_{} {}/{}”.format(key_tuple[0].value, key_tuple[1], key_tuple[2], ind,
- df.shape[0]))
# 完成一层循环一次,即批量保存完一个并行的序列返回的数据后,进行清屏 do_clear_output()
参数: - h5_fn – hdf5文件路径名, 如果为None。即忽略整个批处理流程
- mode – 打开hdf5文件模式。eg:w, r, a
返回:
-
abupy.UtilBu.ABuFileUtil.
copy_file
(source, target_dir)[源代码]¶ 拷贝文件操作,支持文件夹拷贝操作 :param source: 文件名或者文件夹,str对象, 相对路径或者绝对路径 :param target_dir: 文件名或者文件夹,str对象, 相对路径或者绝对路径
-
abupy.UtilBu.ABuFileUtil.
del_file
(a_path)[源代码]¶ 删除文件操作,支持文件夹删除操作 :param a_path: 文件名或者文件夹,str对象, 相对路径或者绝对路径
-
abupy.UtilBu.ABuFileUtil.
del_hdf5
(file_name, key)[源代码]¶ 对hdf5进行删除操作 :param file_name: hdf5文件路径名 :param key: 保存的数据key
-
abupy.UtilBu.ABuFileUtil.
dump_del_hdf5
(file_name, dump_dict, del_array=None)[源代码]¶ 对hdf5进行删除和保存操作,del_array中的key先删除,后保存dump_dict 字典中的数据 :param file_name: hdf5文件路径名 :param dump_dict: 需要保存到hdf5中的数据字典 :param del_array: 需要从hdf5中删除的key序列
-
abupy.UtilBu.ABuFileUtil.
dump_df_csv
(file_name, df)[源代码]¶ 将pd.DataFrame对象保存在csv中 :param file_name: 保存csv的文件名称 :param df: 需要保存的pd.DataFrame对象
-
abupy.UtilBu.ABuFileUtil.
dump_hdf5
(file_name, input_obj, input_key)[源代码]¶ 对hdf5进行保存操作 :param file_name: hdf5文件路径名 :param input_obj: 保存的数据对象 :param input_key: 保存的数据key
-
abupy.UtilBu.ABuFileUtil.
dump_pickle
(input_obj, file_name, how='normal')[源代码]¶ 存贮python序列化的本地文件 :param input_obj: 需要进行序列化的对象 :param file_name: 文件名,str对象, 相对路径或者绝对路径 :param how: 序列化协议选择,默认normal不特殊处理,
zero使用python2, python3协议兼容模式,使用protocol=0, high使用支持的最高协议
-
abupy.UtilBu.ABuFileUtil.
ensure_dir
(a_path)[源代码]¶ 确保a_path所在路径文件夹存在,如果a_path是文件将确保它上一级 文件夹的存在 :param a_path: str对象, 相对路径或者绝对路径
-
abupy.UtilBu.ABuFileUtil.
ensure_file
(a_path)[源代码]¶ 确保a_path所在路径文件存在,首先会使用ensure_dir确保文件夹存在, 确定a_path是file会确保文件存在 :param a_path: str对象, 相对路径或者绝对路径 :return:
-
abupy.UtilBu.ABuFileUtil.
load_df_csv
(file_name)[源代码]¶ 从csv文件中实例化pd.DataFrame对象 :param file_name: 保存csv的文件名称 :return: pd.DataFrame对象
-
abupy.UtilBu.ABuFileUtil.
load_hdf5
(file_name, key)[源代码]¶ 读取hdf5中的数据 :param file_name: df5文件路径名 :param key: 保存的数据key
abupy.UtilBu.ABuKLUtil module¶
abupy中使用的金融时间序列分析模块, 模块真的方法真的参数都为abupy中格式化好的kl如下
- eg:
- close high low p_change open pre_close volume date date_week key atr21 atr14
2016-07-20 228.36 229.800 225.00 1.38 226.47 225.26 2568498 20160720 2 499 9.1923 8.7234 2016-07-21 220.50 227.847 219.10 -3.44 226.00 228.36 4428651 20160721 3 500 9.1711 8.7251 2016-07-22 222.27 224.500 218.88 0.80 221.99 220.50 2579692 20160722 4 501 9.1858 8.7790 2016-07-25 230.01 231.390 221.37 3.48 222.27 222.27 4490683 20160725 0 502 9.2669 8.9298 2016-07-26 225.93 228.740 225.63 -1.77 227.34 230.01 41833 20160726 1 503 9.1337 8.7541
-
abupy.UtilBu.ABuKLUtil.
bcut_change_vc
(df, bins=None)[源代码]¶ - eg:
tsla = ABuSymbolPd.make_kl_df(‘usTSLA’) ABuKLUtil.bcut_change_vc(tsla)
- out:
- p_change rate
(0, 3] 209 0.4147 (-3, 0] 193 0.3829 (3, 7] 47 0.0933 (-7, -3] 44 0.0873 (-10, -7] 6 0.0119 (7, 10] 3 0.0060 (10, inf] 1 0.0020 (-inf, -10] 1 0.0020
参数: - df – abupy中格式化好的kl,或者字典,或者可迭代序列
- bins – 默认eg:[-np.inf, -10, -7, -3, 0, 3, 7, 10, np.inf]
返回: pd.DataFrame
-
abupy.UtilBu.ABuKLUtil.
date_week_mean
(df)[源代码]¶ eg:
tsla = ABuSymbolPd.make_kl_df(‘usTSLA’) ABuKLUtil.date_week_mean(tsla)
out: 周一 0.0626 周二 0.0475 周三 0.0881 周四 0.2691 周五 -0.2838
参数: df – abupy中格式化好的kl,或者字典,或者可迭代序列 返回: pd.Series或者pd.DataFrame
-
abupy.UtilBu.ABuKLUtil.
date_week_wave
(df)[源代码]¶ 根据周几分析金融时间序列中的日波动:
- eg:
tsla = ABuSymbolPd.make_kl_df(‘usTSLA’) ABuKLUtil.date_week_wave(tsla)
- out:
- usTSLAwave date_week 周一 3.8144 周二 3.3326 周三 3.3932 周四 3.3801 周五 2.9923
参数: df – abupy中格式化好的kl,或者字典,或者可迭代序列 返回: pd.Series或者pd.DataFrame
-
abupy.UtilBu.ABuKLUtil.
date_week_win
(df)[源代码]¶ - eg:
tsla = ABuSymbolPd.make_kl_df(‘usTSLA’) ABuKLUtil.date_week_win(tsla)
- out:
- 0 1 win
date_week 0 44 51 0.5368 1 55 48 0.4660 2 48 57 0.5429 3 44 57 0.5644 4 53 47 0.470
参数: df – bupy中格式化好的kl,或者字典,或者可迭代序列 返回: pd.Series或者pd.DataFrame
-
abupy.UtilBu.ABuKLUtil.
p_change_stats
(df)[源代码]¶ - eg :
tsla = ABuSymbolPd.make_kl_df(‘usTSLA’) ABuKLUtil.p_change_stats(tsla)
out:
日涨幅平均值1.861, 共260个交易日上涨走势 日跌幅平均值-1.906, 共244个交易日下跌走势 日平均涨跌比0.977, 上涨下跌数量比:1.066
参数: df – abupy中格式化好的kl,或者字典,或者可迭代序列
-
abupy.UtilBu.ABuKLUtil.
qcut_change_vc
(df, q=10)[源代码]¶ - eg:
tsla = ABuSymbolPd.make_kl_df(‘usTSLA’) ABuKLUtil.qcut_change_vc(tsla)
- out:
- change
0 [-10.45, -3.002] 1 (-3.002, -1.666] 2 (-1.666, -0.93] 3 (-0.93, -0.396] 4 (-0.396, 0.065] 5 (0.065, 0.48] 6 (0.48, 1.102] 7 (1.102, 1.922] 8 (1.922, 3.007] 9 (3.007, 11.17]
参数: - df – abupy中格式化好的kl,或者字典,或者可迭代序列
- q – 透传qcut使用的q参数,默认10,10等分
返回: pd.DataFrame
-
abupy.UtilBu.ABuKLUtil.
resample_close_mean
(df, bins=None)[源代码]¶ 对金融时间序列进行变换周期重新采样,对重新采样的结果进行pct_change处理 ,对pct_change序列取abs绝对值,对pct_change绝对值序列取平均,即算出 重新采样的周期内的平均变化幅度
eg: tsla = ABuSymbolPd.make_kl_df(‘usTSLA’) ABuKLUtil.resample_close_mean(tsla)
- out:
- resample
5D 0.0340 10D 0.0468 21D 0.0683 42D 0.0805 60D 0.1002 90D 0.0931 120D 0.0939
参数: - df – abupy中格式化好的kl,或者字典,或者可迭代序列
- bins – 默认eg: [‘5D’, ‘10D’, ‘21D’, ‘42D’, ‘60D’, ‘90D’, ‘120D’]
返回: pd.DataFrame
abupy.UtilBu.ABuLazyUtil module¶
lazy工具模块
-
class
abupy.UtilBu.ABuLazyUtil.
LazyClsFunc
(func)[源代码]¶ Bases:
abupy.UtilBu.ABuLazyUtil.LazyFunc
描述器类: 作用在类中需要lazy的类方法上,实际上只是使用__get__(owner, owner) 替换原始__get__(self, instance, owner)
abupy.UtilBu.ABuMacUtil module¶
mac os 平台工具模块
-
abupy.UtilBu.ABuMacUtil.
fold_free_size_mb
(folder)[源代码]¶ mac os下剩余磁盘空间获取 :param folder: 目标目录 :return: 返回float,单位mb
-
abupy.UtilBu.ABuMacUtil.
show_msg
(title, msg)[源代码]¶ 使用osascript脚步提示弹窗,主要用在长时间且耗时的任务中,提示重要问题信息 :param title: 弹窗标题 :param msg: 弹窗信息
abupy.UtilBu.ABuMd5 module¶
md5, crc32等加密,变换匹配模块
abupy.UtilBu.ABuOsUtil module¶
操作系统工具函数整合模块
-
abupy.UtilBu.ABuOsUtil.
fold_free_size_mb
(folder)[源代码]¶ 统一平路径可用空间获取,被catch_error装饰,即不应该被提示中断程序,return_val=0,出错也返回0 :param folder: 路径或盘符信息 :return: folder下的可用空间大小
-
abupy.UtilBu.ABuOsUtil.
show_msg
(title, msg, log=True)[源代码]¶ 统一平台弹窗信息提示,被catch_error装饰,即不应该被提示中断程序, 特别长任务的情况下 :param title: 弹窗信息标题 :param msg: 弹窗信息内容 :param log: 是否通过logging.info打印信息 :return:
abupy.UtilBu.ABuPlatform module¶
平台信息工具模块
-
abupy.UtilBu.ABuPlatform.
get_sys_info
()[源代码]¶ 平台基本信息收集 eg:
- [(‘python’, ‘3.6.0.final.0’),
- (‘python-bits’, 64), (‘OS’, ‘Darwin’), (‘OS-release’, ‘15.6.0’), (‘machine’, ‘x86_64’), (‘processor’, ‘i386’), (‘byteorder’, ‘little’), (‘LC_ALL’, ‘None’), (‘LANG’, ‘zh_CN.UTF-8’)]
-
abupy.UtilBu.ABuPlatform.
show_versions
()[源代码]¶ 平台基本信息收集以及主要lib版本号信息 eg.
python: 3.6.0.final.0 python-bits: 64 OS: Darwin OS-release: 15.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: zh_CN.UTF-8
pandas: 0.19.2 sklearn: 0.18.1 numpy: 1.11.3 scipy: 0.18.1 statsmodels: 0.6.1 notebook: 4.3.1 tables: 3.3.0 seaborn: 0.7.1 matplotlib: 2.0.0 requests: 2.12.4 bs4: 4.5.3 numba: 0.30.1
abupy.UtilBu.ABuProgress module¶
辅助进度显示模块,多进程,单进程
-
class
abupy.UtilBu.ABuProgress.
AbuBlockProgress
(label, interval=1, max_step=20)[源代码]¶ Bases:
object
主进程阻塞任务,启动子单进程任务进度显示控制类
-
class
abupy.UtilBu.ABuProgress.
AbuMulPidProgress
(total, label)[源代码]¶ Bases:
object
多进程进度显示控制类
-
class
abupy.UtilBu.ABuProgress.
AbuProgress
(total, a_progress, label=None)[源代码]¶ Bases:
object
单进程(主进程)进度显示控制类
-
progress
¶ property获取self._progress
-
-
abupy.UtilBu.ABuProgress.
K_SOCKET_FN_BASE
= '/Users/tu/abu/data/cache/abu_socket_progress'¶ 多进程下进度条通信socket文件最终名字,这里子进程可以获取g_socket_fn是通过ABuEnvProcess拷贝了主进程全局信息
-
abupy.UtilBu.ABuProgress.
check_process_is_dead
()[源代码]¶ 主进程下的子线程函数:检测ui_progress_dict中的进程pid是否仍然活着,如果死了,从字典中清除,close ui
-
abupy.UtilBu.ABuProgress.
do_check_process_is_dead
()[源代码]¶ 执行检测ui_progress_dict中的进程pid是否仍然活着,如果死了,从字典中清除,close ui
-
abupy.UtilBu.ABuProgress.
do_clear_output
(wait=False)[源代码]¶ 模块方法,clear所有的输出,内部针对notebook和命令行输出做区分 :param wait: 是否同步执行clear操作,透传给IPython.display.clear_output
-
abupy.UtilBu.ABuProgress.
g_show_ui_progress
= True¶ 主进程下用来存贮子进程传递子进程pid为key,进度条对象UIProgress为value
-
abupy.UtilBu.ABuProgress.
g_socket_fn
= None¶ 多进程下进度是否显示ui进度,只针对进程间通信类型的进度,有些太频繁的进度显示可以选择关闭
abupy.UtilBu.ABuRegUtil module¶
拟合工具模块
-
abupy.UtilBu.ABuRegUtil.
calc_regress_deg
(y, show=True)[源代码]¶ 将y值 zoom到与x一个级别,之后再fit出弧度转成角度 1 多个股票的趋势比较提供量化基础,只要同一个时间范围,就可以比较 2 接近视觉感受到的角度 :param y: 可迭代序列 :param show: 是否可视化结果 :return: deg角度float值
-
abupy.UtilBu.ABuRegUtil.
least_valid_poly
(y, zoom=False, show=True, metrics_func=<function metrics_rmse>)[源代码]¶ 套接valid_poly,检测至少poly次拟合曲线可以代表原始曲线y的走势 :param y: 原始可迭代序列 :param zoom: 是否对y数据进行缩放 :param show: 是否原始曲线y,均线,以及拟合曲线可视化 :param metrics_func: 度量始y值和均线y_roll_mean的距离和原始y值和
拟合回归的趋势曲线y_fit的距离的方法,默认使用metrics_rmse返回: 至少poly次拟合曲线可以代表原始曲线y的走势,int
-
abupy.UtilBu.ABuRegUtil.
metrics_cosine
(y, y_fit, show=True)[源代码]¶ 度量原始序列和拟合后的y_fit的cosine余弦距离 :param y: 原始可迭代序列 :param y_fit: 拟合可迭代序列 :param show: 是否输出余弦距离值 :return: 返回曼余弦距离值,float
-
abupy.UtilBu.ABuRegUtil.
metrics_euclidean
(y, y_fit, show=True)[源代码]¶ 度量原始序列和拟合后的y_fit的euclidean欧式距离(L2范数) :param y: 原始可迭代序列 :param y_fit: 拟合可迭代序列 :param show: 是否输出欧式距离(L2范数)值 :return: 返回欧式距离(L2范数)值,float
-
abupy.UtilBu.ABuRegUtil.
metrics_mae
(y, y_fit, show=True)[源代码]¶ - 度量原始序列和拟合后的y_fit的MAE:
- MAE = sum(np.abs(y - y_fit)) / len(y)
参数: - y – 原始可迭代序列
- y_fit – 拟合可迭代序列
- show – 是否输出mae值
返回: 返回mae值,float
-
abupy.UtilBu.ABuRegUtil.
metrics_manhattan
(y, y_fit, show=True)[源代码]¶ 度量原始序列和拟合后的y_fit的manhattan曼哈顿距离(L1范数) :param y: 原始可迭代序列 :param y_fit: 拟合可迭代序列 :param show: 是否输出曼哈顿距离(L1范数)值 :return: 返回曼哈顿距离(L1范数)值,float
-
abupy.UtilBu.ABuRegUtil.
metrics_mse
(y, y_fit, show=True)[源代码]¶ - 度量原始序列和拟合后的y_fit的MSE:
- MSE = sum(np.square(y - y_fit)) / len(y)
参数: - y – 原始可迭代序列
- y_fit – 拟合可迭代序列
- show – 是否输出MSE值
返回: 返回MSE值,float
-
abupy.UtilBu.ABuRegUtil.
metrics_rmse
(y, y_fit, show=True)[源代码]¶ - 度量原始序列和拟合后的y_fit的RMSE:
- RMSE = np.sqrt(sum(np.square(y - y_fit)) / len(y))
参数: - y – 原始可迭代序列
- y_fit – 拟合可迭代序列
- show – 是否输出RMSE值
返回: 返回RMSE值,float
-
abupy.UtilBu.ABuRegUtil.
regress_xy
(x, y, mode=True, zoom=False, show=False)[源代码]¶ 使用statsmodels.regression.linear_model进行简单拟合操作,返回model和y_fit :param x: 可迭代序列 :param y: 可迭代序列 :param mode: 是否需要mode结果,在只需要y_fit且效率需要高时应设置False, 效率差异:
mode=False: 1000 loops, best of 3: 778 µs per loop mode=True: 1000 loops, best of 3: 1.23 ms per loop参数: - zoom – 是否缩放x,y
- show – 是否可视化结果
返回: model, y_fit, 如果mode=False,返回的model=None
-
abupy.UtilBu.ABuRegUtil.
regress_xy_polynomial
(x, y, poly=1, zoom=False, show=False)[源代码]¶ 多项式拟合, 根据参数poly决定,返回拟合后的y_fit :param x: 可迭代序列 :param y: 可迭代序列 :param poly: 几次拟合参数,int :param zoom: 是否对数据进行缩放 :param show: 是否可视化显示拟合结果 :return: y_fit
-
abupy.UtilBu.ABuRegUtil.
regress_y
(y, mode=True, zoom=False, show=False)[源代码]¶ 使用statsmodels.regression.linear_model进行简单拟合操作, 参数中只提供y序列, x使用np.arange(0, len(y))填充 :param y: 可迭代序列 :param mode: 是否需要mode结果,在只需要y_fit且效率需要高时应设置False, 效率差异:
mode=False: 1000 loops, best of 3: 778 µs per loop mode=True: 1000 loops, best of 3: 1.23 ms per loop参数: - zoom – 是否缩放x,y
- show – 是否可视化结果
返回: model, y_fit, 如果mode=False,返回的model=None
-
abupy.UtilBu.ABuRegUtil.
regress_y_polynomial
(y, poly=1, zoom=False, show=False)[源代码]¶ 套接regress_xy_polynomial操作, 参数中只提供y序列,x使用np.arange(0, len(y))填充 :param y: 可迭代序列 :param poly: 几次拟合参数,int :param zoom: 是否对数据进行缩放 :param show: 是否可视化显示拟合结果 :return: y_fit
-
abupy.UtilBu.ABuRegUtil.
search_best_poly
(y, poly_min=1, poly_max=100, zoom=False, show=True, metrics_func=<function metrics_rmse>)[源代码]¶ 寻找poly(1-100)次多项式拟合回归的趋势曲线可以比较完美的代表原始曲线y的走势, 基础思路:
1. 对原始曲线y进行窗口均线计算,窗口的大小= math.ceil(len(y) / 4) eg:
原始y序列=504 -> rolling_window = math.ceil(len(y) / 4) = 1262. 通过pd_rolling_mean计算出均线的值y_roll_mean 3 使用metrics_func方法度量原始y值和均线y_roll_mean的距离distance_mean 3. 迭代计算1-100poly次regress_xy_polynomial的拟合曲线y_fit 4. 使用metrics_func方法度量原始y值和拟合回归的趋势曲线y_fit的距离distance_fit 5. 如果distance_fit <= distance_mean* 0.6即代表拟合曲线可以比较完美的代表原始曲线y的走势,停止迭代 6. 返回停止迭代时的poly次数
参数: - y – 原始可迭代序列
- poly_min – 寻找最佳拟合次数的最少次数,eg,2
- poly_max – 寻找最佳拟合次数的最多次数,eg:20
- zoom – 是否对y数据进行缩放
- show – 是否原始曲线y,均线,以及拟合曲线可视化
- metrics_func – 度量始y值和均线y_roll_mean的距离和原始y值和 拟合回归的趋势曲线y_fit的距离的方法,默认使用metrics_rmse
返回: 返回停止迭代时的poly次数
-
abupy.UtilBu.ABuRegUtil.
valid_poly
(y, poly=1, zoom=False, show=True, metrics_func=<function metrics_rmse>)[源代码]¶ 验证poly(默认=1)次多项式拟合回归的趋势曲线是否能代表原始曲线y的走势, 基础思路:
1. 对原始曲线y进行窗口均线计算,窗口的大小= math.ceil(len(y) / 4) eg:
原始y序列=504 -> rolling_window = math.ceil(len(y) / 4) = 1262. 通过pd_rolling_mean计算出均线的值y_roll_mean 3 使用metrics_func方法度量原始y值和均线y_roll_mean的距离distance_mean 3. 通过计算regress_xy_polynomial计算多项式拟合回归的趋势曲线y_fit 4. 使用metrics_func方法度量原始y值和拟合回归的趋势曲线y_fit的距离distance_fit 5. 如果distance_fit <= distance_mean即代表拟合曲线可以代表原始曲线y的走势
参数: - y – 原始可迭代序列
- poly – 几次拟合参数,int
- zoom – 是否对y数据进行缩放
- show – 是否原始曲线y,均线,以及拟合曲线可视化
- metrics_func – 度量始y值和均线y_roll_mean的距离和原始y值和 拟合回归的趋势曲线y_fit的距离的方法,默认使用metrics_rmse
返回: 是否poly次拟合曲线可以代表原始曲线y的走势
abupy.UtilBu.ABuScalerUtil module¶
标准规范化数据工具模块
-
abupy.UtilBu.ABuScalerUtil.
scaler_log10
(group)[源代码]¶ - 对输入的group进行np.log10
- eg.
pd.DataFrame对象
- input:
- tsla bidu noah sfun goog vips aapl
2014-07-25 223.57 226.50 15.32 12.110 589.02 21.349 97.67 2014-07-28 224.82 225.80 16.13 12.450 590.60 21.548 99.02 2014-07-29 225.01 220.00 16.75 12.220 585.61 21.190 98.38 2014-07-30 228.92 219.13 16.83 11.780 587.42 21.185 98.15
- output:
- tsla bidu noah sfun goog vips aapl
2014-07-25 2.3494 2.3551 1.1853 1.0831 2.7701 1.3294 1.9898 2014-07-28 2.3518 2.3537 1.2076 1.0952 2.7713 1.3334 1.9957 2014-07-29 2.3522 2.3424 1.2240 1.0871 2.7676 1.3261 1.9929 2014-07-30 2.3597 2.3407 1.2261 1.0711 2.7689 1.3260 1.9919
pd.Series对象
input: 2014-07-25 223.57 2014-07-28 224.82 2014-07-29 225.01 2014-07-30 228.92 2014-07-31 223.30
output: 2014-07-25 2.3494 2014-07-28 2.3518 2014-07-29 2.3522 2014-07-30 2.3597 2014-07-31 2.3489
np.array
input: array([[ 223.57 , 226.5 , 15.32 , ..., 589.02 , 21.349, 97.67 ],
[ 224.82 , 225.8 , 16.13 , ..., 590.6 , 21.548, 99.02 ], [ 225.01 , 220. , 16.75 , ..., 585.61 , 21.19 , 98.38 ], ..., [ 222.27 , 160.88 , 25.5 , ..., 742.74 , 13.51 , 98.66 ], [ 230.01 , 160.25 , 25.57 , ..., 739.77 , 13.39 , 97.34 ], [ 225.93 , 163.09 , 24.75 , ..., 740.92 , 13.655, 97.76 ]])
output: array([[ 2.3494, 2.3551, 1.1853, ..., 2.7701, 1.3294, 1.9898], [ 2.3518, 2.3537, 1.2076, ..., 2.7713, 1.3334, 1.9957], [ 2.3522, 2.3424, 1.224 , ..., 2.7676, 1.3261, 1.9929], ..., [ 2.3469, 2.2065, 1.4065, ..., 2.8708, 1.1307, 1.9941], [ 2.3617, 2.2048, 1.4077, ..., 2.8691, 1.1268, 1.9883], [ 2.354 , 2.2124, 1.3936, ..., 2.8698, 1.1353, 1.9902]])
参数: group – pd.DataFrame对象, pd.Series对象, np.array对象
-
abupy.UtilBu.ABuScalerUtil.
scaler_matrix
(group, type_look='look_max', mean_how=False)[源代码]¶ 将二维序列按照 type_look 进行整体数据缩放,把所有数据缩放到一个数量级值上
eg: group:
tsla bidu noah sfun goog vips aapl2014-07-25 223.57 226.50 15.32 12.110 589.02 21.349 97.67 2014-07-28 224.82 225.80 16.13 12.450 590.60 21.548 99.02 2014-07-29 225.01 220.00 16.75 12.220 585.61 21.190 98.38 2014-07-30 228.92 219.13 16.83 11.780 587.42 21.185 98.15
- if type_look == ‘look_max’:
group_max = group.max() eg:
group_max:
tsla 286.04 bidu 250.34 noah 37.32 sfun 12.52 goog 776.60 vips 30.00 aapl 133.00
max = group_max.max() eg:
max = 776.60scale_factor = max / group_max
- eg:
- scale_factor: tsla 2.7150 bidu 3.1022 noah 20.8092 sfun 62.0288 goog 1.0000 vips 25.8867 aapl 5.8391
- if type_look == ‘look_min’:
group_min = group.min() eg:
group_min:
tsla 143.67 bidu 132.37 noah 12.95 sfun 4.44 goog 492.55 vips 10.35 aapl 90.34
min = group_min.min()
- eg:
- min = 4.44
scale_factor = min / group_min
- eg:
scale_factor:
tsla 0.0309 bidu 0.0335 noah 0.3429 sfun 1.0000 goog 0.0090 vips 0.4290 aapl 0.0491
通过计算出来的缩放系数,做个转置后 * 输入group, 如果使用的是look_max: eg.
tsla bidu noah sfun goog vips aapl2014-07-25 606.9936 702.6440 318.7972 751.1682 589.02 552.6544 570.3047 2014-07-28 610.3874 700.4725 335.6527 772.2580 590.60 557.8059 578.1875 2014-07-29 610.9033 682.4798 348.5544 757.9914 585.61 548.5385 574.4504 2014-07-30 621.5189 679.7809 350.2191 730.6987 587.42 548.4090 573.1074
通过计算出来的缩放系数,做个转置后 * 输入group, 如果使用的是look_min: eg.
tsla bidu noah sfun goog vips aapl2014-07-25 6.9092 7.5973 5.2526 12.110 5.3096 9.1584 4.8003 2014-07-28 6.9479 7.5739 5.5303 12.450 5.3239 9.2438 4.8666 2014-07-29 6.9537 7.3793 5.7429 12.220 5.2789 9.0902 4.8351 2014-07-30 7.0746 7.3501 5.7703 11.780 5.2952 9.0881 4.8238
参数: - group – pd.DataFrame or np.array
- type_look – str对象,type_look in (‘look_max’, ‘look_min)
- mean_how – bool, 默认False, 决策group_max或者group_min是使用max,min还是mean
返回: 缩放后的pd.DataFrame,注意统一格式为pd.DataFrame,不管输入的是什么
-
abupy.UtilBu.ABuScalerUtil.
scaler_mm
(group)[源代码]¶ - 对输入的group进行(group - group.min()) / (group.max() - group.min())
- eg.
pd.DataFrame对象
- input:
- tsla bidu noah sfun goog vips aapl
2014-07-25 223.57 226.50 15.32 12.110 589.02 21.349 97.67 2014-07-28 224.82 225.80 16.13 12.450 590.60 21.548 99.02 2014-07-29 225.01 220.00 16.75 12.220 585.61 21.190 98.38 2014-07-30 228.92 219.13 16.83 11.780 587.42 21.185 98.15
- output:
- tsla bidu noah sfun goog vips aapl
2014-07-25 0.5612 0.7979 0.0973 0.9493 0.3396 0.5597 0.1718 2014-07-28 0.5700 0.7920 0.1305 0.9913 0.3452 0.5699 0.2035 2014-07-29 0.5713 0.7428 0.1559 0.9629 0.3276 0.5517 0.1885 2014-07-30 0.5988 0.7354 0.1592 0.9084 0.3340 0.5514 0.1831
pd.Series对象
input: 2014-07-25 223.57 2014-07-28 224.82 2014-07-29 225.01 2014-07-30 228.92 2014-07-31 223.30
output: 2014-07-25 0.5612 2014-07-28 0.5700 2014-07-29 0.5713 2014-07-30 0.5988 2014-07-31 0.5593
np.array
input: array([[ 223.57 , 226.5 , 15.32 , ..., 589.02 , 21.349, 97.67 ],
[ 224.82 , 225.8 , 16.13 , ..., 590.6 , 21.548, 99.02 ], [ 225.01 , 220. , 16.75 , ..., 585.61 , 21.19 , 98.38 ], ..., [ 222.27 , 160.88 , 25.5 , ..., 742.74 , 13.51 , 98.66 ], [ 230.01 , 160.25 , 25.57 , ..., 739.77 , 13.39 , 97.34 ], [ 225.93 , 163.09 , 24.75 , ..., 740.92 , 13.655, 97.76 ]])
output:
- array([[ 0.2838, 0.2876, 0.0141, ..., 0.7571, 0.0219, 0.1207],
- [ 0.2854, 0.2867, 0.0151, ..., 0.7591, 0.0222, 0.1225], [ 0.2857, 0.2792, 0.0159, ..., 0.7527, 0.0217, 0.1217], ..., [ 0.2821, 0.2026, 0.0273, ..., 0.9561, 0.0117, 0.122 ], [ 0.2921, 0.2018, 0.0274, ..., 0.9523, 0.0116, 0.1203], [ 0.2868, 0.2055, 0.0263, ..., 0.9538, 0.0119, 0.1209]])
参数: group – pd.DataFrame对象, pd.Series对象, np.array对象
-
abupy.UtilBu.ABuScalerUtil.
scaler_one
(group)[源代码]¶ 使用序列的第一个数据进行标准化处理,即将序列转换为第一个数据为1.0开始的序列 eg: group
2011-07-28 9820.0 2011-07-29 9845.0 2011-08-01 9685.0 2011-08-02 9653.0 2011-08-03 9549.0 2011-08-04 9345.0 2011-08-05 9114.8 2011-08-08 8766.8 2011-08-09 8750.0 2011-08-10 8646.0
...2017-07-13 5864.5 2017-07-14 5928.0 2017-07-17 6004.5 2017-07-18 6004.0 2017-07-19 5966.0 2017-07-20 5982.0 2017-07-21 5997.0 2017-07-24 6033.5 2017-07-25 6261.0 2017-07-26 6306.5
scaler_one(group)
2011-07-28 1.0000 2011-07-29 1.0025 2011-08-01 0.9863 2011-08-02 0.9830 2011-08-03 0.9724 2011-08-04 0.9516 2011-08-05 0.9282 2011-08-08 0.8927 2011-08-09 0.8910 2011-08-10 0.8804
...2017-07-13 0.5972 2017-07-14 0.6037 2017-07-17 0.6115 2017-07-18 0.6114 2017-07-19 0.6075 2017-07-20 0.6092 2017-07-21 0.6107 2017-07-24 0.6144 2017-07-25 0.6376 2017-07-26 0.6422
参数: group – pd.DataFrame对象, pd.Series对象, np.array对象
-
abupy.UtilBu.ABuScalerUtil.
scaler_std
(group)[源代码]¶ - 对输入的group进行(group - group.mean()) / group.std()
- eg.
pd.DataFrame对象
- input:
- tsla bidu noah sfun goog vips aapl
2014-07-25 223.57 226.50 15.32 12.110 589.02 21.349 97.67 2014-07-28 224.82 225.80 16.13 12.450 590.60 21.548 99.02 2014-07-29 225.01 220.00 16.75 12.220 585.61 21.190 98.38 2014-07-30 228.92 219.13 16.83 11.780 587.42 21.185 98.15
output:
tsla bidu noah sfun goog vips aapl2014-07-25 -0.1924 1.2047 -1.5325 2.6732 -0.4127 0.4478 -1.1364 2014-07-28 -0.1435 1.1791 -1.3850 2.8601 -0.3942 0.4860 -1.0202 2014-07-29 -0.1361 0.9677 -1.2721 2.7337 -0.4525 0.4172 -1.0753 2014-07-30 0.0169 0.9360 -1.2575 2.4919 -0.4314 0.4163 -1.0951
pd.Series对象
input:
2014-07-25 223.57 2014-07-28 224.82 2014-07-29 225.01 2014-07-30 228.92 2014-07-31 223.30
output:
2014-07-25 -0.1924 2014-07-28 -0.1435 2014-07-29 -0.1361 2014-07-30 0.0169 2014-07-31 -0.2030
np.array
input: array([[ 223.57 , 226.5 , 15.32 , ..., 589.02 , 21.349, 97.67 ],
[ 224.82 , 225.8 , 16.13 , ..., 590.6 , 21.548, 99.02 ], [ 225.01 , 220. , 16.75 , ..., 585.61 , 21.19 , 98.38 ], ..., [ 222.27 , 160.88 , 25.5 , ..., 742.74 , 13.51 , 98.66 ], [ 230.01 , 160.25 , 25.57 , ..., 739.77 , 13.39 , 97.34 ], [ 225.93 , 163.09 , 24.75 , ..., 740.92 , 13.655, 97.76 ]])
output:
- array([[ 0.2497, 0.264 , -0.7675, ..., 2.0348, -0.738 , -0.3652],
- [ 0.2558, 0.2606, -0.7635, ..., 2.0425, -0.7371, -0.3587], [ 0.2568, 0.2323, -0.7605, ..., 2.0181, -0.7388, -0.3618], ..., [ 0.2434, -0.0565, -0.7178, ..., 2.7857, -0.7763, -0.3604], [ 0.2812, -0.0596, -0.7174, ..., 2.7712, -0.7769, -0.3669], [ 0.2612, -0.0457, -0.7214, ..., 2.7768, -0.7756, -0.3648]])
参数: group – pd.DataFrame对象, pd.Series对象, np.array对象
-
abupy.UtilBu.ABuScalerUtil.
scaler_xy
(x, y, type_look='look_max', mean_how=True)[源代码]¶ 只针对俩个输入的均值归一化, 取两个序列的平均值或者最大值后,谁的平均值或者最大值大就被认定为是大序列。 根据type_look的值,选择向大序列值看齐,还是小序列值看齐,返回的序列中一个将保持不变,另一个被缩放, 可以被看作是scaler_matrix的特殊情况接口
- eg:
- input x:
- 2014-07-25 223.57 2014-07-28 224.82 2014-07-29 225.01 2014-07-30 228.92 2014-07-31 223.30
- input y:
- 2014-07-25 15.32 2014-07-28 16.13 2014-07-29 16.75 2014-07-30 16.83 2014-07-31 16.06
x, y = ABuScalerUtil.scaler_xy(x, y, type_look=’look_max’, mean_how=False)
- output y:
- 2014-07-25 208.3811 2014-07-28 219.3987 2014-07-29 227.8318 2014-07-30 228.9200 2014-07-31 218.4465
x, y = ABuScalerUtil.scaler_xy(x, y, type_look=’look_max’, mean_how=True)
- output y:
- 2014-07-25 212.6588 2014-07-28 223.9025 2014-07-29 232.5088 2014-07-30 233.6192 2014-07-31 222.9308
x, y = ABuScalerUtil.scaler_xy(x, y, type_look=’look_min’, mean_how=False) output x:
2014-07-25 16.4367 2014-07-28 16.5286 2014-07-29 16.5425 2014-07-30 16.8300 2014-07-31 16.4168x, y = ABuScalerUtil.scaler_xy(x, y, type_look=’look_min’, mean_how=True) output x:
2014-07-25 16.1060 2014-07-28 16.1961 2014-07-29 16.2098 2014-07-30 16.4915 2014-07-31 16.0866
参数: - x – pd.Series对象, np.array对象
- y – pd.Series对象, np.array对象
- type_look – str对象,type_look in (‘look_max’, ‘look_min)
- mean_how – 决定是使用平均值还是最大值来决策序列更大
返回: 缩放后的x,y,pd.Series对象 or np.array
abupy.UtilBu.ABuStatsUtil module¶
统计相关工具模块
-
abupy.UtilBu.ABuStatsUtil.
arr_to_numpy
(func)[源代码]¶ 函数装饰器:定参数装饰器,非通用,通用转换使用ABuDTUtil中的装饰器 将被装饰函数中的arr序列转换为np.array
-
abupy.UtilBu.ABuStatsUtil.
arr_to_pandas
(func)[源代码]¶ 函数装饰器:定参数装饰器,非通用,通用转换使用ABuDTUtil中的装饰器 将被装饰函数中的arr序列转换为pd.DataFrame或者pd.Series
-
abupy.UtilBu.ABuStatsUtil.
cosine_distance_matrix
(df, scale_end=True, to_similar=False)[源代码]¶ 余弦距离: 与cosine_distances_xy的区别主要是,非两两distance计算,只有一个矩阵的输入, 且输入必须为pd.DataFrame or np.array or 多层迭代序列[[],[]],注意需要理解数据的测距目的来分析 是否需要进行scale_start,进行和不进行scale_start的结果将完全不一样,在功能需求及数据理解的情况下 选择是否进行scale_start
- eg:
input:
tsla bidu noah sfun goog vips aapl2014-07-25 223.57 226.50 15.32 12.110 589.02 21.349 97.67 2014-07-28 224.82 225.80 16.13 12.450 590.60 21.548 99.02 2014-07-29 225.01 220.00 16.75 12.220 585.61 21.190 98.38 ... ... ... ... ... ... ... ... 2016-07-22 222.27 160.88 25.50 4.850 742.74 13.510 98.66 2016-07-25 230.01 160.25 25.57 4.790 739.77 13.390 97.34 2016-07-26 225.93 163.09 24.75 4.945 740.92 13.655 97.76
ABuStatsUtil.cosine_distance_matrix(cc, scale_start=True)
output:
tsla bidu noah sfun goog vips aapltsla 0.0000 0.1743 0.4434 0.2945 0.2394 0.4763 0.1266 bidu 0.1743 0.0000 0.5808 0.2385 0.3986 0.3034 0.1470 noah 0.4434 0.5808 0.0000 1.0000 0.3411 0.7626 0.2632 sfun 0.2945 0.2385 1.0000 0.0000 0.7494 0.4448 0.4590 goog 0.2394 0.3986 0.3411 0.7494 0.0000 0.9717 0.2806 vips 0.4763 0.3034 0.7626 0.4448 0.9717 0.0000 0.2669 aapl 0.1266 0.1470 0.2632 0.4590 0.2806 0.2669 0.0000
ABuStatsUtil.cosine_distance_matrix(cc, scale_start=False)
output:
tsla bidu noah sfun goog vips aapltsla 0.0000 0.1743 0.4434 0.2945 0.2394 0.4763 0.1266 bidu 0.1743 0.0000 0.5808 0.2385 0.3986 0.3034 0.1470 noah 0.4434 0.5808 0.0000 1.0000 0.3411 0.7626 0.2632 sfun 0.2945 0.2385 1.0000 0.0000 0.7494 0.4448 0.4590 goog 0.2394 0.3986 0.3411 0.7494 0.0000 0.9717 0.2806 vips 0.4763 0.3034 0.7626 0.4448 0.9717 0.0000 0.2669 aapl 0.1266 0.1470 0.2632 0.4590 0.2806 0.2669 0.0000
参数: - df – pd.DataFrame or np.array or 多层迭代序列[[],[]], 之所以叫df,是因为在内部会统一转换为pd.DataFrame
- scale_end – 对结果矩阵进行标准化处理
- to_similar – 是否进行后置输出转换similar值
返回: distance_df,pd.DataFrame对象
-
abupy.UtilBu.ABuStatsUtil.
cosine_distances_xy
(x, y, to_similar=False)[源代码]¶ 余弦距离计算两个序列distance,注意需要理解数据的测距目的来分析 是否需要进行scale_start,进行和不进行scale_start的结果将完全不一样,在功能需求及数据理解的情况下 选择是否进行scale_start :param x: 可迭代序列 :param y: 可迭代序列 :param to_similar: 是否进行后置输出转换similar值 :return: float数值
-
abupy.UtilBu.ABuStatsUtil.
demean
(arr, rolling_window=0, show=False)[源代码]¶ 去均值化后处理demean, 如果输入的是np.array进行转换为pd.DataFrame处理, 被arr_to_pandas装饰,统一输出,且这样使用arr.mean()不需要axis参数区别np.array轴向 eg:
- cc.head()
- tsla bidu noah sfun goog vips aapl
2014-07-25 223.57 226.50 15.32 12.11 589.02 21.349 97.67 2014-07-28 224.82 225.80 16.13 12.45 590.60 21.548 99.02 2014-07-29 225.01 220.00 16.75 12.22 585.61 21.190 98.38 2014-07-30 228.92 219.13 16.83 11.78 587.42 21.185 98.15 2014-07-31 223.30 216.05 16.06 11.47 571.60 20.550 95.60
ABuStatsUtil.demean(cc.head())
tsla bidu noah sfun goog vips aapl2014-07-25 -1.554 5.004 -0.898 0.104 4.17 0.1846 -0.094 2014-07-28 -0.304 4.304 -0.088 0.444 5.75 0.3836 1.256 2014-07-29 -0.114 -1.496 0.532 0.214 0.76 0.0256 0.616 2014-07-30 3.796 -2.366 0.612 -0.226 2.57 0.0206 0.386 2014-07-31 -1.824 -5.446 -0.158 -0.536 -13.25 -0.6144 -2.164
ABuStatsUtil.demean(cc.head().values)
0 1 2 3 4 5 60 -1.554 5.004 -0.898 0.104 4.17 0.1846 -0.094 1 -0.304 4.304 -0.088 0.444 5.75 0.3836 1.256 2 -0.114 -1.496 0.532 0.214 0.76 0.0256 0.616 3 3.796 -2.366 0.612 -0.226 2.57 0.0206 0.386 4 -1.824 -5.446 -0.158 -0.536 -13.25 -0.6144 -2.164
tsla.head()
2014-07-25 223.57 2014-07-28 224.82 2014-07-29 225.01 2014-07-30 228.92 2014-07-31 223.30
ABuStatsUtil.demean(tsla.head())
2014-07-25 -1.554 2014-07-28 -0.304 2014-07-29 -0.114 2014-07-30 3.796 2014-07-31 -1.824
ABuStatsUtil.demean(tsla.head().values)
00 -1.554 1 -0.304 2 -0.114 3 3.796 4 -1.824
参数: - arr – pd.DataFrame or pd.Series or Iterable
- rolling_window – 默认=0,即不使用移动平均做去均值,rolling_window > 0 生效, 注意如果rolling_window值过小将导致去均值后不连续,比如5日,10日的 结果只能类似close pct_change的结果,如果需求要钝化,最好是两个月以上 的交易日数量,user要根据需求,选择使用的参数,
- show – 是否可视化去均值后的结果,默认False
返回:
-
abupy.UtilBu.ABuStatsUtil.
euclidean_distance_matrix
(df, scale_end=True, to_similar=False)[源代码]¶ 欧式距离(L2范数): 与euclidean_distance_xy的区别主要是,非两两distance计算,只有一个矩阵的输入, 且输入必须为pd.DataFrame or np.array or 多层迭代序列[[],[]], 注意需要理解数据的测距目的来分析 是否需要进行scale_start,进行和不进行scale_start的结果将完全不一样,在功能需求及数据理解的情况下 选择是否进行scale_start
input:
tsla bidu noah sfun goog vips aapl2014-07-25 223.57 226.50 15.32 12.110 589.02 21.349 97.67 2014-07-28 224.82 225.80 16.13 12.450 590.60 21.548 99.02 2014-07-29 225.01 220.00 16.75 12.220 585.61 21.190 98.38 ... ... ... ... ... ... ... ... 2016-07-22 222.27 160.88 25.50 4.850 742.74 13.510 98.66 2016-07-25 230.01 160.25 25.57 4.790 739.77 13.390 97.34 2016-07-26 225.93 163.09 24.75 4.945 740.92 13.655 97.76
ABuStatsUtil.euclidean_distance_matrix(cc, scale_start=True)
output:
tsla bidu noah sfun goog vips aapltsla 0.0000 0.4086 0.7539 0.7942 0.4810 0.7638 0.3713 bidu 0.4086 0.0000 0.7732 0.7047 0.6185 0.6161 0.4184 noah 0.7539 0.7732 0.0000 0.7790 0.7174 0.6957 0.7425 sfun 0.7942 0.7047 0.7790 0.0000 0.9950 0.5422 0.9558 goog 0.4810 0.6185 0.7174 0.9950 0.0000 1.0000 0.5379 vips 0.7638 0.6161 0.6957 0.5422 1.0000 0.0000 0.7348 aapl 0.3713 0.4184 0.7425 0.9558 0.5379 0.7348 0.0000
ABuStatsUtil.euclidean_distance_matrix(cc, scale_start=False)
tsla bidu noah sfun goog vips aapltsla 0.0000 0.0781 0.3314 0.3573 0.6527 0.3386 0.1933 bidu 0.0781 0.0000 0.2764 0.3018 0.7112 0.2827 0.1392 noah 0.3314 0.2764 0.0000 0.0284 0.9732 0.0140 0.1408 sfun 0.3573 0.3018 0.0284 0.0000 1.0000 0.0203 0.1674 goog 0.6527 0.7112 0.9732 1.0000 0.0000 0.9820 0.8369 vips 0.3386 0.2827 0.0140 0.0203 0.9820 0.0000 0.1481 aapl 0.1933 0.1392 0.1408 0.1674 0.8369 0.1481 0.0000
参数: - df – pd.DataFrame or np.array or 多层迭代序列[[],[]], 之所以叫df,是因为在内部会统一转换为pd.DataFrame
- scale_end – 对结果矩阵进行标准化处理
- to_similar – 是否进行后置输出转换similar值
返回: distance_df,pd.DataFrame对象
-
abupy.UtilBu.ABuStatsUtil.
euclidean_distance_xy
(x, y, to_similar=False)[源代码]¶ 欧式距离(L2范数)计算两个序列distance, g_euclidean_safe控制是否使用euclidean_distances计算 还是使用la.norm,效率差别如下:
euclidean_distances: 10000 loops, best of 3: 128 µs per loop la.norm : 10000 loops, best of 3: 89.6 µs per loop在需要批量且很耗时的情况下切好模式,否则不要切换 :param x: 可迭代序列 :param y: 可迭代序列 :param to_similar: 是否进行后置输出转换similar值 :return: float数值
-
abupy.UtilBu.ABuStatsUtil.
manhattan_distance_matrix
(df, scale_end=True, to_similar=False)[源代码]¶ 曼哈顿距离(L1范数): 与manhattan_distances_xy的区别主要是,非两两distance计算,只有一个矩阵的输入, 且输入必须为pd.DataFrame or np.array or 多层迭代序列[[],[]],注意需要理解数据的测距目的来分析 是否需要进行scale_start,进行和不进行scale_start的结果将完全不一样,在功能需求及数据理解的情况下 选择是否进行scale_start
- eg:
input:
tsla bidu noah sfun goog vips aapl2014-07-25 223.57 226.50 15.32 12.110 589.02 21.349 97.67 2014-07-28 224.82 225.80 16.13 12.450 590.60 21.548 99.02 2014-07-29 225.01 220.00 16.75 12.220 585.61 21.190 98.38 ... ... ... ... ... ... ... ... 2016-07-22 222.27 160.88 25.50 4.850 742.74 13.510 98.66 2016-07-25 230.01 160.25 25.57 4.790 739.77 13.390 97.34 2016-07-26 225.93 163.09 24.75 4.945 740.92 13.655 97.76
ABuStatsUtil.manhattan_distance_matrix(cc, scale_start=True)
output:
tsla bidu noah sfun goog vips aapltsla 0.0000 0.3698 0.6452 0.7917 0.4670 0.7426 0.3198 bidu 0.3698 0.0000 0.5969 0.7056 0.6495 0.5822 0.4000 noah 0.6452 0.5969 0.0000 0.7422 0.7441 0.6913 0.6896 sfun 0.7917 0.7056 0.7422 0.0000 0.9236 0.4489 1.0000 goog 0.4670 0.6495 0.7441 0.9236 0.0000 0.8925 0.5134 vips 0.7426 0.5822 0.6913 0.4489 0.8925 0.0000 0.7038 aapl 0.3198 0.4000 0.6896 1.0000 0.5134 0.7038 0.0000
ABuStatsUtil.manhattan_distance_matrix(cc, scale_start=False)
output:
tsla bidu noah sfun goog vips aapltsla 0.0000 0.0640 0.3318 0.3585 0.6415 0.3395 0.1906 bidu 0.0640 0.0000 0.2750 0.3018 0.6982 0.2827 0.1338 noah 0.3318 0.2750 0.0000 0.0267 0.9733 0.0124 0.1412 sfun 0.3585 0.3018 0.0267 0.0000 1.0000 0.0191 0.1680 goog 0.6415 0.6982 0.9733 1.0000 0.0000 0.9809 0.8320 vips 0.3395 0.2827 0.0124 0.0191 0.9809 0.0000 0.1489 aapl 0.1906 0.1338 0.1412 0.1680 0.8320 0.1489 0.000
参数: - df – pd.DataFrame or np.array or 多层迭代序列[[],[]], 之所以叫df,是因为在内部会统一转换为pd.DataFrame
- scale_end – 对结果矩阵进行标准化处理
- to_similar – 是否进行后置输出转换similar值
返回: distance_df,pd.DataFrame对象
-
abupy.UtilBu.ABuStatsUtil.
manhattan_distances_xy
(x, y, to_similar=False)[源代码]¶ 曼哈顿距离(L1范数)计算两个序列distance,注意需要理解数据的测距目的来分析 是否需要进行scale_start,进行和不进行scale_start的结果将完全不一样,在功能需求及数据理解的情况下 选择是否进行scale_start :param x: 可迭代序列 :param y: 可迭代序列 :param to_similar: 是否进行后置输出转换similar值 :return: float数值
-
abupy.UtilBu.ABuStatsUtil.
normality_stats
(arr)[源代码]¶ - 统计信息偏度,峰度,正态分布检测,p-value
- eg:
input:
2014-07-25 223.57 2014-07-28 224.82 2014-07-29 225.01
...2016-07-22 222.27 2016-07-25 230.01 2016-07-26 225.93
output:
array skew = -0.282635248604699 array skew p-value = 0.009884539532576725 array kurt = 0.009313464006726946 array kurt p-value = 0.8403947352953821 array norm = NormaltestResult(statistic=6.6961445106692237, pvalue=0.035152053009441256) array norm p-value = 0.035152053009441256
input:
tsla bidu noah sfun goog vips aapl2014-07-25 223.57 226.50 15.32 12.110 589.02 21.349 97.67 2014-07-28 224.82 225.80 16.13 12.450 590.60 21.548 99.02 2014-07-29 225.01 220.00 16.75 12.220 585.61 21.190 98.38 ... ... ... ... ... ... ... ... 2016-07-22 222.27 160.88 25.50 4.850 742.74 13.510 98.66 2016-07-25 230.01 160.25 25.57 4.790 739.77 13.390 97.34 2016-07-26 225.93 163.09 24.75 4.945 740.92 13.655 97.76
output:
array skew = [-0.2826 -0.2544 0.1456 1.0322 0.2095 0.095 0.1719] array skew p-value = [ 0.0099 0.0198 0.1779 0. 0.0539 0.3781 0.1124] array kurt = [ 0.0093 -0.8414 -0.4205 0.4802 -1.547 -0.9203 -1.2104] array kurt p-value = [ 0.8404 0. 0.0201 0.0461 1. 0. 0. ] array norm = NormaltestResult(statistic=array([ 6.6961, 52.85 , 7.2163, 69.0119, 3.7161, 69.3468, 347.229 ]), pvalue=array([ 0.0352, 0. , 0.0271, 0. , 0.156 , 0. , 0. ])) array norm p-value = [ 0.0352 0. 0.0271 0. 0.156 0. 0. ]
参数: arr – pd.DataFrame or pd.Series or Iterable
-
abupy.UtilBu.ABuStatsUtil.
print_stats
(arr)[源代码]¶ - 对arr的统计相关信息输出
- eg:
input:
tsla bidu noah sfun goog vips aapl2014-07-25 223.57 226.50 15.32 12.110 589.02 21.349 97.67 2014-07-28 224.82 225.80 16.13 12.450 590.60 21.548 99.02 2014-07-29 225.01 220.00 16.75 12.220 585.61 21.190 98.38 ... ... ... ... ... ... ... ... 2016-07-22 222.27 160.88 25.50 4.850 742.74 13.510 98.66 2016-07-25 230.01 160.25 25.57 4.790 739.77 13.390 97.34 2016-07-26 225.93 163.09 24.75 4.945 740.92 13.655 97.76
output:
array size = 504 array min = [ 143.67 132.37 12.95 4.44 492.55 10.35 90.34] array max = [ 286.04 250.34 37.32 12.52 776.6 30. 133. ] array mean = [ 228.4885 193.4488 23.7362 7.2458 624.3016 19.0181 110.881 ] array var = [ 653.509 752.7421 30.1604 3.3109 7310.2084 27.0994
135.156 ]array std = [ 25.5638 27.4361 5.4919 1.8196 85.4998 5.2057 11.6257] array skew = [-0.2826 -0.2544 0.1456 1.0322 0.2095 0.095 0.1719] array kurt = [ 0.0093 -0.8414 -0.4205 0.4802 -1.547 -0.9203 -1.2104]
参数: arr – pd.DataFrame or pd.Series or Iterable
-
abupy.UtilBu.ABuStatsUtil.
print_stats_sm
(arr)[源代码]¶ 对arr的统计相关信息输出,并且分别输出数据的实体前面,中间,最后5列, 被arr_to_numpy装饰, 统一输出,且这样使用arr.max(), arr.min()等不需要axis参数区别
- eg:
input:
tsla bidu noah sfun goog vips aapl2014-07-25 223.57 226.50 15.32 12.110 589.02 21.349 97.67 2014-07-28 224.82 225.80 16.13 12.450 590.60 21.548 99.02 2014-07-29 225.01 220.00 16.75 12.220 585.61 21.190 98.38 ... ... ... ... ... ... ... ... 2016-07-22 222.27 160.88 25.50 4.850 742.74 13.510 98.66 2016-07-25 230.01 160.25 25.57 4.790 739.77 13.390 97.34 2016-07-26 225.93 163.09 24.75 4.945 740.92 13.655 97.76
output:
head = [[ 223.57 226.5 15.32 12.11 589.02 21.349 97.67 ]
[ 224.82 225.8 16.13 12.45 590.6 21.548 99.02 ] [ 225.01 220. 16.75 12.22 585.61 21.19 98.38 ] [ 228.92 219.13 16.83 11.78 587.42 21.185 98.15 ] [ 223.3 216.05 16.06 11.47 571.6 20.55 95.6 ]]mid = [[ 267.2 207.63 26.25 7.44 644.28 21.23 125.16]
[ 265.41 206.25 23.4 7.17 623.56 20.25 124.5 ] [ 253.01 197.68 22.16 6.76 627.26 19.99 122.77] [ 264.82 168.03 22.94 6.76 628. 20.2 123.38] [ 263.82 170.01 23.35 6.79 631.93 20.26 122.99]]tail = [[ 228.36 160.65 25.11 4.9 741.19 13.63 99.96 ]
[ 220.5 161.4 25.51 4.79 738.63 13.69 99.43 ] [ 222.27 160.88 25.5 4.85 742.74 13.51 98.66 ] [ 230.01 160.25 25.57 4.79 739.77 13.39 97.34 ] [ 225.93 163.09 24.75 4.945 740.92 13.655 97.76 ]]
参数: arr –
-
abupy.UtilBu.ABuStatsUtil.
stats_dict
(arr)[源代码]¶ 通过序列构造arr的基础统计信息dict,被arr_to_numpy装饰,统一输出,且这样使用arr.max(), arr.min()等不需要axis参数区别 :param arr: pd.DataFrame or pd.Series or Iterable :return: stats_dict,dict对象
- eg:
- {‘count’: 504,
- ‘kurtosis’: 0.009313464006726946, ‘max’: 286.04000000000002, ‘mean’: 228.48845238095237, ‘min’: 143.66999999999999, ‘skewness’: -0.282635248604699, ‘std’: 25.538448192811927}
-
abupy.UtilBu.ABuStatsUtil.
stats_namedtuple
(arr)[源代码]¶ 通过序列构造arr的基础统计信息dict, 被arr_to_numpy装饰,统一输出,且这样使用arr.max(), arr.min()等不需要axis参数区别 与stats_dict的区别只是返回namedtuple对象 :param arr: pd.DataFrame or pd.Series or Iterable :return: AbuMomentsTuple对象
- eg:
- count:504 max:286.04 min:143.67 mean:228.48845238095237 std:25.538448192811927 skewness:-0.282635248604699 kurtosis:0.009313464006726946
abupy.UtilBu.ABuStrUtil module¶
字符工具模块
-
abupy.UtilBu.ABuStrUtil.
create_random_with_alpha
(salt_count)[源代码]¶ 种子字符池 = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”, 从种子字符池中随机抽取salt_count个字符, 返回生成字符串, :param salt_count: 生成的字符序列的长度 :return: 返回生成字符串
-
abupy.UtilBu.ABuStrUtil.
create_random_with_num
(salt_count)[源代码]¶ 种子字符池 = “0123456789”, 从种子字符池中随机抽取salt_count个字符, 返回生成字符串, :param salt_count: 生成的字符序列的长度 :return: 返回生成字符串
-
abupy.UtilBu.ABuStrUtil.
create_random_with_num_low
(salt_count)[源代码]¶ 种子字符池 = “abcdefghijklmnopqrstuvwxyz0123456789”, 从种子字符池中随机抽取salt_count个字符, 返回生成字符串, :param salt_count: 生成的字符序列的长度 :return: 返回生成字符串
-
abupy.UtilBu.ABuStrUtil.
digit_str
(item)[源代码]¶ 从第一个字符开始删除,直到所有字符都是数字为止,或者item长度 < 2 eg:
input: ABuStrUtil.digit_str(‘sh000001’) output: 000001
input: ABuStrUtil.digit_str(‘shszsh000001’) output: 000001
参数: item – 字符串对象 返回: 过滤head字母的字符串对象
-
abupy.UtilBu.ABuStrUtil.
to_bytes
(text, encoding=None, errors='strict')[源代码]¶ 在中to_native_str对py2生效,对bytes直接返回,其它的encode,默认utf-8
-
abupy.UtilBu.ABuStrUtil.
to_native_str
(text, encoding=None, errors='strict')[源代码]¶ 套接to_unicode和to_bytes针对python版本不同处理
abupy.UtilBu.ABuWinUtil module¶
Module contents¶
-
class
abupy.UtilBu.
AbuProgress
(total, a_progress, label=None)[源代码]¶ Bases:
object
单进程(主进程)进度显示控制类
-
progress
¶ property获取self._progress
-
-
class
abupy.UtilBu.
AbuBlockProgress
(label, interval=1, max_step=20)[源代码]¶ Bases:
object
主进程阻塞任务,启动子单进程任务进度显示控制类