Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Last active January 4, 2016 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miraculixx/24c11a60550dcc009f44 to your computer and use it in GitHub Desktop.
Save miraculixx/24c11a60550dcc009f44 to your computer and use it in GitHub Desktop.
Python implementation of the sigmoid to map a point value to a rating (where points are milliseconds and the rating is the resulting score).
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Untitled0"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "%matplotlib\n\"\"\"\nin reference to http://programmers.stackexchange.com/questions/225487/mapping-values-over-a-curve/225503#225503\n\"\"\"\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport math\n\nMIN_RATING=1\nMAX_RATING=2000\n\nMIN_POINTS=1\nMAX_POINTS=8000\n\nSIGMOID_RANGE=25\n\ndef sigmoid(x):\n return 1 / (1 + math.exp(-x))\n\ndef map2range(value, low, high, new_low, new_high):\n '''map a value from one range to another'''\n return value * 1.0 / (high - low + 1) * (new_high - new_low + 1)\n\ndef map2sigmoid(value, max=SIGMOID_RANGE):\n '''map a value from its rating range to the sigmoid range'''\n return map2range(value, MIN_RATING, MAX_RATING, 1.0, max) - (max / 2.0) \n\ndef map2rating(value, max=SIGMOID_RANGE):\n '''map a points value to a rating value using the sigmoid function'''\n rating_range = map2range(value, MIN_POINTS, MAX_POINTS, MIN_RATING, MAX_RATING)\n sigmoid_range = map2sigmoid(rating_range, max=max)\n s = sigmoid(sigmoid_range) \n if s < 0.5:\n r = math.floor(s * MAX_RATING)\n else:\n r = math.ceil(s * MAX_RATING)\n return r\n\nax = plt.subplot(1,1,1)\nx = np.arange(MIN_RATING,MAX_RATING,1);\ny = [sigmoid(map2sigmoid(y, max=10)) for y in x]\nax.plot(x, y, label=\"range=10\")\ny = [sigmoid(map2sigmoid(y, max=20)) for y in x]\nax.plot(x, y, label=\"range=20\")\ny = [sigmoid(map2sigmoid(y, max=30)) for y in x]\nax.plot(x, y, label=\"range=30\")\n\nhandles, labels = ax.get_legend_handles_labels()\nax.legend(handles, labels, loc=2)\n\nprint \"%5s %7s %7s\" % (\"ms\", \"r=6\", \"r=20\")\nfor points in range(0, 8001, 500):\n print \"%5d %7.0f %7.0f\" % (points, map2rating(points,max=6), map2rating(points, max=20))\n\n\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Using matplotlib backend: MacOSX\n ms r=6 r=20"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "\n 0 94 0\n 500 135 0\n 1000 190 1\n 1500 265 3\n 2000 364 13\n 2500 490 45\n 3000 641 151\n 3500 814 445\n 4000 1000 1000\n 4500 1186 1555\n 5000 1359 1849\n 5500 1510 1955\n 6000 1636 1987\n 6500 1735 1997\n 7000 1810 1999\n 7500 1865 2000\n 8000 1906 2000\n"
}
],
"prompt_number": 194
}
],
"metadata": {}
}
]
}
%matplotlib
"""
in reference to http://programmers.stackexchange.com/questions/225487/mapping-values-over-a-curve/225503#225503
"""
import numpy as np
import matplotlib.pyplot as plt
import math
MIN_RATING=1
MAX_RATING=2000
MIN_POINTS=1
MAX_POINTS=8000
SIGMOID_RANGE=25
def sigmoid(x):
return 1 / (1 + math.exp(-x))
def map2range(value, low, high, new_low, new_high):
'''map a value from one range to another'''
return value * 1.0 / (high - low + 1) * (new_high - new_low + 1)
def map2sigmoid(value, max=SIGMOID_RANGE):
'''map a value from its rating range to the sigmoid range'''
return map2range(value, MIN_RATING, MAX_RATING, 1.0, max) - (max / 2.0)
def map2rating(value, max=SIGMOID_RANGE):
'''map a points value to a rating value using the sigmoid function'''
rating_range = map2range(value, MIN_POINTS, MAX_POINTS, MIN_RATING, MAX_RATING)
sigmoid_range = map2sigmoid(rating_range, max=max)
s = sigmoid(sigmoid_range)
if s < 0.5:
r = math.floor(s * MAX_RATING)
else:
r = math.ceil(s * MAX_RATING)
return r
ax = plt.subplot(1,1,1)
x = np.arange(MIN_RATING,MAX_RATING,1);
y = [sigmoid(map2sigmoid(y, max=10)) for y in x]
ax.plot(x, y, label="range=10")
y = [sigmoid(map2sigmoid(y, max=20)) for y in x]
ax.plot(x, y, label="range=20")
y = [sigmoid(map2sigmoid(y, max=30)) for y in x]
ax.plot(x, y, label="range=30")
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, labels, loc=2)
print "%5s %7s %7s" % ("ms", "r=6", "r=20")
for points in range(0, 8001, 500):
print "%5d %7.0f %7.0f" % (points, map2rating(points,max=6), map2rating(points, max=20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment