zoukankan      html  css  js  c++  java
  • numpy中dtype

    简单说明dtype使用方法:

    Rule为规则类,其中有3个字段,1为类型,2为计算规则,3为保留位数

    如 :'close': Rule(float64, 1 / 10000.0, 2),

    表示收盘价,Rule有三个字段,首为浮点类型,次为计算规则,末为保留小数位数

    Converter类则是Rule字典集合,这里表示的为open,close等为key的集合

    # /usr/bin/python3
    
    # -*- encoding: utf-8 -*-
    
    from collections import namedtuple
    
    import numpy as np
    
    float64 = np.dtype('float64')
    
    Rule = namedtuple('Rule', ['dtype', 'multiplier', 'round'])
    
    class Converter(object):
    
        def __init__(self, rules):
    
            self._rules = rules
    
        def convert(self, name, data):
    
            try:
    
                r = self._rules[name]
    
            except KeyError:
    
                return data
    
            result = data * r.multiplier
    
            if r.round:
    
                result = np.round(result, r.round)
    
            return result
    
        def field_type(self, name):
    
            try:
    
                return self._rules[name].dtype
    
            except KeyError:
    
                return self._rules['open'].dtype
    
                  
    
    if __name__ == '__main__':
    
        
    
        StockBarConverter = Converter({
    
            'open': Rule(float64, 1 / 10000.0, 4),
    
            'close': Rule(float64, 1 / 10000.0, 2),
    
            'high': Rule(float64, 1 / 10000.0, 2),
    
            'low': Rule(float64, 1 / 10000.0, 2),
    
            'limit_up': Rule(float64, 1/10000.0, 2),
    
            'limit_down': Rule(float64, 1/10000.0, 2),
    
            'volume': Rule(float64, 1, 0),
    
        })
    
        _converter = StockBarConverter
    
        data = np.array([1001, 2103])
    
        result = _converter.convert('open', data)
    
        open_type = _converter.field_type("open")
    
        print("open_type: ", open_type) #open_type:  float64
  • 相关阅读:
    程序员掌握的单词汇总
    Django--基础补充
    C#入门概述
    python常用模块
    软件开发经验之谈
    p7.BTC-挖矿总结
    p6.BTC-挖矿难度
    p5.BTC-网络
    p4.BTC-实现
    p3.BTC-协议
  • 原文地址:https://www.cnblogs.com/luhouxiang/p/7401859.html
Copyright © 2011-2022 走看看