Book Image

Data Science with SQL Server Quick Start Guide

By : Dejan Sarka
Book Image

Data Science with SQL Server Quick Start Guide

By: Dejan Sarka

Overview of this book

SQL Server only started to fully support data science with its two most recent editions. If you are a professional from both worlds, SQL Server and data science, and interested in using SQL Server and Machine Learning (ML) Services for your projects, then this is the ideal book for you. This book is the ideal introduction to data science with Microsoft SQL Server and In-Database ML Services. It covers all stages of a data science project, from businessand data understanding,through data overview, data preparation, modeling and using algorithms, model evaluation, and deployment. You will learn to use the engines and languages that come with SQL Server, including ML Services with R and Python languages and Transact-SQL. You will also learn how to choose which algorithm to use for which task, and learn the working of each algorithm.
Table of Contents (15 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Predicting with T-SQL


The last question to answer is how you use the R and Python models in T-SQL. Of course, you can create the models and train them directly with the sys.sp_execute_external_script stored procedure. However, it would make no sense to retrain the complete model on the same training set for every new prediction, even if it is prediction on a single case. SQL Server 2017 introduces the PREDICT() function. Using this function means performing native predictions in SQL Server. 

In order to use the PREDICT() function, you need to serialize the model in a SQL Server table, in a VARBINARY(MAX) column. You don't even have to have the ML Services (In-Database) installed in the SQL Server where you serialize your models to do the native predictions. The following code creates the table for the models:

CREATE TABLE dbo.dsModels
(Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
 ModelName NVARCHAR(50) NOT NULL,
 Model VARBINARY(MAX) NOT NULL);
GO

Unfortunately, not all models are supported...