Skip to content

Commit f053484

Browse files
author
gitName
committed
AB#1008732: Create and understand classification models in machine learning
1 parent 09aafc6 commit f053484

File tree

3 files changed

+138
-22
lines changed

3 files changed

+138
-22
lines changed

learn-pr/azure/understand-classification-machine-learning/notebooks/6-3-exercise-build-regression-model.ipynb

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,31 @@
5656
"metadata": {},
5757
"outputs": [],
5858
"source": [
59-
"import graphing # custom graphing code. See our GitHub repo for details\n",
59+
"import matplotlib.pyplot as plt\n",
60+
"\n",
61+
"# Create subplots\n",
62+
"fig, axs = plt.subplots(2, 2)\n",
63+
"\n",
64+
"# Create box plots\n",
65+
"axs[0, 0].boxplot([dataset[\"avalanche\"], dataset[\"surface_hoar\"]])\n",
66+
"axs[0, 0].set_xticklabels(['Avalanche', 'Surface Hoar'])\n",
67+
"axs[0, 0].set_ylabel('Values')\n",
68+
"\n",
69+
"axs[0, 1].boxplot([dataset[\"avalanche\"], dataset[\"fresh_thickness\"]])\n",
70+
"axs[0, 1].set_xticklabels(['Avalanche', 'Fresh Thickness'])\n",
71+
"axs[0, 1].set_ylabel('Values')\n",
72+
"\n",
73+
"axs[1, 0].boxplot([dataset[\"avalanche\"], dataset[\"weak_layers\"]])\n",
74+
"axs[1, 0].set_xticklabels(['Avalanche', 'Weak Layers'])\n",
75+
"axs[1, 0].set_ylabel('Values')\n",
76+
"\n",
77+
"axs[1, 1].boxplot([dataset[\"avalanche\"], dataset[\"no_visitors\"]])\n",
78+
"axs[1, 1].set_xticklabels(['Avalanche', 'Visitors'])\n",
79+
"axs[1, 1].set_ylabel('Values')\n",
6080
"\n",
61-
"graphing.box_and_whisker(dataset, label_x=\"avalanche\", label_y=\"surface_hoar\", show=True)\n",
62-
"graphing.box_and_whisker(dataset, label_x=\"avalanche\", label_y=\"fresh_thickness\", show=True)\n",
63-
"graphing.box_and_whisker(dataset, label_x=\"avalanche\", label_y=\"weak_layers\", show=True)\n",
64-
"graphing.box_and_whisker(dataset, label_x=\"avalanche\", label_y=\"no_visitors\")"
81+
"plt.tight_layout()\n",
82+
"\n",
83+
"plt.show()"
6584
]
6685
},
6786
{
@@ -196,14 +215,24 @@
196215
"metadata": {},
197216
"outputs": [],
198217
"source": [
199-
"# plot the model\n",
200-
"# Show a graph of the result\n",
201-
"predict = lambda x: model.predict(pandas.DataFrame({\"weak_layers\": x}))\n",
202-
"\n",
203-
"graphing.line_2D([(\"Model\", predict)],\n",
204-
" x_range=[-20,40],\n",
205-
" label_x=\"weak_layers\", \n",
206-
" label_y=\"estimated probability of an avalanche\")"
218+
"import matplotlib.pyplot as plt\n",
219+
"import pandas as pd\n",
220+
"\n",
221+
"predict = lambda x: model.predict(pd.DataFrame({\"weak_layers\": x}))\n",
222+
"\n",
223+
"# Generate x values\n",
224+
"x_values = range(-20, 41)\n",
225+
"y_values = predict(x_values)\n",
226+
"\n",
227+
"# Create the plot\n",
228+
"plt.plot(x_values, y_values, label=\"Model\")\n",
229+
"\n",
230+
"# Add labels\n",
231+
"plt.xlabel(\"weak_layers\")\n",
232+
"plt.ylabel(\"estimated probability of an avalanche\")\n",
233+
"plt.legend()\n",
234+
"\n",
235+
"plt.show()"
207236
]
208237
},
209238
{

learn-pr/azure/understand-classification-machine-learning/notebooks/6-5-exercise-assess-regression-model.ipynb

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,20 @@
2727
"!pip install statsmodels\n",
2828
"!wget https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-learning/main/graphing.py\n",
2929
"!wget https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-learning/main/Data/avalanche.csv\n",
30-
"import graphing # custom graphing code. See our GitHub repo for details\n",
30+
"import matplotlib.pyplot as plt\n",
3131
"\n",
3232
"#Import the data from the .csv file\n",
3333
"dataset = pandas.read_csv('avalanche.csv', delimiter=\"\\t\")\n",
3434
"\n",
3535
"#Let's have a look at the data and the relationship we're going to model\n",
3636
"print(dataset.head())\n",
3737
"\n",
38-
"graphing.box_and_whisker(dataset, label_x=\"avalanche\", label_y=\"weak_layers\")"
38+
"plt.boxplot([dataset[\"avalanche\"], dataset[\"weak_layers\"]])\n",
39+
"\n",
40+
"plt.xlabel(\"Avalanches, Weak Layers\")\n",
41+
"plt.ylabel(\"Values\")\n",
42+
"\n",
43+
"plt.show()"
3944
]
4045
},
4146
{
@@ -133,10 +138,22 @@
133138
"metadata": {},
134139
"outputs": [],
135140
"source": [
141+
"import matplotlib.pyplot as plt\n",
142+
"import pandas as pd\n",
143+
"\n",
136144
"def predict(weak_layers):\n",
137-
" return model.predict(dict(weak_layers=weak_layers))\n",
145+
" return model.predict(pd.DataFrame({\"weak_layers\": weak_layers}))\n",
146+
"\n",
147+
"plt.scatter(test['weak_layers'], test['avalanche'])\n",
148+
"\n",
149+
"trendline_values = predict(test['weak_layers'])\n",
150+
"plt.plot(test['weak_layers'], trendline_values, color='red', label=\"Trendline\")\n",
151+
"\n",
152+
"plt.xlabel(\"weak_layers\")\n",
153+
"plt.ylabel(\"avalanche\")\n",
154+
"plt.legend()\n",
138155
"\n",
139-
"graphing.scatter_2D(test, label_x=\"weak_layers\", label_y=\"avalanche\", trendline=predict)"
156+
"plt.show()"
140157
]
141158
},
142159
{
@@ -154,7 +171,20 @@
154171
"metadata": {},
155172
"outputs": [],
156173
"source": [
157-
"graphing.scatter_2D(test, label_x=\"weak_layers\", label_y=\"avalanche\", x_range=[-20,20], trendline=predict)"
174+
"\n",
175+
"def predict(weak_layers):\n",
176+
" return model.predict(pd.DataFrame({\"weak_layers\": weak_layers}))\n",
177+
"\n",
178+
"\n",
179+
"plt.scatter(test['weak_layers'], test['avalanche'])\n",
180+
"trendline_values = predict(test['weak_layers'])\n",
181+
"plt.plot(test['weak_layers'], trendline_values, color='red', label=\"Trendline\")\n",
182+
"plt.xlim(-20, 20)\n",
183+
"plt.xlabel(\"weak_layers\")\n",
184+
"plt.ylabel(\"avalanche\")\n",
185+
"plt.legend()\n",
186+
"\n",
187+
"plt.show()"
158188
]
159189
},
160190
{

learn-pr/azure/understand-classification-machine-learning/notebooks/6-7-exercise-improve-classification.ipynb

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"!pip install statsmodels\n",
2828
"!wget https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-learning/main/graphing.py\n",
2929
"!wget https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-learning/main/Data/avalanche.csv\n",
30-
"import graphing # custom graphing code. See our GitHub repo for details\n",
3130
"\n",
3231
"#Import the data from the .csv file\n",
3332
"dataset = pandas.read_csv('avalanche.csv', delimiter=\"\\t\", index_col=0)\n",
@@ -254,15 +253,44 @@
254253
"metadata": {},
255254
"outputs": [],
256255
"source": [
257-
"graphing.model_to_surface_plot(model_with_interaction, [\"weak_layers\", \"wind\"], test)"
256+
"import matplotlib.pyplot as plt\n",
257+
"import numpy as np\n",
258+
"import pandas as pd\n",
259+
"from mpl_toolkits.mplot3d import Axes3D\n",
260+
"\n",
261+
"def predict(weak_layers, wind, surface_hoar=0, fresh_thickness=0, no_visitors=0):\n",
262+
" return model_with_interaction.predict(pd.DataFrame({\n",
263+
" \"weak_layers\": weak_layers,\n",
264+
" \"wind\": wind,\n",
265+
" \"surface_hoar\": surface_hoar,\n",
266+
" \"fresh_thickness\": fresh_thickness,\n",
267+
" \"no_visitors\": no_visitors\n",
268+
" }))\n",
269+
"\n",
270+
"# Generate a graph for weak_layers and wind\n",
271+
"weak_layers = np.linspace(-20, 20, 100)\n",
272+
"wind = np.linspace(-20, 20, 100)\n",
273+
"weak_layers_grid, wind_grid = np.meshgrid(weak_layers, wind)\n",
274+
"\n",
275+
"predicted_values = predict(weak_layers_grid.ravel(), wind_grid.ravel()).to_numpy().reshape(weak_layers_grid.shape)\n",
276+
"\n",
277+
"fig = plt.figure()\n",
278+
"ax = fig.add_subplot(111, projection='3d')\n",
279+
"ax.plot_surface(weak_layers_grid, wind_grid, predicted_values, cmap='viridis')\n",
280+
"\n",
281+
"ax.set_xlabel(\"Weak Layers\")\n",
282+
"ax.set_ylabel(\"Wind\")\n",
283+
"ax.set_zlabel(\"Predicted Values\")\n",
284+
"\n",
285+
"plt.show()"
258286
]
259287
},
260288
{
261289
"cell_type": "markdown",
262290
"id": "1d8af2ee",
263291
"metadata": {},
264292
"source": [
265-
"The graph is interactive - rotate it and explore how there's a clear s-shaped relationship between the features and probability.\n",
293+
"There's now a clear s-shaped relationship between the features and probability.\n",
266294
"\n",
267295
"Let's now look at the features that we've said can interact:"
268296
]
@@ -274,7 +302,36 @@
274302
"metadata": {},
275303
"outputs": [],
276304
"source": [
277-
"graphing.model_to_surface_plot(model_with_interaction, [\"no_visitors\", \"fresh_thickness\"], test)"
305+
"import matplotlib.pyplot as plt\n",
306+
"import numpy as np\n",
307+
"import pandas as pd\n",
308+
"from mpl_toolkits.mplot3d import Axes3D\n",
309+
"\n",
310+
"def predict(no_visitors, fresh_thickness, weak_layers=0, wind=0, surface_hoar=0):\n",
311+
" return model_with_interaction.predict(pd.DataFrame({\n",
312+
" \"no_visitors\": no_visitors,\n",
313+
" \"fresh_thickness\": fresh_thickness,\n",
314+
" \"weak_layers\": weak_layers,\n",
315+
" \"wind\": wind,\n",
316+
" \"surface_hoar\": surface_hoar\n",
317+
" }))\n",
318+
"\n",
319+
"# Generate the graph\n",
320+
"no_visitors = np.linspace(-20, 20, 100)\n",
321+
"fresh_thickness = np.linspace(-20, 20, 100)\n",
322+
"no_visitors_grid, fresh_thickness_grid = np.meshgrid(no_visitors, fresh_thickness)\n",
323+
"\n",
324+
"predicted_values = predict(no_visitors_grid.ravel(), fresh_thickness_grid.ravel()).to_numpy().reshape(no_visitors_grid.shape)\n",
325+
"\n",
326+
"fig = plt.figure()\n",
327+
"ax = fig.add_subplot(111, projection='3d')\n",
328+
"ax.plot_surface(no_visitors_grid, fresh_thickness_grid, predicted_values, cmap='viridis')\n",
329+
"\n",
330+
"ax.set_xlabel(\"No Visitors\")\n",
331+
"ax.set_ylabel(\"Fresh Thickness\")\n",
332+
"ax.set_zlabel(\"Predicted Values\")\n",
333+
"\n",
334+
"plt.show()"
278335
]
279336
},
280337
{

0 commit comments

Comments
 (0)