diff --git a/samples/features/sql-big-data-cluster/machine-learning/sql/python/README.md b/samples/features/sql-big-data-cluster/machine-learning/sql/python/README.md index 25b6af05..2af9a001 100644 --- a/samples/features/sql-big-data-cluster/machine-learning/sql/python/README.md +++ b/samples/features/sql-big-data-cluster/machine-learning/sql/python/README.md @@ -6,7 +6,7 @@ SQL Server 2016 added capability to run R script from T-SQL. SQL Server 2017 add **Applies to:** SQL Server 2017+, SQL Server 2019, SQL Server 2019 big data cluster -In this example, we are building a machine learning model using Python and a logistic regression algorithm for a recommendation engine on an online store. Based on existing users' click pattern online and their interest in other categories and demographics, we are training a machine learning model. This model will then be used to predict if the visitor is interested in a given item category using the T-SQL PREDICT function. +In this example, we are building a machine learning model using Python. The script uses a logistic regression algorithm from revoscalepy package in Microsoft ML Server. Based on existing users' click pattern online and their interest in other categories and demographics, we are training a machine learning model. This model will then be used to predict if the visitor is interested in a given item category using the T-SQL PREDICT function. [book-click-prediction-partitioned-py.sql](book-click-prediction-partitioned-py.sql/) @@ -14,6 +14,12 @@ In this example, we are building a machine learning model using Python and a log In this example, we are leveraging the new partitioning support (SQL Server 2019) in sp_execute_external_script to partition the input data and run the Python script per partition. So we will modify the training script to train model per group of users based on credit rating. The Python script will produce N models for the same input data set. +[book-click-prediction-sklearn-py.sql](book-click-prediction-sklearn-py.sql/) + +**Applies to:** SQL Server 2017+, SQL Server 2019 big data cluster + +In this example, we are building a machine learning model using Python. The script uses a logistic regression algorithm from sklearn package. In SQL Server 2017 or SQL Server 2019, you need to install ***sklearn*** package before running the SQL script. + ## Instructions 1. Connect to SQL Server or SQL Server Master instance. diff --git a/samples/features/sql-big-data-cluster/machine-learning/sql/python/book-click-prediction-partitioned-py.sql b/samples/features/sql-big-data-cluster/machine-learning/sql/python/book-click-prediction-partitioned-py.sql index e9fbea7f..dd057357 100644 Binary files a/samples/features/sql-big-data-cluster/machine-learning/sql/python/book-click-prediction-partitioned-py.sql and b/samples/features/sql-big-data-cluster/machine-learning/sql/python/book-click-prediction-partitioned-py.sql differ diff --git a/samples/features/sql-big-data-cluster/machine-learning/sql/python/book-click-prediction-sklearn-py.sql b/samples/features/sql-big-data-cluster/machine-learning/sql/python/book-click-prediction-sklearn-py.sql new file mode 100644 index 00000000..76df9f45 --- /dev/null +++ b/samples/features/sql-big-data-cluster/machine-learning/sql/python/book-click-prediction-sklearn-py.sql @@ -0,0 +1,140 @@ +USE sales +GO + +-- Inspect top 100 rows +-- +SELECT TOP(100) * FROM web_clickstreams_hdfs_book_clicks; +GO + +-- Step #1a +-- Create the training stored procedure +CREATE OR ALTER PROCEDURE [dbo].[train_book_category_visitor_sklearn_python] +(@model_name varchar(100)) +AS +BEGIN + DECLARE @model varbinary(max) + , @input_query nvarchar(max) + , @train_script nvarchar(max) + + -- Set the input query for training. We will use 80% of the data. + SET @input_query = N' +SELECT TOP(80) PERCENT SIGN(q.clicks_in_category) AS book_category + , q.college_education + , q.male + , q.clicks_in_1 + , q.clicks_in_2 + , q.clicks_in_3 + , q.clicks_in_4 + , q.clicks_in_5 + , q.clicks_in_6 + , q.clicks_in_7 + , q.clicks_in_8 + , q.clicks_in_9 + FROM web_clickstreams_hdfs_book_clicks as q +'; + -- Python script that uses logistic regression function from sklearn package to generate model to predict book_category click(s). + SET @train_script = N' +model = bytes() + +# build classification model to predict book_category +import pickle +from sklearn.linear_model import LogisticRegression + +# 1. instantiate model +logreg = LogisticRegression( solver="lbfgs") + +# 2. fit and finalize the model +feature_cols = ["college_education", "male", "clicks_in_1", "clicks_in_2","clicks_in_3","clicks_in_4","clicks_in_5","clicks_in_6","clicks_in_7","clicks_in_8","clicks_in_9"] +logit_model = logreg.fit(indata[feature_cols], indata["book_category"]) + +model = pickle.dumps(logit_model) +'; + + -- Generate sales model using Python script with the book clicks stats for each user + EXECUTE sp_execute_external_script + @language = N'Python' + , @script = @train_script + , @input_data_1 = @input_query + , @input_data_1_name = N'indata' + , @params = N'@model varbinary(max) OUTPUT' + , @model = @model OUTPUT; + + -- Save the trained model to predict user clicks on book category in the website + DELETE FROM sales_models WHERE model_name = @model_name; + INSERT INTO sales_models (model_name, model) VALUES(@model_name, @model); +END; +GO + + +-- Step #1b +-- Train the book category prediction model: +DECLARE @model_name varchar(100) = 'category_model - sklearn (Python)'; +EXECUTE dbo.train_book_category_visitor_sklearn_python @model_name; +SELECT * FROM sales_models WHERE model_name = @model_name; +GO + +-- Step #2a +-- Predict the book category clicks for new users based on their pattern of +-- visiting various categories in the web site +CREATE OR ALTER PROCEDURE [dbo].[predict_book_category_visitor_sklearn_python] +(@model_name varchar(100), @top_percent int = 20) +AS +BEGIN + DECLARE @model varbinary(max) = (SELECT model FROM sales_models WHERE model_name = @model_name) + , @input_query nvarchar(max) + , @predict_script nvarchar(max); + + -- Set the input query for scoring. We will use 20% of the data by default + SET @input_query = N' +SELECT TOP(@top_count_value) PERCENT SIGN(q.clicks_in_category) AS book_category + , q.college_education + , q.male + , q.clicks_in_1 + , q.clicks_in_2 + , q.clicks_in_3 + , q.clicks_in_4 + , q.clicks_in_5 + , q.clicks_in_6 + , q.clicks_in_7 + , q.clicks_in_8 + , q.clicks_in_9 + FROM web_clickstreams_hdfs_book_clicks as q +'; + + -- Scoring script that uses sklearn logistic regression model to predict book_category click(s) + SET @predict_script = N' +import pandas as pd +import pickle + +logit_model = pickle.loads(model) + +feature_cols = ["college_education", "male", "clicks_in_1", "clicks_in_2","clicks_in_3","clicks_in_4","clicks_in_5","clicks_in_6","clicks_in_7","clicks_in_8","clicks_in_9"] + +predictions = logit_model.predict(indata[feature_cols]) + +predictions_df = pd.DataFrame(predictions, columns = ["book_category_prediction"]) +outdata = pd.concat([predictions_df, indata], axis = 1, copy = False) +'; + + -- Predict the book category click based on the sklearn model + EXECUTE sp_execute_external_script + @language = N'Python' + , @script = @predict_script + , @input_data_1 = @input_query + , @input_data_1_name = N'indata' + , @output_data_1_name = N'outdata' + , @params = N'@model varbinary(max), @top_count_value int' + , @model = @model + , @top_count_value = @top_percent + WITH RESULT SETS ((book_category_prediction bit, book_category_actual bit, college_education varchar(30), male bit, + clicks_in_1 int, clicks_in_2 int, clicks_in_3 int, clicks_in_4 int, clicks_in_5 int, + clicks_in_6 int, clicks_in_7 int, clicks_in_8 int, clicks_in_9 int)); +END +GO + +-- Step #2b +-- Predict the book category clicks for new users based on their pattern of +-- visiting various categories in the web site +DECLARE @model_name varchar(100) = 'category_model - sklearn (Python)'; +EXECUTE dbo.predict_book_category_visitor_sklearn_python @model_name, 1 /* Score only on 1 PERENT for testing purpose. */; +GO diff --git a/samples/features/sql-big-data-cluster/machine-learning/sql/r/book-click-prediction-partitioned-r.sql b/samples/features/sql-big-data-cluster/machine-learning/sql/r/book-click-prediction-partitioned-r.sql index d71d3746..cbedffc3 100644 Binary files a/samples/features/sql-big-data-cluster/machine-learning/sql/r/book-click-prediction-partitioned-r.sql and b/samples/features/sql-big-data-cluster/machine-learning/sql/r/book-click-prediction-partitioned-r.sql differ diff --git a/samples/features/sql-big-data-cluster/machine-learning/sql/r/book-click-prediction-r.sql b/samples/features/sql-big-data-cluster/machine-learning/sql/r/book-click-prediction-r.sql index a1f015cb..8019c93b 100644 Binary files a/samples/features/sql-big-data-cluster/machine-learning/sql/r/book-click-prediction-r.sql and b/samples/features/sql-big-data-cluster/machine-learning/sql/r/book-click-prediction-r.sql differ