Search code examples
pythontensorflow-liteknn

How to convert KNN Scikit-learn Model in python to tensorflow lite model?


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?


Solution

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