title: Saving Patsy for sklean inference
body: <p>I am a huge lover of your <code>sklego</code> project, especially patsy implementation within sklean.</p>
  <p>However, there is one thing I still would like your opinion on - how do you use a pipeline containing PatsyTransformer only for inference?</p>
  <p>As the pickling is not yet supported on the patsy side I came up with a workaround.</p>
  <pre class="lang-py prettyprint-override"><code>import seaborn as sns
  from joblib import dump, load
  from sklego.preprocessing import PatsyTransformer
  from sklearn.linear_model import LinearRegression
  from sklearn.pipeline import Pipeline
  
  # Load The data
  
  data = sns.load_dataset(&quot;tips&quot;)
  
  # Basic Pipeline
  pipe = Pipeline([
      (&quot;patsy&quot;, PatsyTransformer(&quot;tip + C(day)&quot;)),
      (&quot;model&quot;, LinearRegression())
  ])
  
  data
  
  # Train the pipeline
  pipe.fit(data, data['total_bill'])
  
  from sklearn.base import BaseEstimator, TransformerMixin
  
  # Class for inferencing with pre-trained model (fit only passes, no training happens)
  class Model_Inferencer(BaseEstimator, TransformerMixin):
      &quot;&quot;&quot;
      Function that applyes pre-trained models within a pipeline setting.
      &quot;&quot;&quot;
  
      def __init__(self, pre_trained_model=None):
          self.pre_trained_model = pre_trained_model
  
      def transform(self, X):
  
          preds = self.pre_trained_model.predict(X)
          return preds
  
      def predict(self, X):
  
          preds = self.pre_trained_model.predict(X)
          return preds
  
      def fit(self, X, y=None, **fit_params):
          return self
  
  pipe.predict(data)[:10]
  
  # Save the model
  dump(pipe['model'], 'model_github.joblib')
  
  # Load The model
  loaded_model = load('model_github.joblib')
  
  # Create Inference Pipeline
  pipe_inference = Pipeline([
      (&quot;patsy&quot;, PatsyTransformer(&quot;tip + C(day)&quot;)),
      (&quot;inferencer&quot;, Model_Inferencer(loaded_model))
  ])
  
  
  # Inference pipeline needs to be fitted 
  # pipe_inference.fit(data)
  
  # Save predictions (works only when fitted)
  pipe_inference.predict(data)
  
  </code></pre>
  <p>I also tried saving the info by hand:</p>
  <pre class="lang-py prettyprint-override"><code>import h5py
  
  def save_patsy(patsy_step, filename):
      &quot;&quot;&quot;Save the coefficients of a linear model into a .h5 file.&quot;&quot;&quot;
      with h5py.File(filename, 'w') as hf:
          hf.create_dataset(&quot;design_info&quot;,  data=patsy_step.design_info_)
  
  def load_coefficients(patsy_step, filename):
      &quot;&quot;&quot;Attach the saved coefficients to a linear model.&quot;&quot;&quot;
      with h5py.File(filename, 'r') as hf:
          design_info = hf['design_info'][:]
      patsy_step.design_info_ = design_info
  
  
  save_patsy(pipe['patsy'], &quot;clf.h5&quot;)
  </code></pre>
  <p>However, a bummer error will occur.</p>
  <pre class="lang-py prettyprint-override"><code> Object dtype dtype('O') has no native HDF5 equivalent
  </code></pre>