Skip to content

Instantly share code, notes, and snippets.

@paulochf
Created November 28, 2017 19:40
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 paulochf/e3eaf29ba04a4802cf96ae94dfc7a22f to your computer and use it in GitHub Desktop.
Save paulochf/e3eaf29ba04a4802cf96ae94dfc7a22f to your computer and use it in GitHub Desktop.
Numpy types ordering regarding their maximum values
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I'm doing some type optimization and it raised me a question: how int and float types can be ordered with respect to their maximum values?"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-28T19:37:43.966738Z",
"start_time": "2017-11-28T19:37:43.825930Z"
}
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From [this numpy doc](https://docs.scipy.org/doc/numpy-1.10.1/reference/arrays.scalars.html), I can reach the following types:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-28T19:37:44.314271Z",
"start_time": "2017-11-28T19:37:43.968766Z"
}
},
"outputs": [],
"source": [
"int_types = [np.int8, np.int16, np.int32, np.int64]\n",
"float_types = [np.float16, np.float32, np.float64]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using numpy's [iinfo](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.iinfo.html) and [iinfo](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.finfo.html), I can get their maximum values."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-28T19:37:44.417571Z",
"start_time": "2017-11-28T19:37:44.325515Z"
}
},
"outputs": [],
"source": [
"int_maxes = map(lambda x: np.iinfo(x).max, int_types)\n",
"float_maxes = map(lambda x: np.finfo(x).max, float_types)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Putting all together:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-28T19:37:44.526684Z",
"start_time": "2017-11-28T19:37:44.427252Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(numpy.int8, 127),\n",
" (numpy.int16, 32767),\n",
" (numpy.int32, 2147483647),\n",
" (numpy.int64, 9223372036854775807),\n",
" (numpy.float16, 65504.0),\n",
" (numpy.float32, 3.4028235e+38),\n",
" (numpy.float64, 1.7976931348623157e+308)]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"maxes = list(zip(int_types, int_maxes)) + list(zip(float_types, float_maxes))\n",
"maxes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And sorting (biggest to smallest):"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2017-11-28T19:37:44.647072Z",
"start_time": "2017-11-28T19:37:44.531876Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(numpy.float64, 1.7976931348623157e+308),\n",
" (numpy.float32, 3.4028235e+38),\n",
" (numpy.int64, 9223372036854775807),\n",
" (numpy.int32, 2147483647),\n",
" (numpy.float16, 65504.0),\n",
" (numpy.int16, 32767),\n",
" (numpy.int8, 127)]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(maxes, key=lambda x: x[1], reverse=True)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"toc_cell": false,
"toc_position": {},
"toc_section_display": "block",
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment