Skip to content

Instantly share code, notes, and snippets.

@kmike
Created May 24, 2017 16:24
Show Gist options
  • Save kmike/5f547089b62f85db79169f6499bfbc61 to your computer and use it in GitHub Desktop.
Save kmike/5f547089b62f85db79169f6499bfbc61 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <style>\n",
" table.eli5-weights tr:hover {\n",
" filter: brightness(85%);\n",
" }\n",
"</style>\n",
"\n",
"\n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" <p style=\"margin-bottom: 0.5em; margin-top: 0em\">\n",
" <b>\n",
" \n",
" y=alt.atheism\n",
" \n",
"</b>\n",
"\n",
" \n",
" (score <b>-0.226</b>)\n",
"\n",
"top features\n",
" </p>\n",
" \n",
" <table class=\"eli5-weights\"\n",
" style=\"border-collapse: collapse; border: none; margin-top: 0em; table-layout: auto; margin-bottom: 2em;\">\n",
" <thead>\n",
" <tr style=\"border: none;\">\n",
" \n",
" <th style=\"padding: 0 1em 0 0.5em; text-align: right; border: none;\" title=\"Feature contribution already accounts for the feature value (for linear models, contribution = weight * feature value), and the sum of feature contributions is equal to the score or, for some classifiers, to the probability. Feature values are shown if &quot;show_feature_values&quot; is True.\">\n",
" Contribution<sup>?</sup>\n",
" </th>\n",
" \n",
" <th style=\"padding: 0 0.5em 0 0.5em; text-align: left; border: none;\">Feature</th>\n",
" \n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" \n",
" <tr style=\"background-color: hsl(120, 100.00%, 83.95%); border: none;\">\n",
" <td style=\"padding: 0 1em 0 0.5em; text-align: right; border: none;\">\n",
" +0.611\n",
" </td>\n",
" <td style=\"padding: 0 0.5em 0 0.5em; text-align: left; border: none;\">\n",
" Highlighted in text (sum)\n",
" </td>\n",
" \n",
"</tr>\n",
" \n",
" \n",
"\n",
" \n",
" \n",
" <tr style=\"background-color: hsl(0, 100.00%, 80.00%); border: none;\">\n",
" <td style=\"padding: 0 1em 0 0.5em; text-align: right; border: none;\">\n",
" -0.837\n",
" </td>\n",
" <td style=\"padding: 0 0.5em 0 0.5em; text-align: left; border: none;\">\n",
" &lt;BIAS&gt;\n",
" </td>\n",
" \n",
"</tr>\n",
" \n",
"\n",
" </tbody>\n",
" </table>\n",
"\n",
" \n",
"\n",
"\n",
"\n",
" <p style=\"margin-bottom: 2.5em; margin-top:-0.5em;\">\n",
" <span style=\"opacity: 0.80\">\n",
"</span><span style=\"background-color: hsl(120, 100.00%, 72.79%); opacity: 0.92\" title=\"0.045\">don</span><span style=\"opacity: 0.80\">'t be so </span><span style=\"background-color: hsl(0, 100.00%, 91.49%); opacity: 0.82\" title=\"-0.008\">sure</span><span style=\"opacity: 0.80\">. </span><span style=\"background-color: hsl(0, 100.00%, 93.45%); opacity: 0.82\" title=\"-0.006\">look</span><span style=\"opacity: 0.80\"> what </span><span style=\"background-color: hsl(0, 100.00%, 91.11%); opacity: 0.82\" title=\"-0.009\">happened</span><span style=\"opacity: 0.80\"> to </span><span style=\"background-color: hsl(120, 100.00%, 93.19%); opacity: 0.82\" title=\"0.006\">japanese</span><span style=\"opacity: 0.80\"> </span><span style=\"background-color: hsl(120, 100.00%, 84.14%); opacity: 0.85\" title=\"0.021\">citizens</span><span style=\"opacity: 0.80\"> in the us during\n",
"</span><span style=\"background-color: hsl(120, 100.00%, 88.80%); opacity: 0.83\" title=\"0.013\">world</span><span style=\"opacity: 0.80\"> </span><span style=\"background-color: hsl(120, 100.00%, 79.32%); opacity: 0.88\" title=\"0.030\">war</span><span style=\"opacity: 0.80\"> </span><span style=\"background-color: hsl(120, 100.00%, 88.36%); opacity: 0.83\" title=\"0.013\">ii</span><span style=\"opacity: 0.80\">. if you're </span><span style=\"background-color: hsl(120, 100.00%, 82.52%); opacity: 0.86\" title=\"0.024\">prepared</span><span style=\"opacity: 0.80\"> to </span><span style=\"background-color: hsl(120, 100.00%, 75.89%); opacity: 0.90\" title=\"0.038\">say</span><span style=\"opacity: 0.80\"> &quot;</span><span style=\"background-color: hsl(120, 100.00%, 93.52%); opacity: 0.81\" title=\"0.006\">let</span><span style=\"opacity: 0.80\">'s </span><span style=\"background-color: hsl(120, 100.00%, 91.64%); opacity: 0.82\" title=\"0.008\">round</span><span style=\"opacity: 0.80\"> these </span><span style=\"background-color: hsl(120, 100.00%, 75.89%); opacity: 0.90\" title=\"0.038\">people</span><span style=\"opacity: 0.80\"> up and\n",
"</span><span style=\"background-color: hsl(120, 100.00%, 71.75%); opacity: 0.92\" title=\"0.047\">stick</span><span style=\"opacity: 0.80\"> them in a </span><span style=\"background-color: hsl(120, 100.00%, 87.78%); opacity: 0.84\" title=\"0.014\">concentration</span><span style=\"opacity: 0.80\"> </span><span style=\"background-color: hsl(120, 100.00%, 91.58%); opacity: 0.82\" title=\"0.008\">camp</span><span style=\"opacity: 0.80\"> without </span><span style=\"background-color: hsl(120, 100.00%, 85.84%); opacity: 0.85\" title=\"0.018\">trial</span><span style=\"opacity: 0.80\">&quot;, it's only a </span><span style=\"background-color: hsl(0, 100.00%, 82.92%); opacity: 0.86\" title=\"-0.023\">short</span><span style=\"opacity: 0.80\"> </span><span style=\"background-color: hsl(0, 100.00%, 86.99%); opacity: 0.84\" title=\"-0.016\">step</span><span style=\"opacity: 0.80\"> to\n",
"</span><span style=\"background-color: hsl(120, 100.00%, 86.11%); opacity: 0.84\" title=\"0.017\">gassing</span><span style=\"opacity: 0.80\"> them without </span><span style=\"background-color: hsl(120, 100.00%, 85.84%); opacity: 0.85\" title=\"0.018\">trial</span><span style=\"opacity: 0.80\">. after all, it seems that the </span><span style=\"background-color: hsl(120, 100.00%, 88.88%); opacity: 0.83\" title=\"0.012\">nazis</span><span style=\"opacity: 0.80\"> </span><span style=\"background-color: hsl(120, 100.00%, 92.75%); opacity: 0.82\" title=\"0.007\">originally</span><span style=\"opacity: 0.80\">\n",
"only </span><span style=\"background-color: hsl(0, 100.00%, 94.10%); opacity: 0.81\" title=\"-0.005\">intended</span><span style=\"opacity: 0.80\"> to </span><span style=\"background-color: hsl(120, 100.00%, 86.11%); opacity: 0.84\" title=\"0.017\">imprison</span><span style=\"opacity: 0.80\"> the </span><span style=\"background-color: hsl(0, 100.00%, 96.10%); opacity: 0.81\" title=\"-0.003\">jews</span><span style=\"opacity: 0.80\">; the </span><span style=\"background-color: hsl(0, 100.00%, 91.35%); opacity: 0.82\" title=\"-0.009\">final</span><span style=\"opacity: 0.80\"> </span><span style=\"background-color: hsl(0, 100.00%, 94.18%); opacity: 0.81\" title=\"-0.005\">solution</span><span style=\"opacity: 0.80\"> was </span><span style=\"background-color: hsl(120, 100.00%, 86.11%); opacity: 0.84\" title=\"0.017\">dreamt</span><span style=\"opacity: 0.80\"> up </span><span style=\"background-color: hsl(120, 100.00%, 87.07%); opacity: 0.84\" title=\"0.015\">partly</span><span style=\"opacity: 0.80\">\n",
"because they </span><span style=\"background-color: hsl(120, 100.00%, 72.37%); opacity: 0.92\" title=\"0.046\">couldn</span><span style=\"opacity: 0.80\">'t </span><span style=\"background-color: hsl(120, 100.00%, 87.08%); opacity: 0.84\" title=\"0.015\">afford</span><span style=\"opacity: 0.80\"> to </span><span style=\"background-color: hsl(0, 100.00%, 85.74%); opacity: 0.85\" title=\"-0.018\">run</span><span style=\"opacity: 0.80\"> the </span><span style=\"background-color: hsl(120, 100.00%, 87.38%); opacity: 0.84\" title=\"0.015\">camps</span><span style=\"opacity: 0.80\"> because of the </span><span style=\"background-color: hsl(120, 100.00%, 90.26%); opacity: 0.83\" title=\"0.010\">devastation</span><span style=\"opacity: 0.80\">\n",
"</span><span style=\"background-color: hsl(120, 100.00%, 93.70%); opacity: 0.81\" title=\"0.006\">caused</span><span style=\"opacity: 0.80\"> by </span><span style=\"background-color: hsl(120, 100.00%, 86.11%); opacity: 0.84\" title=\"0.017\">goering</span><span style=\"opacity: 0.80\">'s </span><span style=\"background-color: hsl(120, 100.00%, 84.81%); opacity: 0.85\" title=\"0.019\">total</span><span style=\"opacity: 0.80\"> </span><span style=\"background-color: hsl(120, 100.00%, 79.32%); opacity: 0.88\" title=\"0.030\">war</span><span style=\"opacity: 0.80\">. those who </span><span style=\"background-color: hsl(120, 100.00%, 60.00%); opacity: 1.00\" title=\"0.077\">weren</span><span style=\"opacity: 0.80\">'t </span><span style=\"background-color: hsl(120, 100.00%, 86.11%); opacity: 0.84\" title=\"0.017\">gassed</span><span style=\"opacity: 0.80\"> </span><span style=\"background-color: hsl(0, 100.00%, 87.57%); opacity: 0.84\" title=\"-0.015\">generally</span><span style=\"opacity: 0.80\"> </span><span style=\"background-color: hsl(120, 100.00%, 80.00%); opacity: 0.87\" title=\"0.029\">died</span><span style=\"opacity: 0.80\"> of\n",
"</span><span style=\"background-color: hsl(120, 100.00%, 87.08%); opacity: 0.84\" title=\"0.015\">malnutrition</span><span style=\"opacity: 0.80\"> or </span><span style=\"background-color: hsl(0, 100.00%, 97.64%); opacity: 0.80\" title=\"-0.001\">disease</span><span style=\"opacity: 0.80\">.\n",
"</span>\n",
" </p>\n",
"\n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
" \n",
"\n",
"\n",
"\n"
],
"text/plain": [
"Explanation(estimator=\"SGDClassifier(alpha=0.001, average=False, class_weight=None, epsilon=0.1,\\n eta0=0.0, fit_intercept=True, l1_ratio=0.15,\\n learning_rate='optimal', loss='hinge', n_iter=5, n_jobs=1,\\n penalty='l2', power_t=0.5, random_state=42, shuffle=True, verbose=0,\\n warm_start=False)\", description=None, error=None, method='linear model', is_regression=False, targets=[TargetExplanation(target='alt.atheism', feature_weights=FeatureWeights(pos=[FeatureWeight(feature='weren', weight=0.0773954100541661, std=None, value=0.14113251347628228), FeatureWeight(feature='war', weight=0.060331064235727434, std=None, value=0.25329243510498373), FeatureWeight(feature='stick', weight=0.047099798280833598, std=None, value=0.13878354649278488), FeatureWeight(feature='couldn', weight=0.045627494068862393, std=None, value=0.11665332745553016), FeatureWeight(feature='don', weight=0.044633578336684819, std=None, value=0.059008669069186982), FeatureWeight(feature='people', weight=0.03756098108116717, std=None, value=0.061605331599386762), FeatureWeight(feature='say', weight=0.037547593579566788, std=None, value=0.070035159108064279), FeatureWeight(feature='trial', weight=0.035103971266393565, std=None, value=0.29326399549283061), FeatureWeight(feature='died', weight=0.028746754338132854, std=None, value=0.1217005880387947), FeatureWeight(feature='prepared', weight=0.023715008933589188, std=None, value=0.13113962337932056), FeatureWeight(feature='citizens', weight=0.020645220994210334, std=None, value=0.14822258183331075), FeatureWeight(feature='total', weight=0.019411252142228472, std=None, value=0.12805491520907761), FeatureWeight(feature='imprison', weight=0.017081411509440175, std=None, value=0.1978808731083859), FeatureWeight(feature='gassing', weight=0.017081411509440175, std=None, value=0.1978808731083859), FeatureWeight(feature='goering', weight=0.017081411509440175, std=None, value=0.1978808731083859), FeatureWeight(feature='gassed', weight=0.017081411509440175, std=None, value=0.1978808731083859), FeatureWeight(feature='dreamt', weight=0.017081411509440175, std=None, value=0.1978808731083859), FeatureWeight(feature='partly', weight=0.015420739822327539, std=None, value=0.17080502455743396), FeatureWeight(feature='malnutrition', weight=0.015399766224814532, std=None, value=0.18788798301142415), FeatureWeight(feature='afford', weight=0.015399766224814532, std=None, value=0.18788798301142415), FeatureWeight(feature='camps', weight=0.014886086253111977, std=None, value=0.17080502455743396), FeatureWeight(feature='concentration', weight=0.014231245031190482, std=None, value=0.16700590406326113), FeatureWeight(feature='ii', weight=0.013267434659514979, std=None, value=0.13768801060300351), FeatureWeight(feature='world', weight=0.012561859446063313, std=None, value=0.087675101707034339), FeatureWeight(feature='nazis', weight=0.012429767025009412, std=None, value=0.17529843038426265), FeatureWeight(feature='devastation', weight=0.010285112920622628, std=None, value=0.18788798301142415), FeatureWeight(feature='camp', weight=0.0083609832490070066, std=None, value=0.15821547193027247), FeatureWeight(feature='round', weight=0.0082766677954305527, std=None, value=0.15174937359587662), FeatureWeight(feature='originally', weight=0.0067404117855649811, std=None, value=0.13197514405233782), FeatureWeight(feature='japanese', weight=0.0061648526753915909, std=None, value=0.16700590406326113), FeatureWeight(feature='let', weight=0.005741759401189735, std=None, value=0.091972128013194321), FeatureWeight(feature='caused', weight=0.0055198750668633599, std=None, value=0.11904797656414623)], neg=[FeatureWeight(feature='<BIAS>', weight=-0.83683508055376643, std=None, value=1.0), FeatureWeight(feature='short', weight=-0.022953012470607569, std=None, value=0.10922331578554058), FeatureWeight(feature='run', weight=-0.017726710033409473, std=None, value=0.10956325909850168), FeatureWeight(feature='step', weight=-0.015549760669647097, std=None, value=0.12284709705831905), FeatureWeight(feature='generally', weight=-0.014569310446517788, std=None, value=0.11447088475047355), FeatureWeight(feature='happened', weight=-0.0090320514326100703, std=None, value=0.11904797656414623), FeatureWeight(feature='final', weight=-0.0086863622374825557, std=None, value=0.12597095614933734), FeatureWeight(feature='sure', weight=-0.0084809410873312695, std=None, value=0.084017216553222904), FeatureWeight(feature='look', weight=-0.0058345855656802403, std=None, value=0.089577478904578248), FeatureWeight(feature='intended', weight=-0.0050267547796723272, std=None, value=0.11904797656414623), FeatureWeight(feature='solution', weight=-0.004931561536108256, std=None, value=0.13113962337932056), FeatureWeight(feature='jews', weight=-0.0027801630252263926, std=None, value=0.11620110376866166), FeatureWeight(feature='disease', weight=-0.0013547744822263942, std=None, value=0.10518421774345865)], pos_remaining=0, neg_remaining=0), proba=None, score=-0.22584955588060562, weighted_spans=WeightedSpans(docs_weighted_spans=[DocWeightedSpans(document='\\ndon\\'t be so sure. look what happened to japanese citizens in the us during\\nworld war ii. if you\\'re prepared to say \"let\\'s round these people up and\\nstick them in a concentration camp without trial\", it\\'s only a short step to\\ngassing them without trial. after all, it seems that the nazis originally\\nonly intended to imprison the jews; the final solution was dreamt up partly\\nbecause they couldn\\'t afford to run the camps because of the devastation\\ncaused by goering\\'s total war. those who weren\\'t gassed generally died of\\nmalnutrition or disease.\\n', spans=[('don', [(1, 4)], 0.044633578336684819), ('sure', [(13, 17)], -0.0084809410873312695), ('look', [(20, 24)], -0.0058345855656802403), ('happened', [(30, 38)], -0.0090320514326100703), ('japanese', [(42, 50)], 0.0061648526753915909), ('citizens', [(51, 59)], 0.020645220994210334), ('world', [(77, 82)], 0.012561859446063313), ('war', [(83, 86)], 0.060331064235727434), ('ii', [(87, 89)], 0.013267434659514979), ('prepared', [(102, 110)], 0.023715008933589188), ('say', [(114, 117)], 0.037547593579566788), ('let', [(119, 122)], 0.005741759401189735), ('round', [(125, 130)], 0.0082766677954305527), ('people', [(137, 143)], 0.03756098108116717), ('stick', [(151, 156)], 0.047099798280833598), ('concentration', [(167, 180)], 0.014231245031190482), ('camp', [(181, 185)], 0.0083609832490070066), ('trial', [(194, 199)], 0.035103971266393565), ('short', [(214, 219)], -0.022953012470607569), ('step', [(220, 224)], -0.015549760669647097), ('gassing', [(228, 235)], 0.017081411509440175), ('trial', [(249, 254)], 0.035103971266393565), ('nazis', [(286, 291)], 0.012429767025009412), ('originally', [(292, 302)], 0.0067404117855649811), ('intended', [(308, 316)], -0.0050267547796723272), ('imprison', [(320, 328)], 0.017081411509440175), ('jews', [(333, 337)], -0.0027801630252263926), ('final', [(343, 348)], -0.0086863622374825557), ('solution', [(349, 357)], -0.004931561536108256), ('dreamt', [(362, 368)], 0.017081411509440175), ('partly', [(372, 378)], 0.015420739822327539), ('couldn', [(392, 398)], 0.045627494068862393), ('afford', [(401, 407)], 0.015399766224814532), ('run', [(411, 414)], -0.017726710033409473), ('camps', [(419, 424)], 0.014886086253111977), ('devastation', [(440, 451)], 0.010285112920622628), ('caused', [(452, 458)], 0.0055198750668633599), ('goering', [(462, 469)], 0.017081411509440175), ('total', [(472, 477)], 0.019411252142228472), ('war', [(478, 481)], 0.060331064235727434), ('weren', [(494, 499)], 0.0773954100541661), ('gassed', [(502, 508)], 0.017081411509440175), ('generally', [(509, 518)], -0.014569310446517788), ('died', [(519, 523)], 0.028746754338132854), ('malnutrition', [(527, 539)], 0.015399766224814532), ('disease', [(543, 550)], -0.0013547744822263942)], preserve_density=False, vec_name=None)], other=FeatureWeights(pos=[FeatureWeight(feature=<FormattedFeatureName 'Highlighted in text (sum)'>, weight=0.61098552467316103, std=None, value=None)], neg=[FeatureWeight(feature='<BIAS>', weight=-0.83683508055376643, std=None, value=1.0)], pos_remaining=0, neg_remaining=0)))], feature_importances=None, decision_tree=None, highlight_spaces=None, transition_features=None)"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"doc = twenty_train.data[53]\n",
"eli5.explain_prediction(clf, doc, vec=vec, top_targets=1,\n",
" target_names=twenty_train.target_names)"
]
}
],
"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"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment