After obtaining accuracy metric from my keras binary classification model, I need know what the model made the predictions. So, I'm interested in variable importance. I use lime package.
library(lime)
explainer <- lime (
x = x_train,
model = model_keras,
bin_continuous = FALSE)
explanation <- explain (
x_test[1:20,], # Show first 20 samples
explainer = explainer,
n_labels = 1,
n_features = 5)
Explain function gives me the following error in py_get_attr_impl function: AttributeError: 'function' object has no attribute 'func_name'.
I have compiled keras model with R, but this Issue seems to be that error comes from Python version. Problems with Reticulate package?
It works with python 2.7 but generates error with python 3+.
Actually function attribute func_name was renamed in python 3+ to __name__.
lime package (models.R) has a line:
if (keras::get_layer(x, index = -1)$activation$func_name == 'linear')
I removed $func_name and the code worked for me.
I suppose this is not the best workaround, however the possible solution that comes to mind:
if (keras::get_layer(x, index = -1)$activation$__name__ == 'linear')
did not work with R.
Related
I am attempting to the use the SHAP python library on an XGBoost model I built for binary classification on tabular data.
While attempting to generate dependency plots in particular, I get the following error.
TypeError: loop of ufunc does not support argument 0 of type Explanation which has no callable conjugate method
Here is the full traceback:
https://pastebin.com/u3imDXir
My code for the SHAP analysis seems fairly basic and I am able to generate beeswarm plots with no trouble.
import shap
explainer = shap.Explainer(model, X_train)
shap_values = explainer(X_train)
max_display = 20
shap.summary_plot(shap_values, X_train, max_display=max_display, color_bar=False, show=False)
plt.gcf().set_size_inches(5, 7)
plt.colorbar(label='feature value', ticks=[], fraction=0.03, pad=0.04)
plt.title = "SHAP beeswarm summary plot"
plt.savefig(('../reports/figures/01_successbutdialysisDurReversed/all_Data_plotted/summary_plot_limFeatures_' + str(max_display) + '.png'), dpi=600, bbox_inches='tight')`
It is when I attempt to make the dependence_plot that I start to get the errors.
shap.dependence_plot("iv_total", shap_values, X_train)
plt.show()
I have thus far attempted to wipe my virtualenv and install the latest anaconda distributions for major packages, on Python 3.10. If it is of relevance, I am using an M1 Mac. I would appreciate any help with this!
I am using LightGBM's LGBMClassifier for a binary classification problem and want to print out the actual diagram.
Here is how I trained/fit the model
clf = lgb.LGBMClassifier()
clf.fit(x_train, y_train, categorical_feature = x_train.select_dtypes(include = 'category').columns.tolist())
And here is how I am trying to print the diagram
lgb.create_tree_digraph(clf, orientation='vertical')
However, the only output I am getting is
<graphviz.graphs.Digraph at 0x7f6a10a5ed00>
I also tried using the parent lightgbm.train() method to build the model and as the booster argument in create_tree_diagraph, however I am getting similar output.
Is there an additional library or function I have to call to print out the tree, or is there another way to perhaps save it to a .png file?
I am using a Python Notebook in Databricks.
The REPL explained to you that you have a Digraph in hand.
Good. Let's assign it to a temp var, and render it.
https://graphviz.readthedocs.io/en/stable/manual.html#basic-usage
g = lgb.create_tree_digraph( ... )
g.render(view=True)
g.format = "png"
g.render("output.png")
g.view()
This relies on dot being already installed
so it works when bash invokes it.
I am using lstm predictor for timeseries prediction..
regressor = skflow.Estimator(model_fn=lstm_model(TIMESTEPS, RNN_LAYERS, DENSE_LAYERS))
validation_monitor = learn.monitors.ValidationMonitor(X['val'], y['val'],
every_n_steps=PRINT_STEPS,
early_stopping_rounds=1000)
regressor.fit(X['train'], y['train'], monitors=[validation_monitor])
But while doing regressor.fit, i am getting the error as shown in Title, need help on this..
I understand that your code imports the lstm_model from the file lstm_predictor.py when initializing your estimator. If so, the problem is caused by the following line:
x_ = learn.ops.split_squeeze(1, time_steps, X)
As the README.md of that repo tells, the Tensorflow API has changed significantly. The function split_squeeze also seems to be removed from the module tensorflow.contrib.learn.python.ops. This issue has been discussed in that repository but no changes have been made in that repo since 2 years!
Yet, you can simply replace that function with tf.unstack. So simply change the line as:
x_ = tf.unstack(X, num=time_steps, axis=1)
With this I was able to get past the problem.
I am using GridSearchCV to do classification and my codes are:
parameter_grid_SVM = {'dual':[True,False],
'loss':["squared_hinge","hinge"],
'penalty':["l1","l2"]
}
clf = GridSearchCV(LinearSVC(),param_grid=parameter_grid_SVM,verbose=2)
clf.fit(trian_data, labels)
And then, I meet the error
ValueError: Unsupported set of arguments: penalty='l1' is only supported when dual='false'., Parameters: penalty='l1', loss='hinge', dual=False
later on I change my code to :
clf = GridSearchCV(LinearSVC(penalty='l1',dual=False),verbose=2)
And I meet the error
TypeError: init() takes at least 3 arguments (3 given)
I also tried:
parameter_grid_SVM = {
'loss':["squared_hinge"]
}
clf = GridSearchCV(LinearSVC(penalty='l1',dual=False),param_grid=parameter_grid_SVM,verbose=2)
clf.fit(trian_data, labels)
However, I still have the error
ValueError: Unsupported set of arguments: penalty='l1' is only supported when dual='false'., Parameters: penalty='l1', loss='squared_hinge', dual=False
Anyone has idea what I should do to deal with that?
I also met this problem when doing sparse SVM. I find one piece of working demo code in this page SVM module explanation. Hope it might help.
clf = LinearSVC(loss='l2', penalty='l1', dual=False)
One option is to instruct GridSearchCV to set the score manually if model throws an exception using the error_score parameter. See my answer here.
Had a similar issue and in my case, it was writing twelve 12 instead of 'el two' l2 in some instances.
The code that produces this error message is here. I don't see what would cause this to only happen occasionally, but the bare else means that presumably it's something else other than just the penalty='l1', dual='false' combination.
I am getting the following error when i perform classification of new data with the following command in Python:
classifier.predict(new_data)
AttributeError: python 'SVC' object has no attribute _dual_coef_
In my laptop though, the command works fine! What's wrong?
I had this exact error
AttributeError: python 'SVC' object has no attribute _dual_coef_
with a model trained using scikit-learn version 0.15.2, when I tried to run it in scikit-learn version 0.16.1. I did solve it by re-training the model in the latest scikit-learn 0.16.1.
Make sure you are loading the right version of the package.
Have you loaded the model based on which you try to predict?
In this case it can be a version conflict, try to re-learn the model using the same sklearn version.
You can see a similar problem here: Sklearn error: 'SVR' object has no attribute '_impl'
I had the same problem,I use Sklearn version 0.23.02 but I was trying to run an archive trained with a version 0.18... and my error said: "'SVC' object has no attribute 'break_ties'", I just retrained the model with my version and fix the problem I generate other svc.pickle to run with the 0.23.02 version and replace the oldie.
"""
X = X_train
y = y_train
"""
X = X_test
y = y_test
# Instantiate and train the classifier
from sklearn.neighbors import KNeighborsClassifier
clf = KNeighborsClassifier(n_neighbors=1)
clf.fit(X, y)
# Check the results using metrics
from sklearn import metrics
y_pred = clf.predict(X)
print(metrics.confusion_matrix(y_pred, y))