Remove extra spacing

This commit is contained in:
Lorin Thwaits
2023-02-20 19:33:29 +00:00
parent 434b798c98
commit b062f2c734
1461 changed files with 13333 additions and 76271 deletions
@@ -27,7 +27,7 @@ This sample consist of a binary classifier that predict whether a particular bi
- **Applies to:** SQL Server 2017 CTP2.0 or higher
- **Key features:** SQL Server Machine Learning Services
- **Key features:** SQL Server Machine Learning Services
- **Workload:** SQL Server Machine Learning Services
- **Programming Language:** Python, TSQL
- **Author:** Yassine Khelifi
@@ -36,8 +36,8 @@ This sample consist of a binary classifier that predict whether a particular bi
## Before you begin
To run this sample, you need the following prerequisites:
1. [Download this DB backup file](https://sq14samples.blob.core.windows.net/data/velibDB.bak) and restore it using Setup.sql.
To run this sample, you need the following prerequisites:
1. [Download this DB backup file](https://sq14samples.blob.core.windows.net/data/velibDB.bak) and restore it using Setup.sql.
**Software prerequisites:**
@@ -47,7 +47,7 @@ To run this sample, you need the following prerequisites:
3. [Python Tools for Visual Studio](https://www.visualstudio.com/vs/python/) or another Python IDE
## Run this sample
1. From SQL Server Management Studio, or SQL Server Data Tools, connect to your SQL Server 2017 database and execute setup.sql to restore the sample DB you have downloaded
1. From SQL Server Management Studio, or SQL Server Data Tools, connect to your SQL Server 2017 database and execute setup.sql to restore the sample DB you have downloaded
2. From Python Tools for Visual Studio, open the python tools command under tools menu, add the Machine Learning Services Python environment to the corresponding paths https://docs.microsoft.com/en-us/visualstudio/python/python-environments
@@ -13,24 +13,24 @@ class DataSource():
Args:
connectionstring: connection string to the SQL server.
"""
self.__connectionstring = connectionstring
def loaddata(self):
dataSource = RxSqlServerData(sqlQuery = "select * from dbo.trainingdata", verbose=True, reportProgress =True,
connectionString = self.__connectionstring)
self.__computeContext = RxInSqlServer(connectionString = self.__connectionstring, autoCleanup = True)
self.__computeContext = RxInSqlServer(connectionString = self.__connectionstring, autoCleanup = True)
data = rx_import_datasource(dataSource)
return data
def getcomputecontext(self):
if self.__computeContext is None:
raise RuntimeError("Data must be loaded before requesting computecontext!")
@@ -1,7 +1,7 @@
'''
Pipeline implementation
Pipeline implementation
'''
@@ -28,26 +28,26 @@ class OutliersHandler(BaseEstimator, TransformerMixin):
return self
def transform(self, df):
df.availablebikes = np.where(df.availablebikes > df.bikestands, df.bikestands, df.availablebikes)
return df
class LabelDefiner(BaseEstimator, TransformerMixin):
"""
Defines target variable
Defines target variable
Binary label 0 empty station, 1 otherwise
"""
def __init__(self, availability_threshold = 1):
self.threshold = availability_threshold
def fit(self, x, y = None):
return self
def transform(self, df):
df['label'] = np.where(df.availablebikes < self.threshold, 0, 1)
return df
@@ -73,7 +73,7 @@ class DateTimeFeaturesExtractor(BaseEstimator, TransformerMixin):
class TSFeaturesExtractor(BaseEstimator, TransformerMixin):
"""Extract time series related features"""
def __init__(self, max_lags = 4):
self.__max_lags = max_lags
@@ -82,18 +82,18 @@ class TSFeaturesExtractor(BaseEstimator, TransformerMixin):
def transform(self, df):
df.sort_values(['lastupdate','stationid'], ascending = [True, True])
for i in range(self.__max_lags):
df['lag' + str(i)] = df.groupby(['stationid'])['availablebikes'].shift(i + 1)
df['1st_derivative'] = df.groupby('stationid')['lag0'].transform(lambda x: np.gradient(x))
df['2nd_derivative'] = df.groupby('stationid')['1st_derivative'].transform(lambda x: np.gradient(x))
df['fft_max_coeff'] = df.groupby(['stationid', 'month', 'day', 'hour'])['lag0'].transform(lambda x: np.amax(np.abs(np.fft.rfft(x))))
df['fft_energy'] = df.groupby(['stationid', 'month', 'day', 'hour'])['lag0'].transform(lambda x: np.sum((np.abs(np.fft.rfft(x))) ** 2))
return df
@@ -102,7 +102,7 @@ class TSFeaturesExtractor(BaseEstimator, TransformerMixin):
class StatisticalFeaturesExtractor(BaseEstimator, TransformerMixin):
"""Extract statistical related features"""
def __init__(self, max_lags = 4):
self.__max_lags = max_lags
@@ -110,7 +110,7 @@ class StatisticalFeaturesExtractor(BaseEstimator, TransformerMixin):
return self
def transform(self, df):
df['var'] = df.groupby(['stationid', 'month', 'day', 'hour'])['lag0'].transform('var')
df['cumrelfreq'] = df.groupby(['stationid', 'month', 'day', 'hour'])['lag0'].cumsum() / self.__max_lags
df['mad'] = df.groupby(['stationid', 'month', 'day', 'hour'])['lag0'].transform('mad')
@@ -125,15 +125,15 @@ class StatisticalFeaturesExtractor(BaseEstimator, TransformerMixin):
class FeaturesExcluder(BaseEstimator, TransformerMixin):
"""features to exclude"""
def __init__(self, features = ['availablebikes', 'bikestands','lastupdate', 'zipcode','month', 'day']):
self.__exclusionlist = features
def fit(self, X, y = None):
return self
def transform(self, df):
df.drop(self.__exclusionlist, axis = 1, inplace = True)
return df
@@ -141,9 +141,9 @@ class FeaturesExcluder(BaseEstimator, TransformerMixin):
class FeaturesScaler(BaseEstimator, TransformerMixin):
"""Z-score scaler """
def fit(self, X, y = None):
return self
@@ -156,25 +156,25 @@ class FeaturesScaler(BaseEstimator, TransformerMixin):
X = StandardScaler().fit_transform(df.drop(excluded_cols, axis=1, inplace = False))
X = np.concatenate((df.loc[:, excluded_cols].as_matrix(), X), axis = 1)
df_out = pd.DataFrame(X, columns = cols)
return df_out
class RxClassifier(BaseEstimator, ClassifierMixin):
class RxClassifier(BaseEstimator, ClassifierMixin):
""" Revoscalerpy logisitic regression binary classifier wrapped in sklearn estimator """
def __init__(self, computecontext):
self.__computecontext = computecontext
def fit(self, X, y = None):
"""Fit model to training data
"""Fit model to training data
Args:
@@ -182,9 +182,9 @@ class RxClassifier(BaseEstimator, ClassifierMixin):
y (None): Not used the target variable is passed in X.
return: coefficients (pandas DataFrame)
"""
formula = "label ~ F(stationid) + F(hour) + F(minute) + isweekend + lag0 + \
lag1 + lag2 + lag3 + 1st_derivative + 2nd_derivative\
+ fft_max_coeff + fft_energy + var + cumrelfreq + mad + idxmax + idxmin"
@@ -193,12 +193,12 @@ class RxClassifier(BaseEstimator, ClassifierMixin):
self.__clf = rx_logit_ex(formula, data = X, compute_context = self.__computecontext, report_progress = 3, verbose = 1)
end = time.time()
print("Training time duration: %.2f seconds" % (end - start))
print("Training time duration: %.2f seconds" % (end - start))
return self.__clf.coefficients
def predict(self, X):
"""
"""
Perform classification on X
Args:
@@ -208,8 +208,8 @@ class RxClassifier(BaseEstimator, ClassifierMixin):
"""
if self.__clf is None:
raise RuntimeError("Data must be fitted before calling predict!")
predict = rx_predict_ex(self.__clf, data = X, compute_context = self.__computecontext)
predict = rx_predict_ex(self.__clf, data = X, compute_context = self.__computecontext)
predictions = np.where(predict._results['label_Pred'] == 1, 1, 0)
return predictions
@@ -1,4 +1,4 @@
import sys
import sys
import numpy as np
from sklearn.pipeline import Pipeline
from datasource import DataSource
@@ -9,13 +9,13 @@ from sklearn.metrics import classification_report
def run():
# modify connection string to point to MLS/SQL Server instance where you restored the database
# modify connection string to point to MLS/SQL Server instance where you restored the database
connectionstring = 'Driver=SQL Server;Server=MLMACHINE\\SQLSERVER17;Database=velibdb;Trusted_Connection=True;'
ds = DataSource(connectionstring)
df = ds.loaddata()
pipeline = Pipeline(steps= [('outliers', OutliersHandler()),
('label',LabelDefiner()),
@@ -29,31 +29,31 @@ def run():
# Execute Pipeline
df = pipeline.fit_transform(df)
# split dataset
test_size = 24 * 4 # one day test set of each station
train = df.groupby('stationid').head(df.shape[0] - test_size)
test = df.groupby('stationid').tail(test_size)
# fit classifier
clf = RxClassifier(computecontext = ds.getcomputecontext())
clf = RxClassifier(computecontext = ds.getcomputecontext())
coeffs = clf.fit(train)
#print coefficients and exclude stationid Factor
#print coefficients and exclude stationid Factor
print(coeffs.tail(14))
# run prediction on hold out set and evaluate
# run prediction on hold out set and evaluate
y_pred = clf.predict(test.drop(['label'], axis=1, inplace = False))
y_truth = test['label'].as_matrix()
print(classification_report(y_truth, y_pred))
if __name__ == "__main__":
if __name__ == "__main__":
run()
@@ -61,7 +61,7 @@ def perform_clustering():
data_source = revoscale.RxSqlServerData(sql_query=input_query, column_info=column_info,
connection_string=conn_str)
# import data source and convert to pandas dataframe.
customer_data = pd.DataFrame(revoscalepy.rx_import(data_source))
print("Data frame:", customer_data.head(n=20))
@@ -15,7 +15,7 @@ SELECT
CAST( (ROUND(COALESCE(returns_count / NULLIF(1.0*orders_count, 0), 0), 7) ) AS FLOAT) AS orderRatio,
CAST( (ROUND(COALESCE(returns_items / NULLIF(1.0*orders_items, 0), 0), 7) ) AS FLOAT) AS itemsRatio,
CAST( (ROUND(COALESCE(returns_money / NULLIF(1.0*orders_money, 0), 0), 7) ) AS FLOAT) AS monetaryRatio,
CAST( (COALESCE(returns_count, 0)) AS FLOAT) AS frequency
CAST( (COALESCE(returns_count, 0)) AS FLOAT) AS frequency
FROM
(
SELECT
@@ -41,7 +41,7 @@ FROM
SUM( sr_return_amt ) AS returns_money
FROM store_returns
GROUP BY sr_customer_sk
) returned ON ss_customer_sk=sr_customer_sk
) returned ON ss_customer_sk=sr_customer_sk
'
EXEC sp_execute_external_script
@@ -90,11 +90,11 @@ EXEC [dbo].[py_generate_customer_return_clusters];
-- Select contents of the table
SELECT * FROM py_customer_clusters;
--Get email addresses of customers in cluster 0
--Get email addresses of customers in cluster 0
SELECT customer.[c_email_address], customer.c_customer_sk
FROM dbo.customer
JOIN
[dbo].[py_customer_clusters] as c
ON c.Customer = customer.c_customer_sk
WHERE c.cluster = 0;
@@ -15,7 +15,7 @@ This sample shows how to create a predictive model in Python and operationalize
## About this sample
Predictive modeling is a powerful way to add intelligence to your application. It enables applications to predict outcomes against new data.
The act of incorporating predictive analytics into your applications involves two major phases:
The act of incorporating predictive analytics into your applications involves two major phases:
model training and model operationalization.
In this sample, you will learn how to create a predictive model in python and operationalize it with SQL Server vNext.
@@ -23,18 +23,18 @@ In this sample, you will learn how to create a predictive model in python and op
<!-- Delete the ones that don't apply -->
- **Applies to:** SQL Server 2017 CTP2.0 or higher
- **Key features:** SQL Server Machine Learning Services
- **Key features:** SQL Server Machine Learning Services
- **Workload:** SQL Server Machine Learning Services
- **Programming Language:** T-SQL, Python
- **Authors:** Nellie Gustafsson
- **Update history:** Getting started tutorial for SQL Server ML Services - Python
- **Update history:** Getting started tutorial for SQL Server ML Services - Python
<a name=before-you-begin></a>
## Before you begin
To run this sample, you need the following prerequisites: </br>
[Download this DB backup file](https://sqlchoice.blob.core.windows.net/sqlchoice/TutorialDB.bak) and restore it using Setup.sql.
[Download this DB backup file](https://sqlchoice.blob.core.windows.net/sqlchoice/TutorialDB.bak) and restore it using Setup.sql.
**Software prerequisites:**
@@ -51,14 +51,14 @@ Necessary tables </br>
Creates stored procedure to train a model </br>
Creates a stored procedure to predict using that model </br>
Saves the predicted results to a DB table </br>
3. You can also try the Python script on its own, connecting to SQL Server and getting data using RevoScalePy Rx functions. Just remember to point the Python environment to the corresponding path "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES" if you run in-db Python Server, or
3. You can also try the Python script on its own, connecting to SQL Server and getting data using RevoScalePy Rx functions. Just remember to point the Python environment to the corresponding path "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES" if you run in-db Python Server, or
"C:\Program Files\Microsoft SQL Server\140\PYTHON_SERVER" if you have the standalone Machine Learning Server installed.
<a name=sample-details></a>
## Sample details
This sample shows how to create a predictive model with Python and generate predictions using the model and deploy that in SQL Server with SQL Server Machine Learning Services.
This sample shows how to create a predictive model with Python and generate predictions using the model and deploy that in SQL Server with SQL Server Machine Learning Services.
### rental_prediction.py
The Python script that generates a predictive model and uses it to predict rental counts
@@ -1,8 +1,8 @@
/*
To install the pretrained model in SQL Server, open an elevated CMD promtp:
1. Navigate to the SQL Server installation path:
1. Navigate to the SQL Server installation path:
C:\<SQL SERVER Installation path>\Microsoft SQL Server\140\Setup Bootstrap\SQL2017\x64
2. Run the following command:
2. Run the following command:
RSetup.exe /install /component MLM /<version>/language 1033 /destdir <SQL_DB_instance_folder>\PYTHON_SERVICES\Lib\site-packages\microsoftml\mxLibs
Example:
RSetup.exe /install /component MLM /version 9.2.0.24 /language 1033 /destdir "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\Lib\site-packages\microsoftml\mxLibs"
@@ -16,16 +16,16 @@ GO
--******************************************************************************************************************
-- STEP 1 Stored procedure that uses a pretrained model to determine sentiment of a text, such as a product review
--******************************************************************************************************************
CREATE OR ALTER PROCEDURE [dbo].[get_sentiment]
CREATE OR ALTER PROCEDURE [dbo].[get_sentiment]
(@text NVARCHAR(MAX))
AS
BEGIN
DECLARE @script nvarchar(max);
--Check that text is not empty
IF NULLIF(@text, '') is null
IF NULLIF(@text, '') is null
BEGIN
THROW 50001, 'Please specify a text value to be analyzed.', 1;
THROW 50001, 'Please specify a text value to be analyzed.', 1;
RETURN
END
@@ -55,7 +55,7 @@ sentiment_scores["Sentiment"] = sentiment_scores.scores.apply(lambda score: "Pos
WITH RESULT SETS (("Text" NVARCHAR(MAX),"Score" FLOAT, "Sentiment" NVARCHAR(30)));
END
GO
--******************************************************************************************************************
@@ -1,8 +1,8 @@
/*
To install the pretrained model in SQL Server, open an elevated CMD promtp:
1. Navigate to the SQL Server installation path:
1. Navigate to the SQL Server installation path:
C:\<SQL SERVER Installation path>\Microsoft SQL Server\140\Setup Bootstrap\SQL2017\x64
2. Run the following command:
2. Run the following command:
RSetup.exe /install /component MLM /<version>/language 1033 /destdir <SQL_DB_instance_folder>\PYTHON_SERVICES\Lib\site-packages\microsoftml\mxLibs
Example:
RSetup.exe /install /component MLM /version 9.2.0.24 /language 1033 /destdir "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\Lib\site-packages\microsoftml\mxLibs"
@@ -42,7 +42,7 @@ sentiment_scores["Sentiment"] = sentiment_scores.scores.apply(lambda score: "Pos
WITH RESULT SETS (("Review" NVARCHAR(MAX),"Score" FLOAT, "Sentiment" NVARCHAR(30)));
END
GO
--******************************************************************************************************************
@@ -13,7 +13,7 @@ CREATE TABLE [dbo].[models](
[model] [varbinary](max) NOT NULL,
[create_time] [datetime2](7) NULL DEFAULT (sysdatetime()),
[created_by] [nvarchar](500) NULL DEFAULT (suser_sname()),
PRIMARY KEY CLUSTERED
PRIMARY KEY CLUSTERED
(
[language],
[model_name]
@@ -31,11 +31,11 @@ CREATE OR ALTER VIEW product_reviews_training_data
AS
SELECT TOP(CAST( ( SELECT COUNT(*) FROM product_reviews)*.9 AS INT))
CAST(pr_review_content AS NVARCHAR(4000)) AS pr_review_content,
CASE
WHEN pr_review_rating <3 THEN 1
WHEN pr_review_rating =3 THEN 2
ELSE 3
END AS tag
CASE
WHEN pr_review_rating <3 THEN 1
WHEN pr_review_rating =3 THEN 2
ELSE 3
END AS tag
FROM product_reviews;
GO
@@ -43,11 +43,11 @@ CREATE OR ALTER VIEW product_reviews_test_data
AS
SELECT TOP(CAST( ( SELECT COUNT(*) FROM product_reviews)*.1 AS INT))
CAST(pr_review_content AS NVARCHAR(4000)) AS pr_review_content,
CASE
WHEN pr_review_rating <3 THEN 1
WHEN pr_review_rating =3 THEN 2
ELSE 3
END AS tag
CASE
WHEN pr_review_rating <3 THEN 1
WHEN pr_review_rating =3 THEN 2
ELSE 3
END AS tag
FROM product_reviews;
GO
@@ -75,7 +75,7 @@ import pickle
## Defining the tag column as a categorical type
training_data["tag"] = training_data["tag"].astype("category")
## Create a machine learning model for multiclass text classification.
## Create a machine learning model for multiclass text classification.
## We are using a text featurizer function to split the text in features of 2-word chunks
model = rx_logistic_regression(formula = "tag ~ features", data = training_data, method = "multiClass", ml_transforms=[
featurize_text(language="English",
@@ -91,10 +91,10 @@ modelbin = pickle.dumps(model)
, @script = @train_script
, @input_data_1 = N'SELECT * FROM product_reviews_training_data'
, @input_data_1_name = N'training_data'
, @params = N'@modelbin varbinary(max) OUTPUT'
, @params = N'@modelbin varbinary(max) OUTPUT'
, @modelbin = @model OUTPUT;
--Save model to DB Table
--Save model to DB Table
DELETE FROM dbo.models WHERE model_name = 'rx_logistic_regression' and language = 'Python';
INSERT INTO dbo.models (language, model_name, model) VALUES('Python', 'rx_logistic_regression', @model);
END;
@@ -127,7 +127,7 @@ BEGIN
--The Python script we want to execute
SET @prediction_script = N'
from microsoftml import rx_predict
from revoscalepy import rx_data_step
from revoscalepy import rx_data_step
import pickle
## The input data from the query in @input_data_1 is populated in test_data
@@ -136,7 +136,7 @@ import pickle
## Unserialize the model
model = pickle.loads(model_bin)
## Use the rx_logistic_regression model
## Use the rx_logistic_regression model
predictions = rx_predict(model = model, data = test_data, extra_vars_to_write = ["tag", "pr_review_content"], overwrite = True)
## Converting to output data set
@@ -159,7 +159,7 @@ GO
--***************************************************************************************************
-- STEP 6 Execute the multi class prediction using the model we trained earlier
--***************************************************************************************************
EXECUTE [dbo].[predict_review_sentiment]
EXECUTE [dbo].[predict_review_sentiment]
GO
@@ -7,7 +7,7 @@ as
begin
declare @rx_model varbinary(max) = (select model from iris_models where model_name = @model);
-- Predict based on the specified model:
exec sp_execute_external_script
exec sp_execute_external_script
@language = N'R'
, @script = N'
# Unserialize model from SQL Server
@@ -18,7 +18,7 @@
<DataSet Name="Feature_Settings">
<Query>
<DataSourceName>SqlConnection</DataSourceName>
<CommandText>/*
<CommandText>/*
Retrieve the Machine Learning Services installation setting &amp; configuration options:
Implied Authentication Configuration is checked by verifying if login exists for SQLRUserGroup
*/
@@ -21,11 +21,11 @@
<CommandText>/*
Get list of extended events from SQLSatellite package.
*/
select o.name as event_name, o.description
from sys.dm_xe_objects o
join sys.dm_xe_packages p
on o.package_guid = p.guid
where o.object_type = 'event'
select o.name as event_name, o.description
from sys.dm_xe_objects o
join sys.dm_xe_packages p
on o.package_guid = p.guid
where o.object_type = 'event'
and p.name = 'SQLSatellite';</CommandText>
<rd:UseGenericDesigner>true</rd:UseGenericDesigner>
</Query>
@@ -58,23 +58,23 @@ Custom reports for Machine Learning Services allow you to perform the following
### R Services - Configuration.rdl
This report can be used to view the installation settings of Machine Learning Services and properties of the R or Python runtime. You can also use this report to configure Machine Learning Services after installation.
This report can be used to view the installation settings of Machine Learning Services and properties of the R or Python runtime. You can also use this report to configure Machine Learning Services after installation.
### R Services - Packages.rdl
This report lists the R or Python packages installed on the SQL Server instance and properties like version, name.
This report lists the R or Python packages installed on the SQL Server instance and properties like version, name.
### R Services - Resource Usage.rdl
This report can be used to view the CPU, Memory, IO consumption of SQL Server & external scripts execution. You can also view the memory setting of external resource pools.
This report can be used to view the CPU, Memory, IO consumption of SQL Server & external scripts execution. You can also view the memory setting of external resource pools.
### R Services - Extended Events.rdl
This report can be used to view the extended events that are available to get more insights into external scripts execution.
This report can be used to view the extended events that are available to get more insights into external scripts execution.
### R Services - Execution Statistics.rdl
This report can be used to view the execution statistics of Machine Learning services. For example, you can get the total number of external scripts executions, number of parallel executions and frequently used RevoScaleR functions.
This report can be used to view the execution statistics of Machine Learning services. For example, you can get the total number of external scripts executions, number of parallel executions and frequently used RevoScaleR functions.
<a name=related-links></a>