I'm attempting to do a comparison of different algorithms, to see which is best for my problem.

I'm trying out the code directly from this tutorial: https://machinelearningmastery.com/machine-learning-in-python-step-by-step/

Particularly in the code below:

My imports

import sys
import pandas as pd
import scipy as sp
import sklearn as sk
import numpy as np
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix

from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold

from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier

Spot Check Algorithms

models = []
models.append(('LR', LogisticRegression(solver='liblinear', multi_class='ovr')))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC(gamma='auto')))

evaluate each model in turn

results = []
names = []
for name, model in models:
    kfold = model_selection.KFold(n_splits=10, random_state=seed)
    cv_results = model_selection.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring)
    results.append(cv_results)
    names.append(name)
    msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
    print(msg)

When I run this, I keep getting (<---- on line 12):

NameError Traceback (most recent call last)
<ipython-input-25-e6a861b6e218> in <module>()
     10 names = []

     11 for name, model in models:

     12         kfold = model_selection.KFold(n_splits=10, random_state=seed) <----

     13         cv_results = model_selection.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring)

     14         results.append(cv_results)

NameError: name 'model_selection' is not defined

Can someone explain to me how KFold is working exactly and why it won't accept the instance?

1 Answers

1
RafaelC On

KFold is part of sklearn.model_selection module.

Make sure to import the name into your workspace, either by doing

from sklearn import model_selection   

and using

model_selection.KFold

or

import sklearn.model_selection
sklearn.model_selection.KFold

or even

from sklearn.model_selection import KFold
KFold