Coder Social home page Coder Social logo

discretizer's Introduction

Discretizer

A Python library to discretize floating point numbers into compact bytes using variable sized buckets. Sigmoid and cube-root mapping functions can be used to efficiently discretize a number range with high resolution about a specific number, and less resolution where it is not needed. A linear function can be used for even distributions. Clamping occurs for numbers outside the chosen range.

Usage in Python

Using the linear mapping function to discretize some numbers:

from discretizer import LinearDiscretizer

# create a linear discretizer for the range [-10, 20] into 1 byte
d = LinearDiscretizer(1, -10.0, 20.0)

# discretize some numbers into bytearrays
nums = [-11.0, -10.0, -5.0, 0.0, 15.0, 19.0, 20.0, 21.0]
ba_list = []
for n in nums:
    ba_list.append(d.encode(n))

# covert back to numbers and compare
#  (differences exist due to discretization)
for i in range(len(nums)):
    n = d.decode(ba_list[i])
    print('Original = %f, After discretization = %f, Difference = %f' % \
        (nums[i], n, nums[i]-n))

Using the sigmoid mapping function to provide greater accuracy about the middle of the range:

from discretizer import SigmoidDiscretizer

# create a sigmoid discretizer for the range [-5, 5] into 1 byte
#  using a sharpness parameter of 20
d = SigmoidDiscretizer(1, -5.0, 5.0, 20.0)

# discretize some numbers into bytearrays
nums = [-5.0, -4.0, -1.0, -0.5, -0.25, -0.1, 0.0, 0.1, 0.25, 0.5, 1.0, 4.0, 5.0]
ba_list = []
for n in nums:
    ba_list.append(d.encode(n))

# covert back to numbers and compare
#  (differences exist due to discretization)
for i in range(len(nums)):
    n = d.decode(ba_list[i])
    print('Original = %f, After discretization = %f, Difference = %f' % \
        (nums[i], n, nums[i]-n))

discretizer's People

Contributors

jarrodsinclair avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.