Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I would like to define a new scoring in GridSearchCV as it is said here
http://scikit-learn.org/stable/modules/model_evaluation.html#implementing-your-own-scoring-object
. This is my code:
from sklearn.model_selection import GridSearchCV
def pe_score(estimator,x,y):
clf=estimator
clf.fit(x,y)
z=clf.predict(x)
pe=prob_error(z, y)
return pe
pe_error=pe_score(SVC(),xTrain,yTrain)
grid = GridSearchCV(SVC(), param_grid={'kernel':('linear', 'rbf'), 'C':[1, 10, 100,1000,10000]}, scoring=pe_error)
where prob_error(z,y) is the function that computes the error which I would like to minimize, being z the prediction of the training set and y the true values of the training set. However, I got the following error:
---> 18 clf.fit(xTrain, yTrain)
TypeError: 'numpy.float64' object is not callable
I don't know if the format of pe_error it is well defined. How can I solve it? Thank you.
–
–
Score functions should have the format score_func(y, y_pred, **kwargs)
You can then use the make_scorer function to take your scoring function and get it to work with GridSearchCV.
So, in this case it would be:
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import make_scorer
clf = estimator
clf.fit(x,y)
z = clf.predict(x)
def pe_score(y, y_pred):
pe = prob_error(y_pred, y)
return pe
pe_error = make_scorer(pe_score)
grid = GridSearchCV(SVC(), param_grid={'kernel':('linear', 'rbf'), 'C':[1, 10, 100,1000,10000]}, scoring= pe_error)
(I'm assuming you have prob_error implemented or imported somewhere else in your code)
Documentation: http://scikit-learn.org/stable/modules/generated/sklearn.metrics.make_scorer.html
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.