def test_binsizes(self): ale_eff = aleplot_1D_categorical(X=self.X, model=self.model, feature="x5", predictors=self.X_cleaned.columns, encode_fun=onehot_encode_custom, include_CI=False) self.assertCountEqual(ale_eff.loc[:, "size"], [57, 77, 66])
def test_indexname(self): ale_eff = aleplot_1D_categorical(X=self.X, model=self.model, feature="x5", predictors=self.X_cleaned.columns, encode_fun=onehot_encode_custom, include_CI=False) self.assertEqual(ale_eff.index.name, "x5")
def test_outputshape_noCI(self): ale_eff = aleplot_1D_categorical(X=self.X, model=self.model, feature="x5", predictors=self.X_cleaned.columns, encode_fun=onehot_encode_custom, include_CI=False) self.assertEqual(ale_eff.shape, (3, 2)) self.assertCountEqual(ale_eff.columns, ["eff", "size"])
def test_effvalues(self): ale_eff = aleplot_1D_categorical(X=self.X, model=self.model, feature="x5", predictors=self.X_cleaned.columns, encode_fun=onehot_encode_custom, include_CI=False) self.assertCountEqual( np.round(ale_eff.loc[:, "eff"], 8), [-0.05565697, 0.02367971, 0.02044105], )
def test_bins(self): ale_eff = aleplot_1D_categorical(X=self.X, model=self.model, feature="x5", predictors=self.X_cleaned.columns, encode_fun=onehot_encode_custom, include_CI=False) self.assertCountEqual( ale_eff.index, ['A', 'B', 'C'], )
def test_outputshape_withCI(self): ale_eff = aleplot_1D_categorical(X=self.X, model=self.model, feature="x5", predictors=self.X_cleaned.columns, encode_fun=onehot_encode_custom, include_CI=True, C=0.9) self.assertEqual(ale_eff.shape, (3, 4)) self.assertCountEqual(ale_eff.columns, ["eff", "size", "lowerCI_90%", "upperCI_90%"])
def test_CIvalues(self): ale_eff = aleplot_1D_categorical(X=self.X, model=self.model, feature="x5", predictors=self.X_cleaned.columns, encode_fun=onehot_encode_custom, include_CI=True, C=0.9) # assert that the first bin do not have a CI self.assertTrue(np.isnan(ale_eff.loc[ale_eff.index[0], "lowerCI_90%"])) self.assertTrue(np.isnan(ale_eff.loc[ale_eff.index[0], "upperCI_90%"])) # check the values of the CI self.assertCountEqual( np.round(ale_eff.loc[ale_eff.index[1]:, "lowerCI_90%"], 8), [-0.00605249, -0.00523023], ) self.assertCountEqual( np.round(ale_eff.loc[ale_eff.index[1]:, "upperCI_90%"], 8), [0.05341192, 0.04611233], )
def test_exceptions(self): mod_not_fit = RandomForestRegressor() # dataset should be compatible with the model with self.assertRaises(Exception) as mod_ex_1: aleplot_1D_categorical( X=self.X, model=self.model.predict, feature="x5", predictors=self.X_cleaned.columns, encode_fun=onehot_encode_custom, ) with self.assertRaises(Exception) as mod_ex_2: aleplot_1D_categorical( X=self.X, model=self.model, feature="x5", predictors=self.X.columns, encode_fun=onehot_encode_custom, ) with self.assertRaises(Exception) as mod_ex_3: aleplot_1D_categorical( X=self.X, model=self.model, feature="x5", predictors=self.X_cleaned.columns, encode_fun=onehot_encode, ) mod_ex_msg = ( """There seems to be a problem when predicting with the model. Please check the following: - Your model is fitted. - The list of predictors contains the names of all the features""" """ used for training the model. - The encoding function takes the raw feature and returns the""" """ right columns encoding it, including the case of a missing category. """) self.assertEqual(mod_ex_1.exception.args[0], mod_ex_msg) self.assertEqual(mod_ex_2.exception.args[0], mod_ex_msg) self.assertEqual(mod_ex_3.exception.args[0], mod_ex_msg)