Skip to content

Instantly share code, notes, and snippets.

@mohannishant6
Last active October 4, 2020 11:28
Show Gist options
  • Select an option

  • Save mohannishant6/188f0e8cffeab1f1a4c3ccbb32a94cea to your computer and use it in GitHub Desktop.

Select an option

Save mohannishant6/188f0e8cffeab1f1a4c3ccbb32a94cea to your computer and use it in GitHub Desktop.
random guessing log loss
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import log_loss\n",
"\n",
"def calculate_log_loss(class_ratio,multi=10000):\n",
" \n",
" if sum(class_ratio)!=1.0:\n",
" print(\"warning: Sum of ratios should be 1 for best results\")\n",
" class_ratio[-1]+=1-sum(class_ratio) # add the residual to last class's ratio\n",
" \n",
" actuals=[]\n",
" for i,val in enumerate(class_ratio):\n",
" actuals=actuals+[i for x in range(int(val*multi))]\n",
" \n",
"\n",
" preds=[]\n",
" for i in range(multi):\n",
" preds+=[class_ratio]\n",
"\n",
" return (log_loss(actuals, preds))\n"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6931471805599453"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"logloss=calculate_log_loss([.5,.5],multi=100)\n",
"logloss"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6108643020548936"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"logloss=calculate_log_loss([.3,.7],multi=100)\n",
"logloss"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9985793315873923"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"logloss=calculate_log_loss([.15,.35,.5],multi=100)\n",
"logloss"
]
}
],
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment