1 00:00:00,660 --> 00:00:04,780 Maximum and minimum values are different from the previous types of metrics. 2 00:00:05,790 --> 00:00:08,630 Instead of providing a figure that takes the whole range 3 00:00:08,630 --> 00:00:12,230 it provides a number that represents the end of a range. 4 00:00:12,230 --> 00:00:16,440 In the last section we worked on calculating the total and averages. 5 00:00:16,440 --> 00:00:19,370 Both of these measures take into consideration the whole range. 6 00:00:20,580 --> 00:00:25,590 Here instead of the whole range we look at the minimum or the maximum value. 7 00:00:25,590 --> 00:00:28,130 We only want to grab the element relevent to us. 8 00:00:28,130 --> 00:00:30,520 It's quite often just one value. 9 00:00:30,520 --> 00:00:34,300 The maximum value is the greatest element in a sample. 10 00:00:34,300 --> 00:00:38,830 Similarly, the minimum is the spas element in a sample. 11 00:00:38,830 --> 00:00:41,060 >> We can use Python's built in functions for 12 00:00:41,060 --> 00:00:45,310 lists to find the maximum and the minimum values from a list. 13 00:00:46,540 --> 00:00:48,810 Since we're starting with a list of string elements, 14 00:00:48,810 --> 00:00:52,310 we have to convert the data types to float. 15 00:00:52,310 --> 00:00:55,050 The max and min only work with number types. 16 00:00:55,050 --> 00:01:02,209 [SOUND] We start 17 00:01:02,209 --> 00:01:09,368 with creating 18 00:01:09,368 --> 00:01:14,737 a function 19 00:01:14,737 --> 00:01:21,897 find_max and 20 00:01:21,897 --> 00:01:26,669 we pass in 21 00:01:26,669 --> 00:01:30,845 the data 22 00:01:30,845 --> 00:01:38,004 sample as well 23 00:01:38,004 --> 00:01:43,373 as a column 24 00:01:43,373 --> 00:01:48,159 number. 25 00:01:48,159 --> 00:01:54,275 In our case we want to find the maximum of price which is the third element. 26 00:01:54,275 --> 00:01:59,885 [SOUND] And then we'll take the price and 27 00:01:59,885 --> 00:02:02,945 append it to a list and 28 00:02:02,945 --> 00:02:08,048 then we'll find the max from that. 29 00:02:08,048 --> 00:02:13,757 You could use a list comprehension to make this a more condensed form, but this 30 00:02:13,757 --> 00:02:19,568 is a little bit more readable so I'm just gonna use this to show you how it works. 31 00:02:19,568 --> 00:02:24,748 Now let me print this out so you can see the output. 32 00:02:24,748 --> 00:02:27,922 [SOUND] So, 33 00:02:27,922 --> 00:02:33,568 we have 7, 11. 34 00:02:33,568 --> 00:02:35,410 So, let me comment out. 35 00:02:39,160 --> 00:02:45,900 I'm gonna comment out those lines, clear it, and run it again. 36 00:02:45,900 --> 00:02:50,718 The most expensive tie in our data sample is 711. 37 00:02:50,718 --> 00:02:57,406 [SOUND] Let's find the minimum, 38 00:02:57,406 --> 00:03:02,608 that would be just the two 39 00:03:02,608 --> 00:03:07,810 character is changed and 40 00:03:07,810 --> 00:03:15,498 also those two characters changed. 41 00:03:15,498 --> 00:03:20,419 And let's see what the least expensive tie would be \$10, 42 00:03:20,419 --> 00:03:26,500 one thing to note here is that we have this list repeated in both functions so 43 00:03:26,500 --> 00:03:31,060 maybe we can try to truncate our two functions into one. 44 00:03:35,445 --> 00:03:42,535 So let's create something called find_max_min 45 00:03:42,535 --> 00:03:45,055 we'll take in a data sample as well as the column number, 46 00:03:45,055 --> 00:03:49,850 but we're also gonna have a value for the minimum or max. 47 00:03:49,850 --> 00:03:55,057 I'm gonna call it [SOUND] I'm gonna actually 48 00:03:55,057 --> 00:04:00,518 have two temporary values inside this function and 49 00:04:00,518 --> 00:04:06,364 then I'm gonna repeat that forward loop from before. 50 00:04:06,364 --> 00:04:13,786 [SOUND] I want to look at every single tie in the data sample and 51 00:04:13,786 --> 00:04:17,804 pull out the price for each tie. 52 00:04:17,804 --> 00:04:22,147 And then, I'm gonna check whether 53 00:04:22,147 --> 00:04:27,937 the condition that was passed in equals to max or 54 00:04:27,937 --> 00:04:32,590 maybe the condition was equal to min. 55 00:04:34,230 --> 00:04:36,790 We also just want one last one. 56 00:04:36,790 --> 00:04:42,629 [SOUND] Just in case the input was not max or min. 57 00:04:42,629 --> 00:04:47,241 [SOUND] If it's max, 58 00:04:47,241 --> 00:04:52,667 you want to say the value 59 00:04:52,667 --> 00:04:59,189 equals maximum of the list. 60 00:04:59,189 --> 00:05:00,749 If it's the minimum, 61 00:05:00,749 --> 00:05:05,769 we want to say that the value to return should be the minimum of that list. 62 00:05:05,769 --> 00:05:11,286 [SOUND] 63 00:05:11,286 --> 00:05:15,700 Oops 64 00:05:15,700 --> 00:05:22,321 we have 65 00:05:22,321 --> 00:05:30,048 a typo. 66 00:05:30,048 --> 00:05:31,405 We should add in the r. 67 00:05:31,405 --> 00:05:37,745 [SOUND] All right, so we definitely need the third argument, 68 00:05:37,745 --> 00:05:43,213 but just to get around this error in the future in case 69 00:05:43,213 --> 00:05:48,308 we forget we can set the default value to be max and 70 00:05:48,308 --> 00:05:53,676 we can say we want to get the minimum, just for now. 71 00:05:53,676 --> 00:05:58,661 [SOUND] We forgot to pass back 72 00:05:58,661 --> 00:06:03,434 the, return value. 73 00:06:03,434 --> 00:06:08,739 [SOUND] There we go, 74 00:06:08,739 --> 00:06:12,719 we have 10, 75 00:06:12,719 --> 00:06:16,374 \$10 tie. 76 00:06:16,374 --> 00:06:19,650 Let me clear that. 77 00:06:19,650 --> 00:06:22,800 All right so that's what returns from that function. 78 00:06:24,360 --> 00:06:29,360 Now let's move on and see how NumPy calculates the maximum and minimum. 79 00:06:30,980 --> 00:06:36,605 NumPy provides functions that make it easy to get your maximum and minimum as well. 80 00:06:36,605 --> 00:06:43,482 So, what you would do is say numpy_max equals numpy.amax, 81 00:06:43,482 --> 00:06:48,280 and we're gonna say prices_in_float. 82 00:06:48,280 --> 00:06:52,891 [NOISE] We have price 83 00:06:52,891 --> 00:06:57,790 from before that was 84 00:06:57,790 --> 00:07:02,977 the price column from 85 00:07:02,977 --> 00:07:07,309 the numpy array. 86 00:07:07,309 --> 00:07:13,318 And then, we also said that price_in_float was 87 00:07:13,318 --> 00:07:20,630 a list comprehension where we converted the x for x in price. 88 00:07:22,640 --> 00:07:30,559 So if we print out the numpy_max. 89 00:07:30,559 --> 00:07:33,618 [SOUND] We'll get 711 and 90 00:07:33,618 --> 00:07:38,805 then we can also do the same to get the minimum, 91 00:07:38,805 --> 00:07:42,672 which is replace the max with min. 92 00:07:42,672 --> 00:07:50,604 [SOUND] And if we were to print that, 93 00:07:50,604 --> 00:07:54,711 we'll see that. 94 00:07:54,711 --> 00:07:57,152 The number that comes back is the minimum value. 95 00:07:57,152 --> 00:08:03,409 [SOUND] And there we go. 96 00:08:03,409 --> 00:08:06,014 So I'm gonna remove that line so 97 00:08:06,014 --> 00:08:10,840 that we input this to other files it won't be distracting.