Save the Model to Disk
- Create a JSON representation of your model and store it in a variable called model_json.
- Write the JSON representation to a file called model.json in the path specified in the variable outpath.
Reference Material
https://docs.python.org/3.6/library/os.path.html
https://www.pythonforbeginners.com/files/with-statement-in-python
https://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
Serialize the model to JSON like this:
model_json = model.to_json()
Write the model to disk:
with open(os.path.join (outpath, "model.json"), "w") as json_file: json_file.write(model_json)
# serialize model to JSON model_json = model.to_json() with open(os.path.join (outpath, "model.json"), "w") as json_file: json_file.write(model_json)
- Import the Pickle library.
- Using the dump method of Pickle write the standscalar to disk in a file named standardscalar.pickle in outpath.
Reference Material
https://docs.python.org/3.6/library/os.path.html
https://www.pythonforbeginners.com/files/with-statement-in-python
https://docs.python.org/2/library/pickle.html
Imports:
import pickle
Pickle the standardscalar:
with open(os.path.join (outpath, "standardscalar.pickle"), 'wb') as handle: pickle.dump(sc, handle, protocol=pickle.HIGHEST_PROTOCOL)
#serialize the scalar to pickle import pickle with open(os.path.join (outpath, "standardscalar.pickle"), 'wb') as handle: pickle.dump(sc, handle, protocol=pickle.HIGHEST_PROTOCOL)
- Use the save_weights method of the Sequencer class to serialize the weights to disk.
Reference Material
https://keras.io/models/about-keras-models/
https://docs.python.org/3.6/library/os.path.html
Set outpath to the folder out in the current folder:
# serialize weights to HDF5 model.save_weights(os.path.join (outpath, "model.h5"))
Predict for New Input
- Use the predict method of the Sequential model to generate a new prediction. Don’t forget to scale your inputs with the same StandardScalar used prior to training the model. Use the following parameters:
- Route To Market_Other = 0
- Route To Market_Reseller = 1
- Route To Market Telecoverage = 0
- Route To Market Telesales = 0
- Sales Stage Change Count = 9
- Total Days Identified Through Closing = 119
- Total Days Identified Through Qualified = 97
- Revenue From Client Past Two Years = 0
- Ratio Days Identified To Total Days = .81
- Ratio Days Validated To Total Days = .644
- Ratio Days Qualified To Total Days = .245
- Deal Size Category = 6
- Print either win or loss based on the results of the prediction.
Reference Material
https://keras.io/models/sequential/
http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html
Scale your input like this:
sc.transform(input)
Make the prediction like this:
new_prediction = model.predict(sc.transform(input))
Create a float32 array as input:
input = np.array([[0,1,0,0,9,119,97,0,.81,.644,.245,6]]).astype('float32')
Output the result:
if (new_prediction > 0.5): print ("Won") else: print ("Loss")
#Make a prediction new_prediction = model.predict(sc.transform(np.array([[0,1,0,0,9,119,97,0,.81,.644,.245,6]]))) if (new_prediction > 0.5): print ("Won") else: print ("Loss")
Lab Complete!