Created
June 12, 2018 02:02
-
-
Save shabazpatel/3d547407a6a3086b62fe085e9931c2f1 to your computer and use it in GitHub Desktop.
Prediction using a sklearn based model trained on Iris data set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pickle | |
| import random | |
| random.seed(3) | |
| iris_model = None | |
| Species_class_map = None | |
| def load_iris_model(): | |
| # load the pre-trained Iris model (here we are using a model | |
| # pre-trained on Iris Classification using sklearn | |
| global iris_model | |
| global Species_class_map | |
| iris_model = pickle.load(open('model.dat', 'rb')) | |
| Species_class_map = {0:'Iris-setosa', 1:'Iris-versicolor', 2:'Iris-virginica'} | |
| # Load models | |
| print(("* Loading Iris model")) | |
| load_iris_model() | |
| def predict(self, params): | |
| # take input pd data frame and return dictionary with classificaiton | |
| X = params['X'] | |
| # Test feature | |
| y_pred = iris_model.predict(X) | |
| y_pred = [round(value) for value in y_pred] | |
| prediction_result = {'Species': Species_class_map[y_pred[0]]} | |
| return prediction_result | |
| # if this is the main thread of execution first load the model and | |
| # then start the server | |
| # if __name__ == "__main__": | |
| # print predict({'X': [[ 6.9, 3.2, 5.7, 2.3]]}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment