|
| 1 | +# Demostration: Creating a Machine Learning Model |
| 2 | + |
| 3 | +Costa Rica |
| 4 | + |
| 5 | +[](https://github.com/) |
| 6 | +[brown9804](https://github.com/brown9804) |
| 7 | + |
| 8 | +Last updated: 2025-04-29 |
| 9 | + |
| 10 | +------------------------------------------ |
| 11 | + |
| 12 | + |
| 13 | +<details> |
| 14 | +<summary><b>List of References </b> (Click to expand)</summary> |
| 15 | + |
| 16 | +</details> |
| 17 | + |
| 18 | +<details> |
| 19 | +<summary><b>Table of Content </b> (Click to expand)</summary> |
| 20 | + |
| 21 | +</details> |
| 22 | + |
| 23 | +## Step 1: Set Up Your Azure ML Workspace |
| 24 | + |
| 25 | +> You can use the azure portal approach: |
| 26 | +
|
| 27 | +- Go to the [Azure Portal](https://portal.azure.com/) |
| 28 | +- Create a **Machine Learning workspace**: |
| 29 | + - Resource group |
| 30 | + - Workspace name |
| 31 | + - Region |
| 32 | +- Once created, launch **Azure Machine Learning Studio**. |
| 33 | + |
| 34 | +https://github.com/user-attachments/assets/c199156f-96cf-4ed0-a8b5-c88db3e7a552 |
| 35 | + |
| 36 | +> Or using terraform configurations for setting up an Azure Machine Learning workspace along with compute clusters and supportive resources to form the core of an ML platform, click here to see [Demonstration: Deploying Azure Resources for an ML Platform](./infrastructure/azMachineLearning/README.md) |
| 37 | +
|
| 38 | +## Step 2: Create a Compute Instance |
| 39 | + |
| 40 | +1. **Go to [Azure Machine Learning Studio](https://ml.azure.com/)** and select your workspace. |
| 41 | +2. **Select `Compute` from the left menu** Choose the **`Compute instances`** tab. |
| 42 | +3. **Click `New`** |
| 43 | + - Enter a name for your compute instance. |
| 44 | + - Choose a virtual machine size (e.g., `Standard_DS3_v2`). |
| 45 | + - Optionally, enable SSH access or assign a user. |
| 46 | +4. **Click `Create`**: Azure will provision the compute instance, which may take a few minutes. |
| 47 | + |
| 48 | + https://github.com/user-attachments/assets/bd5f3ce6-7082-4741-8827-8b344cd249a4 |
| 49 | + |
| 50 | +## Step 3: Prepare Your Data |
| 51 | + |
| 52 | +- Upload your dataset to **Azure ML datastore** or connect to exrnal sources (e.g., Azure Blob Storage, SQL, etc.). |
| 53 | +- Use **Data > Datasets** to register and version your dataset. |
| 54 | + |
| 55 | +> For example: Upload the CSV to Azure ML |
| 56 | +
|
| 57 | +1. Under to **Data > + Create > From local files**. |
| 58 | +2. Choose: |
| 59 | + - **Name**: `employee_data` |
| 60 | + - **Type**: Tabular |
| 61 | + - **Browse** and upload the `sample_data.csv` file. |
| 62 | +3. Click **Next**, then **Review + Create**. |
| 63 | + |
| 64 | +> Register the Dataset: |
| 65 | +
|
| 66 | +1. After upload, Azure will preview the data. |
| 67 | +2. Confirm the schema (column names and types). |
| 68 | +3. Click **Create** to register the dataset. |
| 69 | + |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +### **4. Create a New Notebook or Script** |
| 74 | +- Use the compute instance to open a **Jupyter notebook** or create a Python script. |
| 75 | +- Import necessary libraries: |
| 76 | + ```python |
| 77 | + import pandas as pd |
| 78 | + from sklearn.model_selection import train_test_split |
| 79 | + from sklearn.ensemble import RandomForestClassifier |
| 80 | + from sklearn.metrics import accuracy_score |
| 81 | + ``` |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +### **5. Load and Explore the Data** |
| 86 | +- Load the dataset and perform basic EDA (exploratory data analysis): |
| 87 | + ```python |
| 88 | + data = pd.read_csv('your_dataset.csv') |
| 89 | + print(data.head()) |
| 90 | + ``` |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +### **6. Train Your Model** |
| 95 | +- Split the data and train a model: |
| 96 | + ```python |
| 97 | + X = data.drop('target', axis=1) |
| 98 | + y = data['target'] |
| 99 | + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) |
| 100 | + |
| 101 | + model = RandomForestClassifier() |
| 102 | + model.fit(X_train, y_train) |
| 103 | + ``` |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +### **7. Evaluate the Model** |
| 108 | +- Check performance: |
| 109 | + ```python |
| 110 | + predictions = model.predict(X_test) |
| 111 | + print("Accuracy:", accuracy_score(y_test, predictions)) |
| 112 | + ``` |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +### **8. Register the Model** |
| 117 | +- Save and register the model in Azure ML: |
| 118 | + ```python |
| 119 | + import joblib |
| 120 | + joblib.dump(model, 'model.pkl') |
| 121 | + |
| 122 | + from azureml.core import Workspace, Model |
| 123 | + ws = Workspace.from_config() |
| 124 | + Model.register(workspace=ws, model_path="model.pkl", model_name="my_model") |
| 125 | + ``` |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +### **9. Deploy the Model** |
| 130 | +- Create an **inference configuration** and deploy to a web service: |
| 131 | + ```python |
| 132 | + from azureml.core.environment import Environment |
| 133 | + from azureml.core.model import InferenceConfig |
| 134 | + from azureml.core.webservice import AciWebservice |
| 135 | + |
| 136 | + env = Environment.from_conda_specification(name="myenv", file_path="env.yml") |
| 137 | + inference_config = InferenceConfig(entry_script="score.py", environment=env) |
| 138 | + |
| 139 | + deployment_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1) |
| 140 | + service = Model.deploy(workspace=ws, |
| 141 | + name="my-service", |
| 142 | + models=[model], |
| 143 | + inference_config=inference_config, |
| 144 | + deployment_config=deployment_config) |
| 145 | + service.wait_for_deployment(show_output=True) |
| 146 | + ``` |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +### **10. Test the Endpoint** |
| 151 | +- Once deployed, you can send HTTP requests to the endpoint to get predictions. |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | +<div align="center"> |
| 156 | + <h3 style="color: #4CAF50;">Total Visitors</h3> |
| 157 | + <img src="https://profile-counter.glitch.me/brown9804/count.svg" alt="Visitor Count" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/> |
| 158 | +</div> |
0 commit comments