import pickle
# Train a Random Forest Classifier
rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)
rf_classifier.fit(X_train, y_train)
# Make predictions on the test set
y_pred = rf_classifier.predict(X_test)
# Save the model using pickle
# give the file a name
model_filename = 'rf_classifier_iris_version1.pkl'
try:
with open(model_filename, 'wb') as file:
pickle.dump(rf_classifier, file)
print(f"Model saved successfully to {model_filename}")
except Exception as e:
print(f"Error saving model: {e}")
# Load the model using pickle
try:
with open(model_filename, 'rb') as file:
loaded_model = pickle.load(file)
print(f"Model loaded successfully from {model_filename}")
except Exception as e:
print(f"Error loading model: {e}")
import mlflow
import mlflow.sklearn
import shutil
model_path = "models/logit_games_v1"
#shutil.rmtree(model_path)
mlflow.sklearn.save_model(model, model_path)
loaded = mlflow.sklearn.load_model(model_path)
print(loaded.predict_proba(x))
# You can also do this for deep learning models
import tensorflow as tf
import keras
from keras import models, layers
# define the network structure
model = models.Sequential()
model.add(layers.Dense(64,activation='relu',input_shape=(10,)))
model.add(layers.Dropout(0.1))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
def auc(y_true, y_pred):
auc = tf.metrics.auc(y_true, y_pred)[1]
keras.backend.get_session().run(
tf.local_variables_initializer())
return auc
model.compile(optimizer='rmsprop',
loss='binary_crossentropy', metrics=[tf.keras.metrics.AUC()])
history = model.fit(x, y, epochs=100, batch_size=100,
validation_split = .2, verbose=0)
# SAVE & LOAD the deep learning model in .h5 format
from keras.models import load_model
model.save("games.h5")
model = load_model('games.h5', custom_objects={'auc': auc})
model.evaluate(x, y, verbose = 0)
# Define a simple webservice: Ping -> Pong
from flask import Flask
from flask import request
# Define a simple Flask web application with the name "ping"
app = Flask("ping")
# Create a GET route for '/ping' that returns "PONG"
@app.route('/ping', methods=["GET"])
def ping():
return "PONG"
# Create a POST route for '/pingme' that echoes back the JSON data sent in the request
@app.route('/pingme', methods=["POST"])
def pingme():
data = request.get_json()
return data
# Run the Flask application if this script is executed directly
if __name__ == "__main__":
# Run the app on localhost, port 1010.
# Exposes both endpoints, /ping and /pingme
app.run(debug=True, host='0.0.0.0', port=1010)
# See whats up at local host ping
curl <http://localhost:1010/ping>
# Send some data to our app
curl -X POST -H "Content-Type: application/json" -d '{"message": "Hello, Flask!"}' <http://localhost:1010/pingme>
Suppose you have trained your model and are now ready to make it live to make inference.
Well, you save your model from Step1, then download Flask and create your predict.py file.