• Sarah

Machine Learning with Python and Azure: An exercise.

I have been learning to use Azure through Microsoft Learn Azure Portal and Azure DevOps site. Here is what I learned about Azure Notebooks. I had a lot of fun with this one!


Per Microsoft:


"Notebooks a cloud-based platform for building and running Jupyter notebooks. Jupyter is an environment based on IPython that facilitates interactive programming and data analysis using a variety of programming languages, including Python. Jupyter notebooks enjoy widespread use in research and academia for mathematical modeling, machine learning, statistical analysis, and for teaching and learning how to code."


One cool thing is that Jupyter is free and web-based.


In this lab, I created an Azure Notebook and used three different Python libraries — scikit-learn, NumPy, and Seaborn — to analyze climate data collected by NASA.


Getting started was pretty easy...


I signed in at azure.notebooks.com and clicked on My Projects.


Then I clicked + New Project and entered Climate Change for the project name and climate-change as the project ID. Then unchecked the Public project box, and clicked the Create button.


Per the instructions, I clicked the + sign to add a notebook to this project and named it climatechange.ipynb and select Python 3.6 Notebook as the item type. Once it appears in the list, click the notebook to open it and start editing.


Now the notebook was open.


In the very first cell, set the cell type to Markdown and enter the Azure Notebook Climate Change Analysis into that cell. Then, in the toolbar, click the + button to add a new cell.


Set it so the cell type is Code, and then enter the following Python code into the cell:


import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
import seaborn as sns; sns.set()

Click the Run button to run the code cell and import the packages.


Bam!


Next, click File in the menu at the top of the page, and select Upload from the drop-down menu. Then upload the files named 5-year-mean-1951-1980.csv and 5-year-mean-1882-2014.csv from the provided link in the lesson to download. (Link not provided in this article.)


Select /project as the Destination Folder. Click Start Upload to upload the files, and OK once they successfully upload.


Place the cursor in the empty cell at the bottom of the notebook. Enter Import data as the text and change the cell type to Markdown. Now add a Code cell and paste in the following code.


yearsBase, meanBase = np.loadtxt('5-year-mean-1951-1980.csv', delimiter=',', usecols=(0, 1), unpack=True)
years, mean = np.loadtxt('5-year-mean-1882-2014.csv', delimiter=',', usecols=(0, 1), unpack=True)

Click the Run button to run the cell and use NumPy's loadtxt function to load the data.


Ta-Da!


Place the cursor in the empty cell at the bottom of the notebook. Change the cell type to Markdown and enter Create a scatter plot as the text.

Add a Code cell and paste in the following code, which uses Matplotlib to create a scatter plot.


plt.scatter(yearsBase, meanBase)
plt.title('scatter plot of mean temp difference vs year')
plt.xlabel('years', fontsize=12)
plt.ylabel('mean temp difference', fontsize=12)
plt.show()

Click Run to run the cell and create a scatter plot.


Very cool!













Now, moving on...


Place the cursor in the empty cell at the bottom of the notebook. Change the cell type to Markdown and enter Perform linear regression as the text.

Add a Code cell and paste in the following code.


# Creates a linear regression from the data points
m,b = np.polyfit(yearsBase, meanBase, 1)

# This is a simple y = mx + b line function
def f(x):
    return m*x + b

# This generates the same scatter plot as before, but adds a line plot using the function above
plt.scatter(yearsBase, meanBase)
plt.plot(yearsBase, f(yearsBase))
plt.title('scatter plot of mean temp difference vs year')
plt.xlabel('years', fontsize=12)
plt.ylabel('mean temp difference', fontsize=12)
plt.show()

# Prints text to the screen showing the computed values of m and b
print(' y = {0} * x + {1}'.format(m, b))
plt.show()

Now run the cell to display a scatter plot with a regression line.


Nice!














Place the cursor in the empty cell at the bottom of the notebook. Change the cell type to Markdown and enter Perform linear regression with scikit-learn as the text.

Add a Code cell and paste in the following code.


# Pick the Linear Regression model and instantiate it
model = LinearRegression(fit_intercept=True)

# Fit/build the model
model.fit(yearsBase[:, np.newaxis], meanBase)
mean_predicted = model.predict(yearsBase[:, np.newaxis])

# Generate a plot like the one in the previous exercise
plt.scatter(yearsBase, meanBase)
plt.plot(yearsBase, mean_predicted)
plt.title('scatter plot of mean temp difference vs year')
plt.xlabel('years', fontsize=12)
plt.ylabel('mean temp difference', fontsize=12)
plt.show()

print(' y = {0} * x + {1}'.format(model.coef_[0], model.intercept_))

Now run the cell to display a scatter plot with a regression line.


Sweet!













Alright, what's next...


Place the cursor in the empty cell at the bottom of the notebook. Change the cell type to Markdown and enter Perform linear regression with Seaborn as the text.

Add a Code cell and paste in the following code.


plt.scatter(years, mean)
plt.title('scatter plot of mean temp difference vs year')
plt.xlabel('years', fontsize=12)
plt.ylabel('mean temp difference', fontsize=12)
sns.regplot(yearsBase, meanBase)
plt.show()

Run the code cell to produce a scatter chart with a regression line and a visual representation of the range in which the data points are expected to fall.












Easy peasy!


The lesson ended with an exercise on sharing the notebook with others. I won't show you that last step but it was just a few clicks and done.


Overall, it was nice to have made it through this hands-on module. I really like Python and it was a great learning experience to use it for Machine Learning purposes, something I had never really done much of before. The Microsoft Learn site is really helpful and it's free. It gives you great hands-on practical experience, and I am definitely taking advantage to learn all I can. My only problem now is deciding whether or not to get certified, and in which path!


If you want to try some of the exercises or browse all their content visit MICROSOFT LEARN

Happiness is an inside job.

Contact Me

© 2023 by SarahMancinho.com