<jupyter_start><jupyter_text>Train and register model  Connect to workspace<jupyter_code># Connect to workspace
from azureml.core import Workspace

ws = Workspace.from_config()
print(ws.name, "Workspace loaded")<jupyter_output>zpdsnfws02 Workspace loaded<jupyter_text>Upload data to datastores<jupyter_code># Get default datastores
default_ds = ws.get_default_datastore()

# Show all datastores
for ds_name in ws.datastores:
    print(ds_name, "- Default =", ds_name == default_ds.name)
# Upload data to a datastore
default_ds.upload_files(files = ["./datasets/Marketing_Request_20210312.csv"], \
                        target_path = "marketing-request-20210312-data/", \
                        overwrite = True, show_progress = True)<jupyter_output>Uploading an estimated of 1 files
Uploading ./datasets/Marketing_Request_20210312.csv
Uploaded ./datasets/Marketing_Request_20210312.csv, 1 files out of an estimated total of 1
Uploaded 1 files<jupyter_text>Train model<jupyter_code># Train model from datastore
data_ref = default_ds.path("marketing-request-20210312-data").as_download()
print(data_ref)
# Create folder for experiment files
import os

experiment_folder = "marketing_request_20210312_from_datastore"
os.makedirs(experiment_folder, exist_ok = True)
print(experiment_folder, "folder created")<jupyter_output>marketing_request_20210312_from_datastore folder created<jupyter_text>Write training script<jupyter_code>%%writefile $experiment_folder/marketing_request_20210312_training.py
# Import libraries
import os 
import argparse
from azureml.core import Run
import pandas as pd
import numpy as np
import joblib
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# Parameterised training script
# Get parameters
parser = argparse.ArgumentParser()
parser.add_argument("--data-folder", type = str, dest = "data_folder", help = "data folder reference")
args = parser.parse_args()

# Get experiment run context
run = Run.get_context()

# Load data from data reference
data_folder = args.data_folder
print("Loading data from", data_folder)
# Load all files and concatenate into a single dataframe
all_files = os.listdir(data_folder)
marketing_request = pd.concat(pd.read_csv(os.path.join(data_folder, csv_file)) for csv_file in all_files)

# Get features and labels
X, y = marketing_request[["ACCESS_COUNT", "VOLUME"]].values, marketing_request["USERS"].values

# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30, random_state = 123)

# Train linear regression model
print("Training a linear regression model with n_jobs None")
run.log("data_folder", data_folder)
model = LinearRegression().fit(X_train, y_train)

# Calculate accuracy
y_hat = model.predict(X_test)
acc = np.average(y_hat == y_test)
print("Accuracy:", acc)
run.log("Accuracy", np.float(acc))

# Calculate mean squared error
y_scores = model.predict(X_test)
mse = mean_squared_error(y_test, y_scores)
run.log("MSE", np.float(mse))

# Store outputs
os.makedirs("outputs", exist_ok = True)
joblib.dump(value = model, filename = "outputs/marketing_request_20210312_model.pkl")

run.complete()<jupyter_output>Overwriting marketing_request_20210312_from_datastore/marketing_request_20210312_training.py<jupyter_text>Run training script<jupyter_code># Create estimator to run script
from azureml.train.sklearn import SKLearn
from azureml.core import Experiment
from azureml.widgets import RunDetails
import pandas as pd

data_ref = default_ds.path("marketing-request-20210312-data").as_download()

# Set script parameters
script_params = {
    "--data-folder": data_ref
}

# Create estimator
estimator = SKLearn(source_directory = experiment_folder,
                   entry_script = "marketing_request_20210312_training.py",
                   script_params = script_params,
                   compute_target = "local",
                    conda_packages = ["pandas", "pyspark"]
                   )

# Create experiment
experiment_name = "marketing_request_20210312_training"
experiment = Experiment(workspace = ws, name = experiment_name)

# Run experiment
run = experiment.submit(config = estimator)

# Show run details
RunDetails(run).show()
run.wait_for_completion()<jupyter_output>WARNING:azureml.train.sklearn:'SKLearn' estimator is deprecated. Please use 'ScriptRunConfig' from 'azureml.core.script_run_config' with your own defined environment or the AzureML-Tutorial curated environment.
WARNING:azureml.core.environment:'enabled' is deprecated. Please use the azureml.core.runconfig.DockerConfiguration object with the 'use_docker' param instead.
WARNING:root:If 'script' has been provided here and a script file name has been specified in 'run_config', 'script' provided in ScriptRunConfig initialization will take precedence.
WARNING:root:If 'arguments' has been provided here and arguments have been specified in 'run_config', 'arguments' provided in ScriptRunConfig initialization will take precedence.<jupyter_text>Register model<jupyter_code>model = run.register_model(model_path = "outputs/marketing_request_20210312_model.pkl", \
                   model_name = "marketing_request_20210312_model",\
                  tags = {"Training context": "Inline Training"},\
                  properties = {"MSE": run.get_metrics()["MSE"], "Accuracy": run.get_metrics()["Accuracy"]})
print("Model trained and registered.")
print(model.name, model.id, model.version, sep='\t')
print(model.get_model_path("marketing_request_20210312_model"))
model.id<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0928037050231564, 'fraction_numerical': 0.026362664766654793, 'mean_word_length': 5.598119858989424, 'pattern_counts': {'":': 4, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '366532', 'license': 'MIT', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1700, 'n_words': 427, 'path': 'azureml_modelling.ipynb', 'repo_name': 'AkbarAzad/azureml-sdk-python', 'size': '5614'}