Did you know that you can execute R and Python code remotely in SQL Server from Jupyter Notebooks or any IDE? Machine Learning Services in SQL Server eliminates the need to move data around. Instead of transferring large and sensitive data over the network or losing accuracy on ML training with sample csv files, you can have your R/Python code execute within your database. You can work in Jupyter Notebooks, RStudio, PyCharm, VSCode, Visual Studio, wherever you want, and then send function execution to SQL Server bringing intelligence to where your data lives.
This tutorial will show you an example of how you can send your python code from Juptyter notebooks to execute within SQL Server. The same principles apply to R and any other IDE as well. If you prefer to learn through videos, this tutorial is also published on YouTube.
In order for R or Python to execute within SQL, you first need the Machine Learning Services feature installed and configured. See this how-to guide.
In order to send Python execution to SQL from Jupyter Notebooks, you need to use Microsoft's RevoscalePy package. To get RevoscalePy, download and install Microsoft's ML Services Python Client. Documentation Page or Direct Download Link (for Windows).
After downloading, open powershell as an administrator and navigate to the download folder. Start the installation with this command (feel free to customize the install folder):
.\Install-PyForMLS.ps1 -InstallFolder "C:\Program Files\MicrosoftPythonClient"
Be patient while the installation can take a little while. Once installed navigate to the new path you installed in. Let's make an empty folder and open Jupyter Notebooks:
mkdir JupyterNotebooks; cd JupyterNotebooks; ..\Scripts\jupyter-notebook
Create a new notebook with the Python 3 interpreter:
To test if everything is setup, import revoscalepy in the first cell and execute. If there are no error messages you are ready to move forward.
For the rest of the tutorial you can clone this Jupyter Notebook from Github if you don't want to copy paste all of the code. This database setup is a one time step to ensure you have the same data as this tutorial. You don't need to perform any of these setup steps to use your own data.
Modify the connection string for your server and use pyodbc to create a new database.
Iris is a popular dataset for beginner data science tutorials. It is included by default in sklearn package.
(You can also do this with pyodbc, sqlalchemy or other packages)
Write any python code you want to execute in SQL. In this example we are creating a scatter matrix on the iris dataset and only returning the bytestream of the .png back to Jupyter Notebooks to render on our client.
Now that we are finally set up, check out how easy sending remote execution really is! First,
import revoscalepy. Create a
sql_compute_context, and then send the execution of any function seamlessly to SQL Server with
RxExec. No raw data had to be transferred from SQL to the Jupyter Notebook. All computation happened within the database and only the image file was returned to be displayed.
While this example is trivial with the Iris dataset, imagine the additional scale, performance, and security capabilities that you now unlocked. You can use any of the latest open source R/Python packages to build Deep Learning and AI applications on large amounts of data in SQL Server. We also offer leading edge, high-performance algorithms in Microsoft's RevoScaleR and RevoScalePy APIs. Using these with the latest innovations in the open source world allows you to bring unparalleled selection, performance, and scale to your applications.
Check out SQL Machine Learning Services Documentation to learn how you can easily deploy your R/Python code with SQL stored procedures making them accessible in your ETL processes or to any application. Train and store machine learning models in your database bringing intelligence to where your data lives.
Basic R and Python Execution in SQL Server: https://aka.ms/BasicMLServicesExecution
Set up Machine Learning Services in SQL Server: https://aka.ms/SetupMLServices
End-to-end tutorial solutions on Github: https://microsoft.github.io/sql-ml-tutorials/
Other YouTube Tutorials:
How to Install SQL Server Machine Learning Services: https://aka.ms/InstallMLServices
How to Enable SQL Server Machine Learning Services: https://aka.ms/EnableMLServices
Basics of R and Python Execution in SQL: https://aka.ms/ExecuteMLServices
Keywords: #MachineLearning #DataScience #Python #R #Jupyter #JupyterNotebook #JupyterNotebooks #RStudio #SQL #SQLServer #BigData #DataAnalytics #ScikitLearn #Data #AI #ArtificialIntelligence