question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extracting Local Model Coef_

See original GitHub issue

Hi, this is just a followup issue with regards to the local model weights.

I have the following code.

from sklearn.linear_model import LogisticRegression

clf = LogisticRegression()
clf.fit(X_train, y_train)  # Just the Taiwan credit dataset

explainer = lime.lime_tabular.LimeTabularExplainer(X_train, 
                                                   feature_names=feature_names, 
                                                   class_names=['N', "Y"],
                                                   verbose=False, 
                                                   discretize_continuous=False,
                                                   mode='classification')

i = 13
exp = explainer.explain_instance(X_train[i], clf.predict_proba)

total = 0
l = exp.as_map()[1]
l.sort()

for j in range(len(X_train[0])):
    total += X_train[i][j] * (l[j][1])
total + exp.intercept[1]

The issue is that my manual calculation of exp.local_pred is never exactly correct. Am I doing something wrong here? It should just be a simple matter of multiplying the input features by the weights plus intercept no?

The training data is just 3 continuous features.

Thanks a lot in advance!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
EoinKennycommented, Jul 29, 2018

@marcotcr you were exactly right, it works perfect now.

Thanks a lot for taking the time to explain.

1reaction
marcotcrcommented, Jul 29, 2018

That is not normal. I think there are two bugs in your code:

  1. X_train[i, j] will be a categorical value (an integer) if feature[j] is categorical, whereas the explanation weight should be multiplied by 1, indicating the presence of the categorical value.
  2. coefs will give you at most num_feature coefs (this is a parameter to explain_instance, the default is 10). If you have more than 10 features in your dataset, the multiplication won’t work.

I think if you do

ids = [x[0] for x in coefs]
scaled_instance[categorical_features] = 1
manual_calculation = sum(coefs * scaled_instance[ids]) + exp.intercept[1]

you will get the same as exp.local_pred

Read more comments on GitHub >

github_iconTop Results From Across the Web

coef: Extract Model Coefficients - Rdrr.io
Description. coef is a generic function which extracts model coefficients from objects returned by modeling functions. coefficients is an alias for it.
Read more >
Extract Model Coefficients - MATLAB & Simulink - MathWorks
Extract model coefficients such as transfer function numerator and denominator coefficients, state-space matrices, and PID gains.
Read more >
Extract regression coefficient values | Edureka Community
I have a regression model for some time series data investigating drug utilisation. The purpose is to fit a spline to a time...
Read more >
extract coefficients from e(b) after regress - Statalist
Can I extract individual coefficients directly from e(b) after the regress command, or must I copy e(b) to another matrix first and then...
Read more >
Extract Model Coefficients - R
coef is a generic function which extracts model coefficients from objects returned by modeling functions. coefficients is an alias for it. Usage. coef(object,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found