Machine Learning with
SQL Server and ML Server
Bryan Cafferky
Data Solutions Enabler
Subscribe to me on:
GitHub: [Link]
Predictive Modeling with R
For Language and graphics
Predictive Modeling with R
Goals
• Data Science Collaboration
• SQL Server/Machine Learning Server
• Features and Benefits of Machine Learning Server
Predictive Modeling with R
Azure Supported Machine Learning Approaches
Custom Build Your Own Pre Built
Non Scalable Services
Scalable Scalable
• Poor Scalability • Very High • Pre Trained
• High Scalability
• Language Scalability • Easy to Use
• Support
Dependent • General API • Fitted to a
• Model Purpose
Performance
Varies
Examples:
Examples: Examples: Examples:
Cognitive Services
Open Source Commercial
© Copyright Microsoft Corporation. All rights reserved.
Predictive Modeling with R
Cognitive Services
© Copyright Microsoft Corporation. All rights reserved.
Predictive Modeling with R
Machine Learning Solutions
Partner Solutions
• Pre Built Solutions
• Customize to Your Needs
Examples
© Copyright Microsoft Corporation. All rights reserved.
Predictive Modeling with R
Scale Up vs. Scale Out
Scale Up Scale Out
Predictive Modeling with R
Scale Up vs. Scale Out
Scale Out
Predictive Modeling with R
Scale Up vs. Scale Out
Small Medium Large
Open Source Scale Up Scale Out
Predictive Modeling with R
Statistics & Data Science Roles
• Advanced Degree in Statistics
Data Scientist Model • Research/Discovery Orientated
Selection
• Conceptualize/Develop Models
• Has deep domain knowledge
Domain Expert
Feature • Blends Operational Reporting & Analytics
Selection • Some Knowledge of Statistics
• Less Complex Models/Solutions
• Focuses on Scaling Up/Out and Automation
Data Engineer
Data • May Modify Solution/Model to Scale It
Wrangling • Basic Understanding of Statistics and Models
Predictive Modeling with R
Predictive Modeling, Supervised Learning
Input Data is Fed into the Program
The Program is to Determine the Relationship Between the Input Columns
and a Result Output Column
Based on Analysis of Inputs to Result, a Model is Created to Predict the
Result Based on a Given Set of Inputs
History is used to predict the future.
Predictive Modeling with R
Applications of Predictive ModelingModeling
• What is the level of surgical risk?
• Will a person contract a certain illness?
• Will the patient be readmitted?
• Is the person a high health risk?
Predictive Modeling with R
Applications of Other Forms of Machine Learning
• Extract handwritten notes from images.
• Speech recognition and conversion to text.
• Radiology – is there a tumor on the spine?
• Genomics
• Did a patient fall out of their bed?
Predictive Modeling with R
Steps to Creating a Predictive Model
• Analyze/Clean the Data
• Determine the Columns that Predict the
Answer
• Select a Model to Use
• Train the Model
• Evaluate the Model’s Effectiveness
• Use the Model
Predictive Modeling with R
Steps to Predictive Modeling
Predictive Modeling with R
SQL Server/R Integration
Predictive Modeling with R
SQL Server Machine learning Services
SQL Server 2016 SQL Server 2017
• Extensibility Framework • Python Support (3.5.2)
• R Support (3.2.2) • R Support (3.3.3)
• Microsoft R Server • Native Scoring using PREDICT
• In-database Package
Management
Predictive Modeling with R
R Limitations
Client side only tool. No server based processing.
Data limited by client memory.
Single threaded.
Database data must be imported to be used.
Relatively slow.
No scheduling tool.
Predictive Modeling with R
Microsoft buys Revolution
Analytics
Proprietary vendor providing scalability
options.
Support for multi-threading.
Provide a shared R server.
Performant versions of common R
functions.
Data not limited by memory.
Predictive Modeling with R
SQL Server 2016: Reaching R's Potential
Stored procedures can be coded in R with the features
that entails.
SQL Server and R share memory space.
Work can be split between SQL Server and R Server.
Predictive Modeling with R
SQL Server 2016: Reaching R's Potential
Stored procedures can be coded in R with the features that entails.
SQL Server and R share memory space so no data movement.
Work can be split between SQL Server (T-SQL) and R Server.
Operationalize with SQL Agent.
Leverage all the Security Features of SQL Server.
Perfect Solution for Machine Learning with Structured Data.
Predictive Modeling with R
L Server Plus R = Value
Predictive Modeling with R
SQL Server Plus Machine Learning Server = Value
Python Python/R
Stored
and R Procedures/
Functions
Supported
Predictive Modeling with R
L Server: R Operationalized
[Link]
Predictive Modeling with R
L Server: R Operationalized
[Link]
Predictive Modeling with R
L Server/R Architecture
Predictive Modeling with R [Link]
Microsoft R Open
R Open - an open source R distribution from Microsoft that is fixed stable
release.
CRAN is always adding updates to the R language which can be unstable.
Some enhanced performance math functions are included.
Is used in place of the standard CRAN R language download.
Must be used with R scalability features.
Predictive Modeling with R
Microsoft R Client
Is a set of R packages.
Implements RScale functions but with limited scalability.
Useful for learning and development.
May be a good option when small only scale improvments
are needed.
Predictive Modeling with R
Microsoft R Server
A stand alone R execution environment.
Clients submit R requests to R Server for high speed processing.
Can be contained within other environments such as SQL Server.
Versions for Linux, Hadoop and Teradata.
Tight integration with SQL Sever.
Predictive Modeling with R
SQL Server: DeployR
Known as the R Integration Server.
An optional framework for deploying R analytics inside web, desktop, mobile, and
dashboard applications as well as backend systems.
Provides analytics services such as a predictive model as a web service.
Predictive Modeling with R
SQL Server Launchpad Service
Predictive Modeling with R
Demo
SQL Server R Services Demo
Predictive Modeling with R
Adding Python Support
Predictive Modeling with R
QL Server/ML Server with Python
#If you are running SQL Server 2017 RC1 and above: #Get the data from SQL Server Table
from revoscalepy import data_source =
RxComputeContext, RxInSqlServer, RxSqlServerData(table="dbo.rental_data",
RxSqlServerData connection_string=conn_str,
from revoscalepy import rx_import column_info=column_info)
#Connection string to connect to SQL Server instance
computeContext = RxInSqlServer(
conn_str = 'Driver=SQL connection_string = conn_str,
Server;Server=MYSQLSERVER;Database=Tu num_tasks = 1,
torialDB;Trusted_Connection=True;' auto_cleanup = False
)
#Define the columns we wish to import
column_info = {
"Year" : { "type" : "integer" }, RxInSqlServer(connection_string=conn_str,
"Month" : { "type" : "integer" }, num_tasks=1, auto_cleanup=False)
"Day" : { "type" : "integer" },
"RentalCount" : { "type" : "integer" },
"WeekDay" : {
# import data source and convert to pandas dataframe
"type" : "factor", df = [Link](rx_import(input_data =
"levels" : ["1", "2", "3", "4", "5", "6", "7"] data_source))
}, print("Data frame:", df)
"Holiday" : { # Get all the columns from the dataframe.
"type" : "factor", columns = [Link]()
"levels" : ["1", "0"]
},
# Filter the columns to remove ones we don't want to use
"Snow" : { in the training
"type" : "factor", columns = [c for c in columns if c not in ["Year"]]
Predictive Modeling
"levels" : ["1", "0"] with R
}
Take A Ways
• Data Does Not Need to Be Moved
• High Performance rx Functions Server Side
• Models Can Be Stored in SQL a Server Table
• Prediction Results Can Be Stored in a SQL Server Table
• R/Python Stored Procedures and Functions
• SQL Agent Can Be Used to Schedule Model Builds
• Use T-SQL or R/Python As Best Fits the Need
Predictive Modeling with R
Q&A
Predictive Modeling with R
QL Server/R Server Integration
Predictive Modeling with R