0

This is my sample code for a KNN classifier with accuracy over 90%,

sc_X = StandardScaler()

 X_train = sc_X.fit_transform(X_train)

 X_test = sc_X.transform(X_test)

 k=10
 classifier=KNeighborsClassifier(n_neighbors=k)

 classifier.fit(X_train,y_train)

 y_pred=classifier.predict(X_test)

 acc=accuracy_score(y_test, y_pred)

print("For K=",k,"-->Accuracy is:",acc) 


Am trying to convert the above listed model to a tensor flow lite model using this,

converter = lite.TFLiteConverter.from_keras_model(classifier)

tfmodel = converter.convert()

open('trained_model.tflite', 'wb').write(tfmodel)

But i am getting this error,

'KNeighborsClassifier' object has no attribute 'call'

Is there anyway to convert a trained knn model in python to tflite model?

2
  • You can't convert a sklearn model to tensorflow or save it as a keras model. Commented Jul 29, 2021 at 17:09
  • Is there any simple implementation of knn model in keras or any other type, which is also accepted by tensor flow conversion, am trying to deploy a model in an android app (using android studio) Commented Jul 30, 2021 at 10:04

1 Answer 1

1

Looks like KNeighborsClassifier is part of the sklearn library. lite.TFLiteConverter.from_keras_model supports keras models, not sklearn models. You need to build & train a Keras classifier.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a simple implementation in keras for classification models just like with sklearn libraries ?
Yup, the codelab I linked in the answer shows a simple Keras classifier training, and you can customize it from there :-)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.