Skip to content

Commit aff94d4

Browse files
committed
Week 8 workshops (NumPy)
1 parent 4510b73 commit aff94d4

File tree

1 file changed

+276
-0
lines changed

1 file changed

+276
-0
lines changed
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Workshop 8 - NumPy"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"In today's workshop, you will work with a reduced set of data from [[1]](#References). This data describes measurements of different dry beans, used for designing a system for classifying different types of beans. To run this workshop, you will need to download the file `beans.txt` from Blackboard and place it in the same folder as your workshop notebook.\n",
15+
"\n",
16+
"Through the workshop, you will be expected to analyse the measured characteristics of the beans in the dataset:\n",
17+
"- [Loading the dataset](#Loading-the-dataset)\n",
18+
"- [Analysing the dataset](#Analysing-the-dataset)\n",
19+
" - [Array dimensions](#Array-dimensions)\n",
20+
" - [Exercise 1](#Exercise-1)\n",
21+
" - [Extreme values](#Extreme-values)\n",
22+
" - [Exercise 2](#Exercise-2)\n",
23+
" - [Exercise 3](#Exercise-3)\n",
24+
" - [Calculating new features](#Calculating-new-features)\n",
25+
" - [Exercise 4](#Exercise-5)\n",
26+
" - [Dataset statistics](#Dataset-statistics)\n",
27+
" - [Exercise 5](#Exercise-5)\n",
28+
" - [Exercise 6](#Exercise-6)\n",
29+
"- [References](#References)"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 6,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"import numpy as np "
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {},
44+
"source": [
45+
"## Loading the dataset\n",
46+
"\n",
47+
"The file `beans.txt` contains some measurements on 7 different types of beans. The below code snippet loads this data into a NumPy array, such that every row corresponds to a different bean.\n",
48+
"\n",
49+
"The columns correspond to the following measurements of the bean: **area**, **perimiter**, **major axis length**, **minor axis length**. The last column represents the **class** (type of bean), and is encoded as an integer number.\n",
50+
"\n",
51+
"The code for loading the dataset from the file `beans.txt` is provided. This code relies only on the material covered in the lectures and workshops so far. In the coming lectures, we will learn about libraries better suited for handling large data files."
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 7,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"with open('beans.txt', 'r') as beans_data:\n",
61+
" beans = np.array([line.strip().split(',') for line in beans_data], dtype='float')"
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"metadata": {},
67+
"source": [
68+
"To ensure we have correctly loaded the dataset, let's inspect the features (measurements) of the first bean:"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 8,
74+
"metadata": {},
75+
"outputs": [
76+
{
77+
"name": "stdout",
78+
"output_type": "stream",
79+
"text": [
80+
"[28395. 610.291 208.17811671 173.88874704\n",
81+
" 0. ]\n"
82+
]
83+
}
84+
],
85+
"source": [
86+
"print(beans[0])"
87+
]
88+
},
89+
{
90+
"cell_type": "markdown",
91+
"metadata": {},
92+
"source": [
93+
"## Analysing the dataset"
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"### Array dimensions\n",
101+
"\n",
102+
"Let's try and get a basic sense of the dataset by looking the number of samples it contains."
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"metadata": {},
108+
"source": [
109+
"#### Exercise 1\n",
110+
"\n",
111+
"How many different samples (beans) does this dataset contain?"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 11,
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"name": "stdout",
121+
"output_type": "stream",
122+
"text": [
123+
"13611\n"
124+
]
125+
}
126+
],
127+
"source": [
128+
"print(beans.shape[0])"
129+
]
130+
},
131+
{
132+
"cell_type": "markdown",
133+
"metadata": {},
134+
"source": [
135+
"### Extreme values"
136+
]
137+
},
138+
{
139+
"cell_type": "markdown",
140+
"metadata": {},
141+
"source": [
142+
"#### Exercise 2\n",
143+
"\n",
144+
"Let's get a deeper look into the dataset. What is the area of the largest bean in the datset?"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": []
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"#### Exercise 3\n",
159+
" \n",
160+
"Can you print all the features of the largest bean?"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"metadata": {},
167+
"outputs": [],
168+
"source": []
169+
},
170+
{
171+
"cell_type": "markdown",
172+
"metadata": {},
173+
"source": [
174+
"### Calculating new features"
175+
]
176+
},
177+
{
178+
"cell_type": "markdown",
179+
"metadata": {},
180+
"source": [
181+
"#### Exercise 4\n",
182+
"\n",
183+
"Beans have an approximately ellipsoid shape. The ratio between the major and minor axis can tell us something about the shape of the bean. Calculate the **aspect ratio** between the major and minor axis of every bean and store it in a new array `aspect_ratio`.\n",
184+
"\n",
185+
"**Extension:** Add this new informatio to the array representing the dataset as a new column. You will need [`numpy.transpose()`](https://numpy.org/doc/stable/reference/generated/numpy.transpose.html) and [`numpy.hstack()`](https://numpy.org/doc/stable/reference/generated/numpy.hstack.html#numpy.hstack)."
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": null,
191+
"metadata": {},
192+
"outputs": [],
193+
"source": []
194+
},
195+
{
196+
"cell_type": "markdown",
197+
"metadata": {},
198+
"source": [
199+
"### Dataset statistics"
200+
]
201+
},
202+
{
203+
"cell_type": "markdown",
204+
"metadata": {},
205+
"source": [
206+
"#### Exercise 5\n",
207+
"\n",
208+
"First, calculate the mean and standard deviation of the aspect ratio of all the beans."
209+
]
210+
},
211+
{
212+
"cell_type": "code",
213+
"execution_count": null,
214+
"metadata": {},
215+
"outputs": [],
216+
"source": [
217+
"mean_all = np.mean(beans[:, 5])\n",
218+
"std_all = np.std(beans[:, 5])\n",
219+
"print(mean_all, std_all)"
220+
]
221+
},
222+
{
223+
"cell_type": "markdown",
224+
"metadata": {},
225+
"source": [
226+
"#### Exercise 6\n",
227+
"\n",
228+
"Now, let's compare the above global statistics for the dataset to those of class 3. Claculate the mean and standard deviation of the aspect ratio of all the beans of class 3 (you will need to use mask idexing)."
229+
]
230+
},
231+
{
232+
"cell_type": "code",
233+
"execution_count": null,
234+
"metadata": {},
235+
"outputs": [],
236+
"source": []
237+
},
238+
{
239+
"cell_type": "markdown",
240+
"metadata": {},
241+
"source": [
242+
"You should be able to observe a difference between the mean and standard deviation of the measured characteristic between the whole dataset and class 3. **How would you interpret this difference** (especially the one in the standard deviation)?"
243+
]
244+
},
245+
{
246+
"cell_type": "markdown",
247+
"metadata": {},
248+
"source": [
249+
"### References\n",
250+
"\n",
251+
"[1] KOKLU, M. and OZKAN, I.A., (2020), \"Multiclass Classification of Dry Beans Using Computer Vision and Machine Learning Techniques.\" Computers and Electronics in Agriculture, 174, 105507. DOI: https://doi.org/10.1016/j.compag.2020.105507"
252+
]
253+
}
254+
],
255+
"metadata": {
256+
"kernelspec": {
257+
"display_name": "Python 3 (ipykernel)",
258+
"language": "python",
259+
"name": "python3"
260+
},
261+
"language_info": {
262+
"codemirror_mode": {
263+
"name": "ipython",
264+
"version": 3
265+
},
266+
"file_extension": ".py",
267+
"mimetype": "text/x-python",
268+
"name": "python",
269+
"nbconvert_exporter": "python",
270+
"pygments_lexer": "ipython3",
271+
"version": "3.12.6"
272+
}
273+
},
274+
"nbformat": 4,
275+
"nbformat_minor": 4
276+
}

0 commit comments

Comments
 (0)