0
votes

Does anybody know how i can remove the error below?

NameError                                 Traceback (most recent call last)
<ipython-input-31-d3625a93ead4> in <module>()
     11 loo = LeaveOneOut(num_of_examples)
     12 for train_index, test_index in loo:
---> 13     print("%s %s" % (train, test))
     14 

NameError: name 'train' is not defined

And the code that causing the error is like below:

from sklearn import datasets
import numpy as np
from sklearn.preprocessing import StandardScaler

iris = datasets.load_iris()
X = iris.data[:, [2, 3]]
y = iris.target
X, y
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)
num_of_examples = len(X_train_std)

This is where i am getting error:

from sklearn.cross_validation import LeaveOneOut
loo = LeaveOneOut(num_of_examples)
for train_index, test_index in loo:
    print("%s %s" % (train, test))
1
You didn't define the variables train and test in your code. - Miriam Farber
@Miriam Actually i wrote this part according to Leave One Out (LOO) of this link: scikit-learn.org/stable/modules/cross_validation.html - Shelly
See my answer below. Also, I think the relevant documentation is jaquesgrobler.github.io/Online-Scikit-Learn-stat-tut/modules/… as you import LeaveOneOut from cross_validation. - Miriam Farber

1 Answers

0
votes

You didn't define train and test in your code. If you want to print train_index and test_index you should do:

from sklearn.cross_validation import LeaveOneOut
loo = LeaveOneOut(num_of_examples)
for train_index, test_index in loo:
    print("%s %s" % (train_index, test_index))

In addition, this documentation may be relevant.