title: split into train and test by group+ sklearn cross_val_score
body: <p>I have a dataframe in python as shown below:</p>
  
  <pre><code>data    labels    group
   aa       1         x
   bb       1         x
   cc       2         y
   dd       1         y
   ee       3         y
   ff       3         x
   gg       3         z
   hh       1         z
   ii       2         z
  </code></pre>
  
  <p>It is straight forward to randomly split into 70:30 for training and test sets. Here, i need to split into test and train so that 70% of data within each <code>group</code> should be in training and 30% of data within each group as test data. Then predict and find accuracy of test data within each group.</p>
  
  <p>I find that <code>cross_val_score</code> does the splitting, fitting model and predciting with the below function:</p>
  
  <pre><code>&gt;&gt;&gt; from sklearn.model_selection import cross_val_score
  &gt;&gt;&gt; model = LogisticRegression(random_state=0)
  &gt;&gt;&gt; scores = cross_val_score(model, data, labels, cv=5)
  &gt;&gt;&gt; scores   
  </code></pre>
  
  <p>The documentation of <code>cross_val_score</code> have <code>groups</code> parameter which says:</p>
  
  <pre><code>groups : array-like, with shape (n_samples,), optional
  Group labels for the samples used while splitting the dataset into 
  train/test set.
  </code></pre>
  
  <p>Here, i need to split into test and train so that 70% of data within each group should be in training and 30% of data within each group as test data. Then predict and find accuracy of test data within each group. Does using the groups parameter in the below way split data within each group into training and test data and make the predictions?</p>
  
  <pre><code>&gt;&gt;&gt; scores = cross_val_score(model, data, labels, groups= group, cv=5)
  </code></pre>
  
  <p>Any help is appreciated.</p>