简体   繁体   中英

Numpy.ndarray' object has no attribute 'head'

i have a problem with my explainable model,the following happens:

I define muy label enconding

The last column, common name is a categorical value. Label Encode it to numerical values.

label_encoder = LabelEncoder()
x= merged_data_df.iloc[:, 1:14]   
y = label_encoder.fit_transform(merged_data_df['common name'])
print(x.shape, y.shape)

start on train test

x_train, x_test, y_train, y_test = train_test_split(x.values, y,test_size = 0.2, random_state= 0)
 print(f"Train Data: {x_train.shape}, {y_train.shape}")
print(f"Test Data: {x_test.shape}, {y_test.shape}")

Random Forest Classifier

from sklearn.ensemble import RandomForestClassifier
rf_pipeline=make_pipeline(StandardScaler(),RandomForestClassifier(random_state=1502))
rf_pipeline.fit(x_train,y_train)

so far so good, then I want to make the model explainable but I get an error.

import shap
explainer = shap.KernelExplainer(rf_pipeline.predict, x_test)

instance = x_test.loc[[95]]
shap_instance = explainer.shap_values(instance)
shap.initjs()
shap.force_plot(explainer.expected_value,shap_instance, instance)

AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_1058/159078831.py in <module>
----> 1 instance = x_test.loc[[95]]
      2 shap_instance = explainer.shap_values(instance)
      3 shap.initjs()
      4 shap.force_plot(explainer.expected_value,shap_instance, in

    stance)
    
        AttributeError: 'numpy.ndarray' object has no attribute 'loc'
   

Your x_test variable is not a pandas dataframe, so you can't index it using.loc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM