Skip to content

Instantly share code, notes, and snippets.

@rbiswas4
Created May 22, 2018 17:40
Show Gist options
  • Save rbiswas4/4169c1147ad740c0ec274a788f1ee681 to your computer and use it in GitHub Desktop.
Save rbiswas4/4169c1147ad740c0ec274a788f1ee681 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "truths = np.array([0, 0, 1, 1, 0], dtype=np.int)\npredictions = np.ones(shape=(5,2), dtype=np.float)",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "predictions[:, 1] = 0\npredictions[[2,3], 0] = 0\npredictions[[2,3], 1] = 1",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "predictions",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "array([[1., 0.],\n [1., 0.],\n [0., 1.],\n [0., 1.],\n [1., 0.]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import proclam",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "D = proclam.metrics.LogLoss()",
"execution_count": 6,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "D.evaluate(predictions, truths)",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "2.220446049250313e-16"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "D.evaluate(predictions, truths, averaging='per_class')",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "2.220446049250313e-16"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Since all predictions are correct, we should expect the logloss to be\n\n5 * log(p=1) = 0"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "predictions",
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "array([[1.00000000e+00, 2.22044605e-16],\n [1.00000000e+00, 2.22044605e-16],\n [2.22044605e-16, 1.00000000e+00],\n [2.22044605e-16, 1.00000000e+00],\n [1.00000000e+00, 2.22044605e-16]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Assuming this was clipped so that the probabilities don't add up to 1.0 but 1.0 + 2.22e-16, \nThis would be -5 * log(1-2.2e-16)/ 5 = 2.2e-16 . "
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "newpredictions = np.ones(shape=(5,2), dtype=np.float)",
"execution_count": 10,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "newpredictions[:, 1] = 0",
"execution_count": 11,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "D.evaluate(newpredictions, truths, averaging='per_class')",
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 21,
"data": {
"text/plain": "18.021826694558577"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Now we have two incorrect ones, so I would expect \n(- 3* log(1)/3. - 2*log(2.2e-16)/2. -5* 2.2e-16) / 5"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "2 * 16 *np.log(2.2) /2.",
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 26,
"data": {
"text/plain": "12.615317765828324"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.5",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment