Repository: scikit-learn/scikit-learn
Pull request: [MRG] Fixes #8736 add get_n_splits for RepeatedKFold and RepeatedStratifiedKFold


<!--
Thanks for contributing a pull request! Please ensure you have taken a look at
the contribution guidelines: https://github.com/scikit-learn/scikit-learn/blob/master/CONTRIBUTING.md#Contributing-Pull-Requests
-->
#### Reference Issue
Fixes #8736


#### What does this implement/fix? Explain your changes.
This implements the function get_n_splits for RepeatedKFold and RepeatedStratifiedKFold.

#### Any other comments?


<!--
Please be aware that we are a loose team of volunteers so patience is
necessary; assistance handling other issues is very welcome. We value
all user contributions, no matter how minor they are. If we are slow to
review, either the pull request needs some benchmarking, tinkering,
convincing, etc. or more likely the reviewers are simply busy. In either
case, we ask for your understanding during the review process.
For more information, see our FAQ on this topic:
http://scikit-learn.org/dev/faq.html#why-is-my-pull-request-not-getting-any-attention.

Thanks for contributing!
-->


Problem statement:
Bug in RepeatedKFold and RepeatedStratifiedKFold: get_n_splits missing
```python
from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV, RepeatedKFold
iris = datasets.load_iris()
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
svr = svm.SVC()
clf = GridSearchCV(svr, parameters, cv=RepeatedKFold())
clf.fit(iris.data, iris.target)
```
>AttributeError: 'RepeatedKFold' object has no attribute 'get_n_splits'

I blame me :-/ (also the tests)

Hints from issue discussion:
I would like to work on this. This'll be my first contribution and seems like a good bug to start with.
@akshat-harit  go for it!
is this issue still open? I would like to also try to work on it... also my first contribution. ty -L
--also do you have a slack channel for first-time or new contributors?
@akshat-harit @Leahelisabeth Check together who is going for it to avoid duplicate PR.

@Leahelisabeth Check the [contributing guide](scikit-learn.org/stable/developers/contributing.html).
Then the feedbacks will come when you will submit the PR.
You will have more chance to get any quick feedback related to your PR with core developers than on the [gitter](https://gitter.im/scikit-learn/scikit-learn)
@amueller @glemaitre ,
Hello, this issue seems open, I would like to contribute in solving this issue. Can you please give me some pointers to start with.
Thank you.
Start by looking at other implementations of get_n_splits and where they
are tested. Write your own test and open a pull request. Write an
implementation that passes the test.

On 26 Apr 2017 4:25 am, "AishwaryaRK" <notifications@github.com> wrote:

@amueller <https://github.com/amueller> @glemaitre
<https://github.com/glemaitre> ,
Hello, this issue seems open, I would like to contribute in solving this
issue. Can you please give me some pointers to start with.
Thank you.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<https://github.com/scikit-learn/scikit-learn/issues/8736#issuecomment-297121867>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEz66D176nFevYFGOHUFWxBpGDmQI9pks5rzjp9gaJpZM4M7zRE>
.

@jnothman Thanks :)

Commit message: add get_n_splits for RepeatedKFold and RepeatedStratifiedKFold in _RepeatedSplits

Patch to review:
diff --git a/sklearn/model_selection/_split.py b/sklearn/model_selection/_split.py
index 0eb51be93f5bb..2dc8cda3c03f9 100644
--- a/sklearn/model_selection/_split.py
+++ b/sklearn/model_selection/_split.py
@@ -997,6 +997,16 @@ def split(self, X, y=None, groups=None):
             for train_index, test_index in cv.split(X, y, groups):
                 yield train_index, test_index
 
+    def get_n_splits(self):
+        """Returns the number of splitting iterations in the cross-validator
+
+        Returns
+        -------
+        n_splits : int
+            Returns the number of splitting iterations in the cross-validator.
+        """
+        return self.cvargs["n_splits"] * self.n_repeats
+
 
 class RepeatedKFold(_RepeatedSplits):
     """Repeated K-Fold cross validator.
diff --git a/sklearn/model_selection/tests/test_split.py b/sklearn/model_selection/tests/test_split.py
index c997ac9d73e5d..fcd0160ca74ee 100644
--- a/sklearn/model_selection/tests/test_split.py
+++ b/sklearn/model_selection/tests/test_split.py
@@ -844,6 +844,22 @@ def test_repeated_kfold_determinstic_split():
         assert_raises(StopIteration, next, splits)
 
 
+def test_get_n_splits_for_repeated_kfold():
+    n_splits = 3
+    n_repeats = 4
+    rkf = RepeatedKFold(n_splits, n_repeats)
+    expected_n_splits = n_splits * n_repeats
+    assert_equal(expected_n_splits, rkf.get_n_splits())
+
+
+def test_get_n_splits_for_repeated_stratified_kfold():
+    n_splits = 3
+    n_repeats = 4
+    rskf = RepeatedStratifiedKFold(n_splits, n_repeats)
+    expected_n_splits = n_splits * n_repeats
+    assert_equal(expected_n_splits, rskf.get_n_splits())
+
+
 def test_repeated_stratified_kfold_determinstic_split():
     X = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
     y = [1, 1, 1, 0, 0]


Code review comments:
diff_hunk: @@ -997,6 +997,16 @@ def split(self, X, y=None, groups=None):
             for train_index, test_index in cv.split(X, y, groups):
                 yield train_index, test_index
 
+    def get_n_splits(self):
+        """Returns the number of splitting iterations in the cross-validator
+
+        Returns
+        -------
+        n_splits : int
+            Returns the number of splitting iterations in the cross-validator.
+        """
+        return self.cvargs["n_splits"] * self.n_repeats
path: sklearn/model_selection/_split.py
text: @user1:
Don't rely on n_splits bring present in charge even if it works for the two cases here

@author:
@user1  I didn't get by what you mean by "bring present in charge", should I send the arguments -> n_splits and n_repeats in the get_n_splits function, because the get_n_splits for RepeatedKFold and RepeatedStratifiedKFold should return the multiplication of n_splits and n_repeats as those many splits are generated by them. Can you please tell what should I do.

@user1:
Sorry. Auto correct. Being present in cvargs

diff_hunk: @@ -997,6 +997,16 @@ def split(self, X, y=None, groups=None):
             for train_index, test_index in cv.split(X, y, groups):
                 yield train_index, test_index
 
+    def get_n_splits(self):
path: sklearn/model_selection/_split.py
text: @user1:
This should take parameters

@author:
@user1 the calculation for get_n_splits function for RepeatedKFold and RepeatedStratifiedKFold depends only on n_splits and n_repeats and is independent of X or groups or y.  Should I send the arguments -> n_splits and n_repeats in the get_n_splits function