maxpumperla / hyperas

Keras + Hyperopt: A very simple wrapper for convenient hyperparameter optimization
http://maxpumperla.com/hyperas/
MIT License
2.18k stars 318 forks source link

NameError: name 'train_predict' is not defined #275

Open asifrpa opened 4 years ago

asifrpa commented 4 years ago

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_testcv) clf = knn.fit(X_train, y_train) ypred = 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 commented 4 years ago

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

asifrpa commented 4 years ago

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 commented 4 years ago

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_testcv) clf = knn.fit(X_train, y_train) ypred = 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 commented 4 years ago

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 commented 4 years ago

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 ```