title: Grouped sampling in scikit-learn
body: <p>Is there a way to conveniently split a dataset into training and testing sets, keeping records that belong to a same group together?</p>
  
  <p>Take for example a table that records independent and dependent variables for every <code>person_id</code> in such a way that every person may have one or more entries:</p>
  
  <pre><code>import pandas as pd
  
  tbl = pd.DataFrame(dict(
          person_id=list('aaabbcccdeeefffhiijj'),
          random_variable=np.linspace(0, 1, 20),
          dependent_variable=np.arange(20)
      ))
  </code></pre>
  
  <p>Now, I want to split the data to training and testing sets, keeping the records that belong to the same person in the same data set. Obviously, using <code>sklearn.cross_validation.train_test_split</code> doesn't do the job. I'm aware of <code>sklearn.cross_validation.LeavePLabelOut</code>, but instead of creating a single split, it creates all possible combinations, which is not exactly what I currently want. </p>
  
  <p>Another approach would be to calculate a hash value based on the <code>person_id</code> field and use it for the sampling:</p>
  
  <pre><code>import numpy as np
  
  salt = str(np.random.rand()) # randomness source 
  hash_values = tbl['person_id'].apply(lambda p: hash(salt + p) % 100)
  # 50/50 split
  sel_training = hash_values &lt; 50
  training_set = tbl.loc[sel_training]
  testing_set = tbl.loc[-sel_training]
  </code></pre>
  
  <p>Is there a more elegant way to achieve this task?</p>