Modelbit offers an easy way to deploy ML models from notebooks directly into Snowflake. So when you need to collaborate on a model and then deploy it to Snowflake, using Deepnote and Modelbit together is the perfect combination.
In this post, we’ll walk you through building and training a model in Deepnote and deploying it to Snowflake using Modelbit.
To start, make sure the
modelbit package is in your Deepnote project’s
requirements.txt. Then, go ahead and log into Modelbit like so:
From there, you can call
mb.datasets() to get a list of your datasets:
In this example, we'll work with that
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 dataset into a Pandas DataFrame.
Now that we’ve got this dataset, let’s build a model. In this example, we are going try to predict a player's position. Let’s use a
RandomForestClassifier, courtesy of SciKit-Learn:
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 (
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 dataset into training data and testing data.
Finally, we train a
RandomForestClassifier, which we call
position_classifier. Let’s see how it did.
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.
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
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:
If we go ahead and copy that sample Snowflake code into our Snowflake worksheet, we can call our model directly from Snowflake:
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.
Share this post
No credit card required. Run your first notebook in seconds.