|
| 1 | +USE sales |
| 2 | +GO |
| 3 | + |
| 4 | +-- Create the training stored procedure |
| 5 | +CREATE OR ALTER PROCEDURE [dbo].[train_book_category_visitor_python_mml] |
| 6 | +(@model_name varchar(100)) |
| 7 | +AS |
| 8 | +BEGIN |
| 9 | + DECLARE @model varbinary(max) |
| 10 | + , @model_native varbinary(max) |
| 11 | + , @input_query nvarchar(max) |
| 12 | + , @train_script nvarchar(max) |
| 13 | + |
| 14 | +-- Set the input query for training. We will use 80% of the data. |
| 15 | + SET @input_query = N' |
| 16 | +SELECT TOP(80) PERCENT SIGN(q.clicks_in_category) AS book_category |
| 17 | + , q.college_education |
| 18 | + , q.male |
| 19 | + , q.clicks_in_1 |
| 20 | + , q.clicks_in_2 |
| 21 | + , q.clicks_in_3 |
| 22 | + , q.clicks_in_4 |
| 23 | + , q.clicks_in_5 |
| 24 | + , q.clicks_in_6 |
| 25 | + , q.clicks_in_7 |
| 26 | + , q.clicks_in_8 |
| 27 | + , q.clicks_in_9 |
| 28 | + FROM web_clickstreams_book_clicks as q |
| 29 | +'; |
| 30 | + -- Training R script that uses rxLogit function from RevoScaleR package (Microsoft R Server) to generate model to predict book_category click(s). |
| 31 | + SET @train_script = N' |
| 32 | +# build classification model to predict book_category |
| 33 | +from microsoftml import rx_logistic_regression |
| 34 | +from revoscalepy import rx_serialize_model |
| 35 | +import pickle |
| 36 | +
|
| 37 | +logitObj = rx_logistic_regression(formula = """ |
| 38 | + book_category ~ college_education + male + |
| 39 | + clicks_in_1 + clicks_in_2 + clicks_in_3 + clicks_in_4 + clicks_in_5 + |
| 40 | + clicks_in_6 + clicks_in_7 + clicks_in_8 + clicks_in_9 |
| 41 | +""", data = indata); |
| 42 | +
|
| 43 | +model = pickle.dumps(logitObj) |
| 44 | +'; |
| 45 | + |
| 46 | + -- Generate sales model using R scirpt with the book clicks stats for each user |
| 47 | + EXECUTE sp_execute_external_script |
| 48 | + @language = N'Python' |
| 49 | + , @script = @train_script |
| 50 | + , @input_data_1 = @input_query |
| 51 | + , @input_data_1_name = N'indata' |
| 52 | + , @params = N'@input_query nvarchar(max), @model varbinary(max) OUTPUT' |
| 53 | + , @input_query = @input_query |
| 54 | + , @model = @model OUTPUT; |
| 55 | + |
| 56 | + -- Save the trained models to predict user clicks on book category in the website |
| 57 | + DELETE FROM sales_models WHERE model_name = @model_name; |
| 58 | + INSERT INTO sales_models (model_name, model) VALUES(@model_name, @model); |
| 59 | +END; |
| 60 | +GO |
| 61 | + |
| 62 | +-- Step #1 |
| 63 | +-- Train the book category prediction model: |
| 64 | +DECLARE @model_name varchar(100) = 'category_model (Python MML)'; |
| 65 | +EXECUTE dbo.train_book_category_visitor_python_mml @model_name; |
| 66 | +SELECT * FROM sales_models WHERE model_name = @model_name; |
| 67 | +GO |
| 68 | + |
| 69 | +-- Step #2a |
| 70 | +-- Predict the book category clicks for new users based on their pattern of |
| 71 | +-- visiting various categories in the web site |
| 72 | +CREATE OR ALTER PROCEDURE [dbo].[predict_book_category_visitor_python_mml] |
| 73 | +(@model_name varchar(100), @top_percent int = 20) |
| 74 | +AS |
| 75 | +BEGIN |
| 76 | + DECLARE @model varbinary(max) = (SELECT model FROM sales_models WHERE model_name = @model_name) |
| 77 | + , @input_query nvarchar(max) |
| 78 | + , @predict_script nvarchar(max); |
| 79 | + |
| 80 | + -- Set the input query for scoring. We will use 20% of the data by default |
| 81 | + SET @input_query = N' |
| 82 | +SELECT TOP(@top_count_value) PERCENT SIGN(q.clicks_in_category) AS book_category |
| 83 | + , q.college_education |
| 84 | + , q.male |
| 85 | + , q.clicks_in_1 |
| 86 | + , q.clicks_in_2 |
| 87 | + , q.clicks_in_3 |
| 88 | + , q.clicks_in_4 |
| 89 | + , q.clicks_in_5 |
| 90 | + , q.clicks_in_6 |
| 91 | + , q.clicks_in_7 |
| 92 | + , q.clicks_in_8 |
| 93 | + , q.clicks_in_9 |
| 94 | + FROM web_clickstreams_book_clicks as q |
| 95 | +'; |
| 96 | + |
| 97 | + -- Scoring script that uses sklearn logistic regression model to predict book_category click(s) |
| 98 | + SET @predict_script = N' |
| 99 | +from microsoftml import rx_predict |
| 100 | +import pandas as pd |
| 101 | +import pickle |
| 102 | +
|
| 103 | +logit_model = pickle.loads(model) |
| 104 | +
|
| 105 | +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"] |
| 106 | +
|
| 107 | +predictions = rx_predict(logit_model, indata[feature_cols]) |
| 108 | +
|
| 109 | +predictions_df = pd.DataFrame(predictions, columns = ["PredictedLabel"]) |
| 110 | +outdata = pd.concat([predictions_df, indata], axis = 1, copy = False) |
| 111 | +'; |
| 112 | + |
| 113 | + -- Predict the book category click based on the sklearn model |
| 114 | + EXECUTE sp_execute_external_script |
| 115 | + @language = N'Python' |
| 116 | + , @script = @predict_script |
| 117 | + , @input_data_1 = @input_query |
| 118 | + , @input_data_1_name = N'indata' |
| 119 | + , @output_data_1_name = N'outdata' |
| 120 | + , @params = N'@model varbinary(max), @top_count_value int' |
| 121 | + , @model = @model |
| 122 | + , @top_count_value = @top_percent |
| 123 | + WITH RESULT SETS ((book_category_prediction bit, book_category_actual bit, college_education varchar(30), male bit, |
| 124 | + clicks_in_1 int, clicks_in_2 int, clicks_in_3 int, clicks_in_4 int, clicks_in_5 int, |
| 125 | + clicks_in_6 int, clicks_in_7 int, clicks_in_8 int, clicks_in_9 int)); |
| 126 | +END |
| 127 | +GO |
| 128 | + |
| 129 | +-- Step #2b |
| 130 | +-- Predict the book category clicks for new users based on their pattern of |
| 131 | +-- visiting various categories in the web site |
| 132 | +DECLARE @model_name varchar(100) = 'category_model (Python MML)'; |
| 133 | +EXECUTE dbo.predict_book_category_visitor_python_mml @model_name, 1 /* Score only on 1 PERCENT for testing purpose. */; |
| 134 | +GO |
0 commit comments