Skip to content

Instantly share code, notes, and snippets.

@Steboss89
Last active September 18, 2022 17:01
Show Gist options
  • Save Steboss89/8ba3f007239b4ae982d575e5f04071f0 to your computer and use it in GitHub Desktop.
Save Steboss89/8ba3f007239b4ae982d575e5f04071f0 to your computer and use it in GitHub Desktop.
MLflow set up
import mlflow
# all imports
# PREPROCESSING FUNCTION
# input dataframe
# clean dataframe
# set up train and test
# count vectorizer
# fit the vectorizer
# set up a client to check experiments
mlflow_client = MlflowClient(tracking_uri=mlflow_tracking_uri)
# set up models key
classifier = MultinomialNB()
model_name = "NaiveBayes" # name to display on UI
run_name = "NB_model" # name of the single run
exp_name = "FirstSentimentTest" # name of the experiment
try:
print("setting up experiment ")
experiment = mlflow.create_experiment(name = exp_name)
experiment_id = experiment.experiment_id
except:
experiment = mlflow_client.get_experiment_by_name(exp_name)
experiment_id = experiment.experiment_id
print("Set up mlflow tracking uri")
mlflow.set_tracking_uri(mlflow_tracking_uri)
# set up your model
classifier = MultinomialNB()
model_name = "NaiveBayes" # define the model name
run_name = "NB_model" # define the run name
exp_name = "FirstSentimentTest" # set up the experiment family name
# check if epxeriment exists
try:
print("setting up experiment ")
experiment = mlflow.create_experiment(name = exp_name)
experiment_id = experiment.experiment_id
except:
experiment = mlflow_client.get_experiment_by_name(exp_name)
experiment_id = experiment.experiment_id
# run the tracker
with mlflow.start_run(experiment_id=experiment_id, run_name=run_name, nested=False,):
# call the autolog
mlflow.sklearn.autolog(log_models=True,log_input_examples=True,log_model_signatures=True, )
# fit classifier
classifier.fit(X_train, y_train)
# run predictions
y_pred = classifier.predict(X_valid)
fpr, tpr, thresholds = roc_curve(y_valid, y_pred)
roc_auc = auc(fpr, tpr)
# end tracking
mlflow.end_run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment