Python tutorial: Predict ski rental with linear regression in SQL Server Machine Learning Services

APPLIES TO: yesSQL Server noAzure SQL Database noAzure Synapse Analytics (SQL DW) noParallel Data Warehouse

In this four-part tutorial series, you will use Python and linear regression in SQL Server Machine Learning Services to predict the number of ski rentals. The tutorial use a Python notebook in Azure Data Studio, but you can also use your own Python integrated development environment (IDE).

Imagine you own a ski rental business and you want to predict the number of rentals that you'll have on a future date. This information will help you get your stock, staff, and facilities ready.

In the first part of this series, you'll get set up with the prerequisites. In parts two and three, you'll develop some Python scripts in a Jupyter notebook to prepare your data and train a machine learning model. Then, in part three, you'll run those Python scripts inside SQL Server using T-SQL stored procedures.

In this article, you'll learn how to:

  • Import a sample database into SQL Server

In part two, you'll learn how to load the data from SQL Server into a Python data frame, and prepare the data in Python.

In part three, you'll learn how to train a linear regression model in Python.

In part four, you'll learn how to store the model to SQL Server, and then create stored procedures from the Python scripts you developed in parts two and three. The stored procedures will run in SQL Server to make predictions based on new data.

Prerequisites

  • SQL Server Machine Learning Services - For how to install Machine Learning Services, see the Windows installation guide or the Linux installation guide.

  • Python IDE - This tutorial uses a Python notebook in Azure Data Studio. For more information, see How to use notebooks in Azure Data Studio.

    You can also use your own Python IDE, such as a Jupyter notebook or Visual Studio Code with the Python extension and the mssql extension.

  • SQL query tool - This tutorial assumes you're using Azure Data Studio. You can also use SQL Server Management Studio (SSMS).

  • Additional Python packages - The examples in this tutorial series use the following Python packages that may not be installed by default:

    • pandas
    • pyodbc
    • sklearn

    To install these packages:

    1. In Azure Data Studio, select Manage Packages.
    2. In the Manage Packages pane, select the Add new tab.
    3. For each of the following packages, enter the package name, click Search, then click Install.

    As an alternative, you can open a Command Prompt, change to the installation path for the version of Python you use in Azure Data Studio (for example, cd %LocalAppData%\Programs\Python\Python37-32), then run pip install for each package.

Restore the sample database

The sample database used in this tutorial has been saved to a .bak database backup file for you to download and use.

  1. Download the file TutorialDB.bak.

  2. Follow the directions in Restore a database from a backup file in Azure Data Studio, using these details:

    • Import from the TutorialDB.bak file you downloaded
    • Name the target database "TutorialDB"
  3. You can verify that the restored database exists by querying the dbo.rental_data table:

    USE TutorialDB;
    SELECT * FROM [dbo].[rental_data];
    

Enable external scripts by running the following SQL commands:

sp_configure 'external scripts enabled', 1;
RECONFIGURE WITH override;

Next steps

In part one of this tutorial series, you completed these steps:

  • Installed the prerequisites
  • Import a sample database into an SQL Server

To prepare the data from the TutorialDB database, follow part two of this tutorial series: