0

I am using the following code for 5 fold cross validation. I am getting error as kfold is not iterable. I have tried to use shuffle, writing number of folds different way but still getting error.

from sklearn.model_selection import KFold

np.random.seed(28)
#from sklearn.cross_validation import KFold
def classification_model(model, data, predictors, outcome):
  #Fit the model:
    model.fit(data[predictors],data[outcome])
  
  #Make predictions on training set:
    predictions = model.predict(data[predictors])
  
  #Print accuracy
    accuracy = metrics.accuracy_score(predictions,data[outcome])
    print("Accuracy : %s" % "{0:.3%}".format(accuracy))
    #k = 5
    kf = KFold(n_splits=5, shuffle=False)
    error = []
    for train, test in kf:
    # Filter training data
        train_predictors = (data[predictors].iloc[train,:])
         # The target we're using to train the algorithm.
        train_target = data[outcome].iloc[train]
    
    # Training the algorithm using the predictors and target.
        model.fit(train_predictors, train_target)
    
    #Record error from each cross-validation run
        error.append(model.score(data[predictors].iloc[test,:], data[outcome].iloc[test]))
    
        print("Cross-Validation Score : %s" % "{0:.3%}".format(np.mean(error)))
    
  #Fit the model again so that it can be refered outside the function:
    model.fit(data[predictors],data[outcome]) 
predictor_var = ['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']
outcome_var='Outcome'
model=LogisticRegression()
classification_model(model,df_train,predictor_var,outcome_var)

If any one can help in this please, it will be great.

I tried to change the arguments but still getting the same error

0 Answers0