5
$\begingroup$

I would like to use K-fold cross-validation on my data of my model.

My codes in Keras is :

a = np.array(  
[[283, 95, 72, 65],
[290, 100, 80, 72],
[120,170,130,122],
[100,230,110,200],
[300,100,200,500]]
)
X = a[:,0:2]
Y = a[:,3]

from sklearn.model_selection import KFold, cross_val_score
k_fold = KFold(n_splits=3)    
model = models.Sequential()
model.add(Dense(12, input_shape=(3,)))
model.add(LeakyReLU())
model.summary()

cross_val_score(model,X,Y)

But, It makes this error:

If no scoring is specified, the estimator passed should have a 'score' method. The estimator does not.

And when I select a scoring parameter as:

cross_val_score(model,X,Y, scoring= 'accuracy')

It makes another error:

TypeError: Cannot clone object '' (type ): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.

How can I use K-fold cross-validation on this model?

Thank you

$\endgroup$

2 Answers 2

6
$\begingroup$

The cross_val_score seems to be dependent on the model being from sk-learn and having a get_params method. Since your Keras implementation does not have this, it can't provide the necessary information to do the cross_val_score. Try the manual k-fold cross validation found here: https://machinelearningmastery.com/evaluate-performance-deep-learning-models-keras/

$\endgroup$
0
$\begingroup$

There is a nice SKLearn wrapper for keras:

https://keras.io/scikit-learn-api/

I have not personally tested cross validation using the wrapper, but I anticipate it will work since everything else I've tried with the wrapper (no matter how strange) worked as expected.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.