|
| 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 | +<details> |
| 13 | +<summary><b>List of References </b> (Click to expand)</summary> |
| 14 | + |
| 15 | +</details> |
| 16 | + |
| 17 | +<details> |
| 18 | +<summary><b>Table of Content </b> (Click to expand)</summary> |
| 19 | + |
| 20 | +</details> |
| 21 | + |
| 22 | +> Azure ML is a cloud-based platform that provides tools for building, training, and deploying ML models at scale. |
| 23 | +
|
| 24 | +## Step 1: Set Up Your Azure ML Workspace |
| 25 | + |
| 26 | +> You can use the azure portal approach: |
| 27 | +
|
| 28 | +- Go to the [Azure Portal](https://portal.azure.com/) |
| 29 | +- Create a **Machine Learning workspace**: |
| 30 | + - Resource group |
| 31 | + - Workspace name |
| 32 | + - Region |
| 33 | +- Once created, launch **Azure Machine Learning Studio**. |
| 34 | + |
| 35 | +https://github.com/user-attachments/assets/c199156f-96cf-4ed0-a8b5-c88db3e7a552 |
| 36 | + |
| 37 | +> 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) |
| 38 | +
|
| 39 | +### **2. Create a Compute Instance** |
| 40 | +- In Azure ML Studio, go to **Compute > Compute Instances**. |
| 41 | +- Create a new instance (choose CPU or GPU depending on your needs). |
| 42 | +- This will be your development environment (like a cloud-based Jupyter notebook). |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +### **3. Prepare Your Data** |
| 47 | +- Upload your dataset to **Azure ML datastore** or connect to external sources (e.g., Azure Blob Storage, SQL, etc.). |
| 48 | +- Use **Data > Datasets** to register and version your dataset. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +### **4. Create a New Notebook or Script** |
| 53 | +- Use the compute instance to open a **Jupyter notebook** or create a Python script. |
| 54 | +- Import necessary libraries: |
| 55 | + ```python |
| 56 | + import pandas as pd |
| 57 | + from sklearn.model_selection import train_test_split |
| 58 | + from sklearn.ensemble import RandomForestClassifier |
| 59 | + from sklearn.metrics import accuracy_score |
| 60 | + ``` |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +### **5. Load and Explore the Data** |
| 65 | +- Load the dataset and perform basic EDA (exploratory data analysis): |
| 66 | + ```python |
| 67 | + data = pd.read_csv('your_dataset.csv') |
| 68 | + print(data.head()) |
| 69 | + ``` |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +### **6. Train Your Model** |
| 74 | +- Split the data and train a model: |
| 75 | + ```python |
| 76 | + X = data.drop('target', axis=1) |
| 77 | + y = data['target'] |
| 78 | + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) |
| 79 | + |
| 80 | + model = RandomForestClassifier() |
| 81 | + model.fit(X_train, y_train) |
| 82 | + ``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +### **7. Evaluate the Model** |
| 87 | +- Check performance: |
| 88 | + ```python |
| 89 | + predictions = model.predict(X_test) |
| 90 | + print("Accuracy:", accuracy_score(y_test, predictions)) |
| 91 | + ``` |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +### **8. Register the Model** |
| 96 | +- Save and register the model in Azure ML: |
| 97 | + ```python |
| 98 | + import joblib |
| 99 | + joblib.dump(model, 'model.pkl') |
| 100 | + |
| 101 | + from azureml.core import Workspace, Model |
| 102 | + ws = Workspace.from_config() |
| 103 | + Model.register(workspace=ws, model_path="model.pkl", model_name="my_model") |
| 104 | + ``` |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +### **9. Deploy the Model** |
| 109 | +- Create an **inference configuration** and deploy to a web service: |
| 110 | + ```python |
| 111 | + from azureml.core.environment import Environment |
| 112 | + from azureml.core.model import InferenceConfig |
| 113 | + from azureml.core.webservice import AciWebservice |
| 114 | + |
| 115 | + env = Environment.from_conda_specification(name="myenv", file_path="env.yml") |
| 116 | + inference_config = InferenceConfig(entry_script="score.py", environment=env) |
| 117 | + |
| 118 | + deployment_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1) |
| 119 | + service = Model.deploy(workspace=ws, |
| 120 | + name="my-service", |
| 121 | + models=[model], |
| 122 | + inference_config=inference_config, |
| 123 | + deployment_config=deployment_config) |
| 124 | + service.wait_for_deployment(show_output=True) |
| 125 | + ``` |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +### **10. Test the Endpoint** |
| 130 | +- Once deployed, you can send HTTP requests to the endpoint to get predictions. |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +<div align="center"> |
| 135 | + <h3 style="color: #4CAF50;">Total Visitors</h3> |
| 136 | + <img src="https://profile-counter.glitch.me/brown9804/count.svg" alt="Visitor Count" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/> |
| 137 | +</div> |
0 commit comments