-
-
Notifications
You must be signed in to change notification settings - Fork 318
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
Comments
Have you defined or imported the function anywhere in your code? |
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 |
from sklearn.neighbors import KNeighborsClassifier ImportError Traceback (most recent call last) ImportError: cannot import name 'train_predict' from 'sklearn.model_selection' (C:\Users\MyPc\Anaconda3\lib\site-packages\sklearn\model_selection_init_.py) |
def train_clf(clf, X_train, y_train):
def pred_clf(clf, features, target):
def train_predict(clf, X_train, y_train, X_test, y_test):
|
If your methods are so short, consider just putting them inside your main function that you want to optimise. Additionally, no such function |
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
The text was updated successfully, but these errors were encountered: