lab01-checkpoint.ipynb 10.1 KB
Newer Older
root's avatar
root committed
1
{
root's avatar
root committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lab 01\n",
    "\n",
    "Name: Mitch Gavars\n",
    "\n",
    "Class: CSCI 349 2021SP\n",
    "\n",
    "Insructor: Brian King"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Did you read the syllabus? All of it? Do you agree to abide by the cheating rules? Write a sentence clearly indicating your commitment to not cheating.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I will not lie, cheat, or steal in my academic endeavors."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### What are you hoping to get out of this course?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I hope to gain a deeper understanding of data mining. The only time I have ever really talked about data mnining is when talking about ethics. I hope to understand more about it from the development side and gain an understanding of the data mining algorithms."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Print the Python version (available in sys package)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.8.5 (default, Sep  4 2020, 07:30:14) \n",
      "[GCC 7.3.0]\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "print(sys.version)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Create a Python list of 10000 random integers in the range 1 to 100, using the random package. Name the list x_list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "x_list = [random.randint(1,100) for i in range(10000)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### What is the minimum value of x_list? What is the max value? What is the mode?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The min value of x_list is: 1\n",
      "The max value of x_list is: 100\n",
      "The mode of x_list is: 3\n"
     ]
    }
   ],
   "source": [
    "from statistics import mode\n",
    "\n",
    "print(\"The min value of x_list is: {}\".format(min(x_list)))\n",
    "print(\"The max value of x_list is: {}\".format(max(x_list)))\n",
    "print(\"The mode of x_list is: {}\".format(mode(x_list)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Write a function to take a list of numbers as a parameter, and return the average of the list. Then, use your function to report the average value of x_list, printed as a float with 2 places of precision."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The average of x_list is: 50.72\n"
     ]
    }
   ],
   "source": [
    "def average(a_list):\n",
    "    sum = 0\n",
    "    for i in range(len(a_list)):\n",
    "        sum += a_list[i]\n",
    "    avg = sum / len(a_list)\n",
    "    return avg\n",
    "\n",
    "print(\"The average of x_list is: {:.2f}\".format(average(x_list)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Create a list called x_hist that represents a histogram, i.e. a distribution of the numerical data, of x_list. Each entry in x_hist should contain the range of data in widths of 10. So, x_hist[0] represents the frequency of numbers between 1-10, x_hist[1] is the frequency of numbers between 11-20, and so on. Be sure to print x_hist at the end."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[978, 1000, 985, 1038, 970, 992, 987, 1047, 986, 1017]\n",
      "10000\n"
     ]
    }
   ],
   "source": [
    "x_hist = [0 for i in range(10)]\n",
    "\n",
    "for i in x_list:\n",
    "    x_hist[(i-1)//10] += 1\n",
    "    \n",
    "print(x_hist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### What is numpy? What are its strengths? Does it have any weaknesses?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "numpy is a Python library. It is used to ease work with arrays in Python. I do not exactly know if numpy has weaknesses. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Import the numpy package as np and print the numpy version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.19.2\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "print(np.__version__)\n",
    "# https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-1.php\n",
    "# thought it was just np.version not np.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### What is the primary object type in numpy? Can it store data of different types? Discuss."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The primary object type in numpy is an array. Just as with lists in Python, arrays in numpy can hold any data type. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Discuss the types of data available in numpy. You need not list every type. Just generalize, and discuss how the type system is different than the built-in types int and float in Python."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Data types in numpy consist of a lot of the same ones in Python (int, long, double, bool, etc.). The type system in numpy is different than in Python because it allows the interpretation of different types. Each data type in numpy is just a general block of information that can be interpreted as it is come across, whereas in Python each data type is its own object. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Create a numpy array from x_list. Reassign it as x_list. Show the contents."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[77. 67. 41. ... 12. 49. 23.]\n"
     ]
    }
   ],
   "source": [
    "x_list = np.array(x_list)\n",
    "print(x_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Redo the previous exercise, but set the data type of each value to 'float32'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "float32\n"
     ]
    }
   ],
   "source": [
    "x_list = np.array(x_list,'float32')\n",
    "#x_list.astype('float32')\n",
    "print(x_list.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Create a length 10 integer array filled with zeros"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n"
     ]
    }
   ],
   "source": [
    "zeros = np.zeros(10)\n",
    "print(zeros)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Create a float array of 3 rows and 4 columns, all initialized to one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "ones = np.ones((3,4),'float')\n",
    "print(ones)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Create an array of 20 values, evenly spaced between 1 and 3 (HINT – look at np.linspace)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.         1.10526316 1.21052632 1.31578947 1.42105263 1.52631579\n",
      " 1.63157895 1.73684211 1.84210526 1.94736842 2.05263158 2.15789474\n",
      " 2.26315789 2.36842105 2.47368421 2.57894737 2.68421053 2.78947368\n",
      " 2.89473684 3.        ]\n"
     ]
    }
   ],
   "source": [
    "arr_space = np.linspace(1,3,20)\n",
    "print(arr_space)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Set the numpy random seed to the value 12345, then create a 10 x 5 array of random integers on the interval [10, 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[12 15 11 14 19]\n",
      " [15 12 11 16 11]\n",
      " [19 17 16 10 12]\n",
      " [19 11 12 16 17]\n",
      " [17 17 18 17 11]\n",
      " [17 14 10 13 15]\n",
      " [17 13 11 15 12]\n",
      " [15 13 18 15 12]\n",
      " [15 13 10 16 18]\n",
      " [10 15 16 18 19]]\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(12345)\n",
    "arr_test = np.random.randint(10,20,(10,5))\n",
    "print(arr_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
root's avatar
root committed
440
441
442
 "nbformat": 4,
 "nbformat_minor": 4
}