|
| 1 | + |
| 2 | +USE TutorialDB; |
| 3 | + |
| 4 | +-- Table containing ski rental data |
| 5 | +SELECT * FROM [dbo].[rental_data]; |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +-------------------------- STEP 1 - Setup model table ---------------------------------------- |
| 10 | +DROP TABLE IF EXISTS rental_py_models; |
| 11 | +GO |
| 12 | +CREATE TABLE rental_py_models ( |
| 13 | + model_name VARCHAR(30) NOT NULL DEFAULT('default model') PRIMARY KEY, |
| 14 | + model VARBINARY(MAX) NOT NULL |
| 15 | +); |
| 16 | +GO |
| 17 | + |
| 18 | + |
| 19 | +-------------------------- STEP 2 - Train model ---------------------------------------- |
| 20 | +-- Stored procedure that trains and generates an R model using the rental_data and a decision tree algorithm |
| 21 | +DROP PROCEDURE IF EXISTS generate_rental_py_model; |
| 22 | +go |
| 23 | +CREATE PROCEDURE generate_rental_py_model (@trained_model varbinary(max) OUTPUT) |
| 24 | +AS |
| 25 | +BEGIN |
| 26 | + EXECUTE sp_execute_external_script |
| 27 | + @language = N'Python' |
| 28 | + , @script = N' |
| 29 | +import pandas as pd |
| 30 | +df = pd.DataFrame(rental_train_data) |
| 31 | +print(df) |
| 32 | +
|
| 33 | +# Get all the columns from the dataframe. |
| 34 | +columns = df.columns.tolist() |
| 35 | +
|
| 36 | +
|
| 37 | +# Store the variable well be predicting on. |
| 38 | +target = "RentalCount" |
| 39 | +
|
| 40 | +from sklearn.linear_model import LinearRegression |
| 41 | +
|
| 42 | +# Initialize the model class. |
| 43 | +lin_model = LinearRegression() |
| 44 | +# Fit the model to the training data. |
| 45 | +lin_model.fit(df[columns], df[target]) |
| 46 | +
|
| 47 | +import pickle |
| 48 | +#Before saving the model to the DB table, we need to convert it to a binary object |
| 49 | +trained_model = pickle.dumps(lin_model) |
| 50 | +' |
| 51 | + |
| 52 | + , @input_data_1 = N'select "RentalCount", "Year", "Month", "Day", "WeekDay", "Snow", "Holiday" from dbo.rental_data where Year < 2015' |
| 53 | + , @input_data_1_name = N'rental_train_data' |
| 54 | + , @params = N'@trained_model varbinary(max) OUTPUT' |
| 55 | + , @trained_model = @trained_model OUTPUT; |
| 56 | +END; |
| 57 | +GO |
| 58 | + |
| 59 | +------------------- STEP 3 - Save model to table ------------------------------------- |
| 60 | +TRUNCATE TABLE rental_py_models; |
| 61 | + |
| 62 | +DECLARE @model VARBINARY(MAX); |
| 63 | +EXEC generate_rental_py_model @model OUTPUT; |
| 64 | + |
| 65 | +INSERT INTO rental_py_models (model_name, model) VALUES('linear_model', @model); |
| 66 | + |
| 67 | +SELECT * FROM rental_py_models; |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +------------------ STEP 4 - Use the model to predict number of rentals -------------------------- |
| 72 | +DROP PROCEDURE IF EXISTS py_predict_rentalcount; |
| 73 | +GO |
| 74 | +CREATE PROCEDURE py_predict_rentalcount (@model varchar(100)) |
| 75 | +AS |
| 76 | +BEGIN |
| 77 | + DECLARE @py_model varbinary(max) = (select model from rental_py_models where model_name = @model); |
| 78 | + |
| 79 | + EXEC sp_execute_external_script |
| 80 | + @language = N'Python' |
| 81 | + , @script = N' |
| 82 | +
|
| 83 | +
|
| 84 | +import pickle |
| 85 | +rental_model = pickle.loads(py_model) |
| 86 | +
|
| 87 | +import pandas as pd |
| 88 | +df = pd.DataFrame(rental_score_data) |
| 89 | +#print(df) |
| 90 | +
|
| 91 | +# Get all the columns from the dataframe. |
| 92 | +columns = df.columns.tolist() |
| 93 | +# Filter the columns to remove ones we dont want. |
| 94 | +# columns = [c for c in columns if c not in ["Year"]] |
| 95 | +
|
| 96 | +# Store the variable well be predicting on. |
| 97 | +target = "RentalCount" |
| 98 | +
|
| 99 | +# Generate our predictions for the test set. |
| 100 | +lin_predictions = rental_model.predict(df[columns]) |
| 101 | +print(lin_predictions) |
| 102 | +
|
| 103 | +# Import the scikit-learn function to compute error. |
| 104 | +from sklearn.metrics import mean_squared_error |
| 105 | +# Compute error between our test predictions and the actual values. |
| 106 | +lin_mse = mean_squared_error(linpredictions, df[target]) |
| 107 | +#print(lin_mse) |
| 108 | +
|
| 109 | +import pandas as pd |
| 110 | +predictions_df = pd.DataFrame(lin_predictions) |
| 111 | +OutputDataSet = pd.concat([predictions_df, df["RentalCount"], df["Month"], df["Day"], df["WeekDay"], df["Snow"], df["Holiday"], df["Year"]], axis=1) |
| 112 | +' |
| 113 | + , @input_data_1 = N'Select "RentalCount", "Year" ,"Month", "Day", "WeekDay", "Snow", "Holiday" from rental_data where Year = 2015' |
| 114 | + , @input_data_1_name = N'rental_score_data' |
| 115 | + , @params = N'@py_model varbinary(max)' |
| 116 | + , @py_model = @py_model |
| 117 | + with result sets (("RentalCount_Predicted" float, "RentalCount" float, "Month" float,"Day" float,"WeekDay" float,"Snow" float,"Holiday" float, "Year" float)); |
| 118 | + |
| 119 | +END; |
| 120 | +GO |
| 121 | + |
| 122 | + |
| 123 | +---------------- STEP 5 - Create DB table to store predictions ----------------------- |
| 124 | +DROP TABLE IF EXISTS [dbo].[py_rental_predictions]; |
| 125 | +GO |
| 126 | +--Create a table to store the predictions in |
| 127 | +CREATE TABLE [dbo].[py_rental_predictions]( |
| 128 | + [RentalCount_Predicted] [int] NULL, |
| 129 | + [RentalCount_Actual] [int] NULL, |
| 130 | + [Month] [int] NULL, |
| 131 | + [Day] [int] NULL, |
| 132 | + [WeekDay] [int] NULL, |
| 133 | + [Snow] [int] NULL, |
| 134 | + [Holiday] [int] NULL, |
| 135 | + [Year] [int] NULL |
| 136 | +) ON [PRIMARY] |
| 137 | +GO |
| 138 | + |
| 139 | + |
| 140 | +---------------- STEP 6 - Save the predictions in a DB table ----------------------- |
| 141 | +TRUNCATE TABLE py_rental_predictions; |
| 142 | +--Insert the results of the predictions for test set into a table |
| 143 | +INSERT INTO py_rental_predictions |
| 144 | +EXEC py_predict_rentalcount 'linear_model'; |
| 145 | + |
| 146 | +-- Select contents of the table |
| 147 | +SELECT * FROM py_rental_predictions; |
| 148 | + |
0 commit comments