title: How to perform SMOTE with cross validation in sklearn in python
body: <p>I have a highly imbalanced dataset and would like to perform SMOTE to balance the dataset and perfrom cross validation to measure the accuracy. However, most of the existing tutorials make use of only single <code>training</code> and <code>testing</code> iteration to perfrom SMOTE.</p>
  
  <p>Therefore, I would like to know the correct procedure to perfrom SMOTE using cross-validation.</p>
  
  <p>My current code is as follows. However, as mentioned above it only uses single iteration.</p>
  
  <pre><code>from imblearn.over_sampling import SMOTE
  from sklearn.model_selection import train_test_split
  
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
  sm = SMOTE(random_state=2)
  X_train_res, y_train_res = sm.fit_sample(X_train, y_train.ravel())
  clf_rf = RandomForestClassifier(n_estimators=25, random_state=12)
  clf_rf.fit(x_train_res, y_train_res)
  </code></pre>
  
  <p>I am happy to provide more details if needed.</p>