#!/usr/bin/env python # coding: utf-8 # In[1]: from sklearn.datasets import load_boston boston = load_boston() # In[2]: import pandas as pd boston_df = pd.DataFrame(boston.data, columns=boston.feature_names) boston_df.head() # In[3]: from sklearn.model_selection import train_test_split X_train, X_test, Y_train, Y_test = train_test_split(boston_df, boston.target, test_size = 0.25, random_state = 31) print(X_train.shape) print(Y_train.shape) print(X_test.shape) print(Y_test.shape) # In[5]: from aix360.algorithms.rbm import FeatureBinarizer fb = FeatureBinarizer(negations=True) X_train_fb = fb.fit_transform(X_train) X_test_fb = fb.transform(X_test) X_train_fb['CRIM'][:10] # In[6]: from aix360.algorithms.rbm import GLRMExplainer, LinearRuleRegression # In[7]: linear_model = LinearRuleRegression() explainer = GLRMExplainer(linear_model) explainer.fit(X_train_fb, Y_train) # In[8]: Y_pred = explainer.predict(X_test_fb) # In[9]: from sklearn.metrics import r2_score, explained_variance_score, mean_absolute_error, max_error print(f'R2 Score = {r2_score(Y_test, Y_pred)}') print(f'Explained Variance = {explained_variance_score(Y_test, Y_pred)}') print(f'Mean abs. error = {mean_absolute_error(Y_test, Y_pred)}') print(f'Max error = {max_error(Y_test, Y_pred)}') # In[10]: explainer.explain() # In[11]: get_ipython().run_line_magic('matplotlib', 'inline') fb = FeatureBinarizer(negations=True) figs, _ = explainer.visualize(boston_df, fb) # In[ ]: