Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NameError: name 'train_predict' is not defined #275

Open
asifrpa opened this issue Apr 6, 2020 · 5 comments
Open

NameError: name 'train_predict' is not defined #275

asifrpa opened this issue Apr 6, 2020 · 5 comments

Comments

@asifrpa
Copy link

asifrpa commented Apr 6, 2020

from sklearn.neighbors import KNeighborsClassifier
X_train_cv, X_test_cv, y_train_cv, y_test_cv = train_test_split(X_train, y_train, test_size = 0.3, random_state=100)
neighbors = []
accuracy = []
for n in range(3,10):
knn = KNeighborsClassifier(n_neighbors=n)
print("Number of neighbors is: {}".format(n))
train_predict(knn, X_train_cv, y_train_cv, X_test_cv, y_test_cv)
clf_ = knn.fit(X_train, y_train)
y_pred = clf_.predict(X_test)
neighbors.append(n)
accuracy.append( str(("%.2f" %(accuracy_score(y_test,y_pred)* 100) )))
accuracy.sort()
neighbors.sort()
plt.bar( list(range(3, 10)), accuracy, tick_label=neighbors, width=0.8, color="rgbymc")
plt.title("Optimizing Neighbours for KNN")
plt.xlabel("Neighbours")
plt.ylabel("accuracy")
plt.ylim(0)
plt.show()

ERROR
NameError Traceback (most recent call last)
in
6 knn = KNeighborsClassifier(n_neighbors=n)
7 print("Number of neighbors is: {}".format(n))
----> 8 train_predict(knn, X_train_cv, y_train_cv, X_test_cv, y_test_cv)
9 clf_ = knn.fit(X_train, y_train)
10 y_pred = clf_.predict(X_test)

NameError: name 'train_predict' is not defined

@JonnoFTW
Copy link
Collaborator

JonnoFTW commented Apr 7, 2020

Have you defined or imported the function anywhere in your code?

@asifrpa
Copy link
Author

asifrpa commented Apr 7, 2020

def train_predict(clf, X_train, y_train, X_test, y_test):

Defined like this....later in the code

Not imported anywhere else in the code

@asifrpa
Copy link
Author

asifrpa commented Apr 7, 2020

from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_predict
X_train_cv, X_test_cv, y_train_cv, y_test_cv = train_test_split(X_train, y_train, test_size = 0.3, random_state=100)
neighbors = []
accuracy = []
for n in range(3,10):
knn = KNeighborsClassifier(n_neighbors=n)
print("Number of neighbors is: {}".format(n))
train_predict(knn, X_train_cv, y_train_cv, X_test_cv, y_test_cv)
clf_ = knn.fit(X_train, y_train)
y_pred = clf_.predict(X_test)
neighbors.append(n)
accuracy.append( str(("%.2f" %(accuracy_score(y_test,y_pred)* 100) )))
accuracy.sort()
neighbors.sort()
plt.bar( list(range(3, 10)), accuracy, tick_label=neighbors, width=0.8, color="rgbymc")
plt.title("Optimizing Neighbours for KNN")
plt.xlabel("Neighbours")
plt.ylabel("accuracy")
plt.ylim(0)
plt.show()

ImportError Traceback (most recent call last)
in
1 from sklearn.neighbors import KNeighborsClassifier
----> 2 from sklearn.model_selection import train_predict
3 X_train_cv, X_test_cv, y_train_cv, y_test_cv = train_test_split(X_train, y_train, test_size = 0.3, random_state=100)
4 neighbors = []
5 accuracy = []

ImportError: cannot import name 'train_predict' from 'sklearn.model_selection' (C:\Users\MyPc\Anaconda3\lib\site-packages\sklearn\model_selection_init_.py)

@asifrpa
Copy link
Author

asifrpa commented Apr 7, 2020

def train_clf(clf, X_train, y_train):

 return clf.fit(X_train, y_train)

def pred_clf(clf, features, target):

y_pred = clf.predict(features)
return f1_score(target.values, y_pred, pos_label = 1)

def train_predict(clf, X_train, y_train, X_test, y_test):

train_clf(clf, X_train, y_train)

print("F1 score for training set is: {:.4f}".format(pred_clf(clf, X_train, y_train)))
print("F1 score for testing set is: {:.4f}\n".format(pred_clf(clf, X_test, y_test)))

@JonnoFTW
Copy link
Collaborator

JonnoFTW commented Apr 8, 2020

If your methods are so short, consider just putting them inside your main function that you want to optimise.

Additionally, no such function train_predict exists inside sklearn.model_selection. Also consider formatting your code so it's easier to read. Place the code between a pair of ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants