Coder Social home page Coder Social logo

Comments (5)

JonnoFTW avatar JonnoFTW commented on May 27, 2024

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

from hyperas.

asifrpa avatar asifrpa commented on May 27, 2024

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 hyperas.

asifrpa avatar asifrpa commented on May 27, 2024

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)

from hyperas.

asifrpa avatar asifrpa commented on May 27, 2024

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

from hyperas.

JonnoFTW avatar JonnoFTW commented on May 27, 2024

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

from hyperas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.