Build a predictive model with Python using SQL Server 2017 Machine Learning Services
This sample shows how to create a predictive model in Python and operationalize it with SQL Server 2017
Contents
About this sample
Before you begin
Sample details
About this sample
Predictive modeling is a powerful way to add intelligence to your application. It enables applications to predict outcomes against new data. The act of incorporating predictive analytics into your applications involves two major phases: model training and model operationalization.
In this sample, you will learn how to create a predictive model in python and operationalize it with SQL Server vNext.
- Applies to: SQL Server 2017 CTP2.0 or higher
- Key features: SQL Server Machine Learning Services
- Workload: SQL Server Machine Learning Services
- Programming Language: T-SQL, Python
- Authors: Nellie Gustafsson
- Update history: Getting started tutorial for SQL Server ML Services - Python
Before you begin
To run this sample, you need the following prerequisites:
Download this DB backup file and restore it using Setup.sql.
Software prerequisites:
- SQL Server 2017 CTP2.0 (or higher) with Machine Learning Services (Python) installed
- SQL Server Management Studio
- Python Tools for Visual Studio or another Python IDE
Run this sample
- From SQL Server Management Studio, or SQL Server Data Tools, connect to your SQL Server 2017 database and execute setup.sql to restore the sample DB you have downloaded
- From SQL Server Management Studio or SQL Server Data Tools, open the rental_prediction.sql script
This script sets up:
Necessary tables
Creates stored procedure to train a model
Creates a stored procedure to predict using that model
Saves the predicted results to a DB table - You can also try the Python script on its own, connecting to SQL Server and getting data using RevoScalePy Rx functions. Just remember to point the Python environment to the corresponding path "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES" if you run in-db Python Server, or "C:\Program Files\Microsoft SQL Server\140\PYTHON_SERVER" if you have the standalone Machine Learning Server installed.
Sample details
This sample shows how to create a predictive model with Python and generate predictions using the model and deploy that in SQL Server with SQL Server Machine Learning Services.
rental_prediction.py
The Python script that generates a predictive model and uses it to predict rental counts
rental_prediction.sql
Takes the Python code in rental_prediction.py and deploys it inside SQL Server. Creating stored procedures and tables for training, storing models and creating stored procedures for prediction.
setup.sql
Restores the sample DB (Make sure to update the path to the .bak file)