I am working on a ML tutorial project with my own dataset.

I built a ML model using training dataset and generated predictions using test dataset.

Shape of test dataset is (418,10)

My code for model training and predictions is below

rfs_clf = forest_clf = RandomForestClassifier(n_estimators=110, max_depth= 8, max_features='auto',
                                    random_state=0, oob_score=False, min_samples_split = 2,
                                   criterion= 'gini', min_samples_leaf=2, bootstrap=False)
rfs_clf.fit(X_train, y_train)
y_f_predict = rfs_clf.predict_proba(X_test).astype(float)

Now, am trying to explain the predictions using the Lime package available here

explainer = lime.lime_tabular.LimeTabularExplainer(X_train.values,feature_names = feat_cols, class_names=['0','1'],kernel_width=3) 

The error is happening in the line below where I want to explain the prediction specifically for 2nd observation (from my test dataframe which is X_test):

exp = explainer.explain_instance(X_test.iloc[1:2,].values,y_f_predict) # fails
exp = explainer.explain_instance(X_test.iloc[1:2,],y_f_predict) # fails
exp = explainer.explain_instance(X_test[2], y_f_predict)  #key error '2'

ValueError: could not broadcast input array from shape (10,) into shape (1,)
  • $\begingroup$ Have you made sure that the number of observations in your x and y_f_predict is the same? You are selecting one observation from X_test, but how many observations does the y_f_predict array contain? $\endgroup$
    – Oxbowerce
    Dec 22, 2021 at 8:06


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.