Sign inGet started
← Back to all posts

Deploy ML models to Snowflake with Deepnote & Modelbit

By Harry Glaser

Updated on July 7, 2022

Learn how to build and train a machine learning model in Deepnote and deploy it to Snowflake using Modelbit.

Illustrative image for blog post

Modelbit offers an easy way to deploy machine learning models from notebooks directly into Snowflake. So when you need to collaborate on a model and deploy it to Snowflake, using Deepnote and Modelbit together is the perfect combination.

To start, make sure the modelbit package is in your Deepnote project’s requirements.txt. Then, go ahead and log into Modelbit like so:

63362eae157db3b16f862ab9_deepnote1.png

From there, you can call mb.datasets() to get a list of your datasets:⁠

63362ede99989d88820ffd01_deepnote2.png

⁠In this example, we'll work with an nba games dataset. It provides stat lines for every NBA player in every NBA game — fun for us Golden State Warriors fans on the West Coast! Let’s go ahead and pull that data set into a pandas DataFrame.

63362f015e159b7abb9a0812_deepnote3.png

Now that we’ve got this data set, let’s build a model. In this example, we're going to try to predict a player's position. Let’s use a RandomForestClassifier, courtesy of SciKit-Learn:

63362f4a4d1f9e9edf18ecac_deepnote4.png

As you can see, we’re using three features to predict the player’s position: the number of three-point shots they attempt (FG3A), the number of rebounds they get (REB), and the number of shots they block (BLK). 

We do some feature engineering in the next few lines, dropping unused columns and dropping rows with NaNs in any of our feature or target columns. Finally, we use SciKit-Learn’s built-in train/test split functionality to split our data set into training data and testing data.

Finally, we train a RandomForestClassifier, which we call position_classifier. Let’s see how it did.

63362f6c4b15d2654bd426c0_deepnote5.png

⁠Our model has an accuracy score of about 0.6. Not bad for a first attempt!

Let’s go ahead and deploy this model to Snowflake. To do that, we’re going to build a quick function that null-checks our inputs and builds a DataFrame out of them. Once that’s done, we can feed those inputs to our model’s predict function and return its prediction.

63362fa2bc26f7b16ff6ea98_deepnote6.png

Testing our function locally, we see that a player that attempts 15 three-pointers, while only getting one rebound and no blocks, is likely a guard. That checks out.

Now, to deploy the model, we call Modelbit’s deploy function, passing it our predictPosition function.

63362fc899989d38eb100663_deepnote7.png

⁠All done! If we click that link, we’re taken to our Modelbit account, where we can see our model in action, including sample Snowflake code, production Python environment, logs, and more:

modelbit-predictposition.png

If we go ahead and copy that sample Snowflake code into our Snowflake worksheet, we can call our model directly from Snowflake:

snowflake-query.png

From here, you can call that model every time a new row is inserted, every time you build a rollup table, or in batch on all the rows.

Harry Glaser

CEO @ Modelbit

Follow Harry on LinkedIn

Blog

Illustrative image for blog post

Beyond AI chatbots: how we tripled engagement with Deepnote AI

By Gabor Szalai

Updated on April 3, 2024

That’s it, time to try Deepnote

Get started – it’s free
Book a demo

Footer

Solutions

  • Notebook
  • Data apps
  • Machine learning
  • Data teams

Product

Company

Comparisons

Resources

  • Privacy
  • Terms

© Deepnote