0

I customized a CombinedAttributesAdder so I can output the right Feature Engineered column names, depending if you choose to include a feature or not, like this:

class CombinedAttributesAdder(BaseEstimator, TransformerMixin): # BaseEstimator erlaubt get_params und set_params, TransformerMixin fit_transform()

    def __init__(self, add_bedrooms_per_room = True): # Konstruktor, übernimmt Input von außen und importiert diesen
        self.add_bedrooms_per_room = add_bedrooms_per_room # Importieren der äußeren Variable
    def fit(self, X, y=None):
        return self  # Gibt nur das DataFrame zurück
    def transform(self, X, y=None): # Transformation, richtet sich nach Indizes
        rooms_per_household = X[:, rooms_ix] / X[:, households_ix]
        population_per_household = X[:, population_ix] / X[:, households_ix]
        if self.add_bedrooms_per_room:
            bedrooms_per_room = X[:, bedrooms_ix] / X[:, rooms_ix]
            return np.c_[X, rooms_per_household, population_per_household,
                         bedrooms_per_room]
        else:
            return np.c_[X, rooms_per_household, population_per_household]
    def columns(self):
        if self.add_bedrooms_per_room:
            return ["rooms_per_household", "population_per_household", "bedrooms_per_room"]
        else:
            return ["rooms_per_household", "population_per_household"]


attr_adder = CombinedAttributesAdder(add_bedrooms_per_room=False)
housing_extra_attribs = attr_adder.transform(housing.values)

That way everytime I call attr_adder.columns() i can get the right column names and don't need to write them out all the time.

The problem now is that I cant save my model, which the custom CombineAttributesAdder is a part of, with joblib, since I get the following Error:

---------------------------------------------------------------------------
PicklingError                             Traceback (most recent call last)
<ipython-input-76-29425b213cb0> in <module>()
      2 
      3 import joblib
----> 4 joblib.dump(final_model_ch2, "final_model_ch2.pkl") # DIFF
      5 

50 frames
/usr/lib/python3.6/pickle.py in save_global(self, obj, name)
    925                 raise PicklingError(
    926                     "Can't pickle %r: it's not the same object as %s.%s" %
--> 927                     (obj, module_name, name))
    928 
    929         if self.proto >= 2:

PicklingError: Can't pickle <class '__main__.CombinedAttributesAdder'>: it's not the same object as __main__.CombinedAttributesAdder

Is there a way to save the model, while maintaining the custom CombinedAttributesAdder?

Monlist
  • 101
  • 1
  • 1
  • 9
  • Not sure this is the same issue. But you can refer to this thread for additional help: https://stackoverflow.com/questions/1412787/picklingerror-cant-pickle-class-decimal-decimal-its-not-the-same-object – Joseph Seung Jae Dollar Jul 09 '19 at 15:27
  • Thanks! That solved my problem. For anyone who has a similar problem in the future: take care how you call your function, because otherwise it could lead to problems with the saving. My problem was I called a method columns() in my CombinedAttributesAdder class and that lead to the problem. It can get fixed easily by calling the method column_names() or something, since that won't conflict with any other existing function. – Monlist Jul 10 '19 at 08:17

0 Answers0