Search code examples
jsonmachine-learningdeploymentweb-applicationsmulticlass-classification

How to preprocess the new dataset while model deployement using flask


data = dataset.iloc[:,:-1].values
label = dataset.iloc[:,-1].values
from sklearn.preprocessing import LabelEncoder
labelencoder = LabelEncoder()

for i in range(0,5):
    data[:,i] = labelencoder.fit_transform(data[:,i]) 
data1=pd.DataFrame(data[:,:5])
for i in range(7,12):
    data[:,i]=labelencoder.fit_transform(data[:,i])
data2=pd.DataFrame(data[:,7:12])
from sklearn.preprocessing import Normalizer

#----Normalizing Uncategorical Data----#
data3=dataset.iloc[:,[5,6,12]]
dataset.iloc[:,:5]
normalized_data = Normalizer().fit_transform(data3)
data3=pd.DataFrame(normalized_data)

data_full=pd.concat([data1,data2,data3],axis=1)

label=labelencoder.fit_transform(label)
label=pd.DataFrame(label)

Above are my preprocessing steps...the same i want to do to new input data after model deployment through web app. How to write a function for this..? i am using flask for developing apis

What to write under predict fund...? in app.py

@app.route('/predict' methods= 'POST' )
def predict():

Solution

  • You will have to pickle all the transformers that you are using while pre-processing your data. Then you will have to load the same transformers and use them during predictions.

    Creating a new transformer and fitting it on different value will give your weird predictions.

    I created a demo flask project for a meetup. It has all the code that you need.

    Deployment: https://github.com/Ankur-singh/flask_demo/blob/master/final_ml_flask.py

    Training: https://github.com/Ankur-singh/flask_demo/blob/master/iris.py