Skip to content
Snippets Groups Projects
Commit b2e05118 authored by Andy Kuemmel's avatar Andy Kuemmel
Browse files

Update...

Update f22/andy_lec_notes/lec12_Oct03_IterationAlgorithms/lec12_Oct03_IterationAlgorithms_completed.ipynb
parent f08c4664
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:6a76ef95 tags: %% Cell type:markdown id:6a76ef95 tags:
# Iteration Algorithms # Iteration Algorithms
Thanks to CS 220 campus students for their data and to Meena for putting together the project.py file Thanks to CS 220 campus students for their data and to Meena for putting together the project.py file
%% Cell type:markdown id:103da70b tags: %% Cell type:markdown id:103da70b tags:
## Learning Objectives ## Learning Objectives
- Iterate through a dataset using for idx in range(project.count()) - Iterate through a dataset using for idx in range(project.count())
- Compute the frequency of data that meets a certain criteria - Compute the frequency of data that meets a certain criteria
- Find the maximum or minimum value of a numeric column in a dataset - Find the maximum or minimum value of a numeric column in a dataset
- Handle missing numeric values when computing a maximum / minimum - Handle missing numeric values when computing a maximum / minimum
- Use the index of a maximum or minimum to access other information about that data item - Use the index of a maximum or minimum to access other information about that data item
- Use break and continue in for loops when processing a dataset - Use break and continue in for loops when processing a dataset
%% Cell type:code id:28961628 tags: %% Cell type:code id:28961628 tags:
``` python ``` python
import project import project
``` ```
%% Cell type:code id:d1dca7ae tags: %% Cell type:code id:d1dca7ae tags:
``` python ``` python
# TODO: inspect the project module's documentation by using help # TODO: inspect the project module's documentation by using help
help(project) help(project)
``` ```
%% Output %% Output
Help on module project: Help on module project:
NAME NAME
project project
FUNCTIONS FUNCTIONS
__init__() __init__()
count() count()
This function will return the number of records in the dataset This function will return the number of records in the dataset
get_age(idx) get_age(idx)
get_age(idx) returns the age of the student in row idx get_age(idx) returns the age of the student in row idx
get_latitude(idx) get_latitude(idx)
get_latitude(idx) returns the latitude of the student's favourite place in row idx get_latitude(idx) returns the latitude of the student's favourite place in row idx
get_lecture(idx) get_lecture(idx)
get_lecture(idx) returns the lecture of the student in row idx get_lecture(idx) returns the lecture of the student in row idx
get_longitude(idx) get_longitude(idx)
get_longitude(idx) returns the longitude of the student's favourite place in row idx get_longitude(idx) returns the longitude of the student's favourite place in row idx
get_major(idx) get_major(idx)
get_major(idx) returns the major of the student in row idx get_major(idx) returns the major of the student in row idx
get_pet_owner(idx) get_pet_owner(idx)
get_pet_owner(idx) returns the pet preference of student in row idx get_pet_owner(idx) returns the pet preference of student in row idx
get_piazza_topping(idx) get_piazza_topping(idx)
get_piazza_topping(idx) returns the preferred pizza toppings of the student in row idx get_piazza_topping(idx) returns the preferred pizza toppings of the student in row idx
get_procrastinator(idx) get_procrastinator(idx)
get_procrastinator(idx) returns whether student in row idx is a procrastinator get_procrastinator(idx) returns whether student in row idx is a procrastinator
get_runner(idx) get_runner(idx)
get_runner(idx) returns whether student in row idx is a runner get_runner(idx) returns whether student in row idx is a runner
get_sleep_habit(idx) get_sleep_habit(idx)
get_sleep_habit(idx) returns the sleep habit of the student in row idx get_sleep_habit(idx) returns the sleep habit of the student in row idx
get_zip_code(idx) get_zip_code(idx)
get_zip_code(idx) returns the residential zip code of the student in row idx get_zip_code(idx) returns the residential zip code of the student in row idx
DATA DATA
__student__ = [{'Age': '22', 'Latitude': '43.073051', 'Lecture': 'LEC0... __student__ = [{'Age': '22', 'Latitude': '43.073051', 'Lecture': 'LEC0...
FILE FILE
/Users/andrewkuemmel/cs220-f22/lectures/lec12_Oct03_IterationAlgorithms/project.py /Users/andrewkuemmel/cs220-f22/lectures/lec12_Oct03_IterationAlgorithms/project.py
%% Cell type:markdown id:7fb78f6b tags: %% Cell type:markdown id:7fb78f6b tags:
### How many students does the dataset have? ### How many students does the dataset have?
%% Cell type:code id:d67a080f tags: %% Cell type:code id:d67a080f tags:
``` python ``` python
project.count() project.count()
``` ```
%% Output %% Output
992 992
%% Cell type:markdown id:3c97d494 tags: %% Cell type:markdown id:3c97d494 tags:
### What is the age of the student at index 10? ### What is the age of the student at index 10?
%% Cell type:code id:bde8dc35 tags: %% Cell type:code id:bde8dc35 tags:
``` python ``` python
id_10_age = project.get_age(10) id_10_age = project.get_age(10)
id_10_age id_10_age
``` ```
%% Output %% Output
'21' '21'
%% Cell type:code id:b0f87a2c tags: %% Cell type:code id:b0f87a2c tags:
``` python ``` python
# TODO: what is the return value type of the get_age function? # TODO: what is the return value type of the get_age function?
type(id_10_age) type(id_10_age)
# in a CSV file, all data stored as a string # in a CSV file, all data stored as a string
``` ```
%% Output %% Output
str str
%% Cell type:markdown id:37898141 tags: %% Cell type:markdown id:37898141 tags:
### What is the lecture number of the student at index 20? ### What is the lecture number of the student at index 20?
%% Cell type:code id:ba993090 tags: %% Cell type:code id:ba993090 tags:
``` python ``` python
project.get_lecture(20) project.get_lecture(20)
``` ```
%% Output %% Output
'LEC001' 'LEC001'
%% Cell type:markdown id:6e3e3c0a tags: %% Cell type:markdown id:d6edf4d5 tags:
### What are the age and pizza choices of the first 10 students? ### What are the age and pizza choices of the first 10 students?
%% Cell type:code id:46ec7632 tags: %% Cell type:code id:883e25cc tags:
``` python ``` python
for i in range(10): for i in range(10):
print(project.get_age(i), project.get_piazza_topping(i)) print(project.get_age(i), project.get_piazza_topping(i))
``` ```
%% Output %% Output
22 none (just cheese) 22 none (just cheese)
none (just cheese) none (just cheese)
18 none (just cheese) 18 none (just cheese)
none (just cheese) none (just cheese)
none (just cheese) none (just cheese)
18 none (just cheese) 18 none (just cheese)
none (just cheese) none (just cheese)
18 pineapple 18 pineapple
18 none (just cheese) 18 none (just cheese)
18 pepperoni 18 pepperoni
%% Cell type:markdown id:af3d9d8c tags: %% Cell type:markdown id:af3d9d8c tags:
### How many current lecture (example: LEC001) students are in the dataset? ### How many current lecture (example: LEC001) students are in the dataset?
%% Cell type:code id:e024c488 tags: %% Cell type:code id:e024c488 tags:
``` python ``` python
frequency = 0 frequency = 0
for i in range(project.count()): for i in range(project.count()):
if project.get_lecture(i) == "LEC001": if project.get_lecture(i) == "LEC001":
frequency += 1 frequency += 1
print(frequency) print(frequency)
``` ```
%% Output %% Output
195 195
%% Cell type:markdown id:b9ff6434 tags: %% Cell type:markdown id:b9ff6434 tags:
### What is the age of the oldest student in the course? ### What is the age of the oldest student in the course?
%% Cell type:code id:38bd778a tags: %% Cell type:code id:38bd778a tags:
``` python ``` python
# find the maximum # find the maximum
# check for missing data # check for missing data
# check for data out of range # check for data out of range
max_age = 0 max_age = 0
for i in range(project.count()): for i in range(project.count()):
if project.get_age(i) == '': if project.get_age(i) == '':
continue continue
age = int(project.get_age(i)) age = int(project.get_age(i))
if age < 0 or age > 100: if age < 0 or age > 100:
continue continue
if age > max_age: if age > max_age:
max_age = age max_age = age
print(max_age) print(max_age)
``` ```
%% Output %% Output
53 53
%% Cell type:markdown id:b40a32fb tags: %% Cell type:markdown id:b40a32fb tags:
### What is the age of the youngest student in current lecture (example: LEC001)? ### What is the age of the youngest student in current lecture (example: LEC001)?
- use similar algorithm as above question - use similar algorithm as above question
%% Cell type:code id:ea77e0cd tags: %% Cell type:code id:ea77e0cd tags:
``` python ``` python
min_age = int(project.get_age(0) ) #assume first student is youngest,and has age min_age = int(project.get_age(0) ) #assume first student is youngest,and has age
for i in range(project.count()): for i in range(project.count()):
if project.get_lecture != "LEC001": if project.get_lecture(i) != "LEC001":
continue continue
if project.get_age(i) == '': if project.get_age(i) == '':
continue continue
age = int(project.get_age(i)) age = int(project.get_age(i))
if age < 0 or age > 100: if age < 0 or age > 100:
continue continue
if age < min_age: if age < min_age:
min_age = age min_age = age
print(min_age) print(min_age)
``` ```
%% Output %% Output
22 22
%% Cell type:code id:c6b48319 tags:
``` python
# REDO this using None as an initial value for min_age
min_age = None
for i in range(project.count()):
if project.get_lecture(i) != "LEC001":
continue
if project.get_age(i) == '':
continue
age = int(project.get_age(i))
if age < 0 or age > 100:
continue
if min_age == None or age < min_age:
min_age = age
print(min_age)
```
%% Output
17
%% Cell type:markdown id:296c2a49 tags: %% Cell type:markdown id:296c2a49 tags:
### What is the average age of students enrolled in CS220 / CS319? ### What is the average age of students enrolled in CS220 / CS319?
%% Cell type:code id:8b7c8367 tags: %% Cell type:code id:8b7c8367 tags:
``` python ``` python
total_age = 0 total_age = 0
num_students_with_age = 0 num_students_with_age = 0
for i in range(project.count()): for i in range(project.count()):
if project.get_age(i) == '': if project.get_age(i) == '':
continue continue
age = int(project.get_age(i)) age = int(project.get_age(i))
if age < 0 or age > 100: if age < 0 or age > 100:
continue continue
total_age += age total_age += age
num_students_with_age += 1 num_students_with_age += 1
average_age = total_age / num_students_with_age average_age = total_age / num_students_with_age
average_age average_age
``` ```
%% Output %% Output
19.608180839612487 19.608180839612487
%% Cell type:markdown id:48f1c791 tags: %% Cell type:markdown id:48f1c791 tags:
### What major is the youngest student in current lecture (example: LEC001) planning to declare? ### What major is the youngest student in current lecture (example: LEC001) planning to declare?
- now, we need to find some other detail about the youngest student - now, we need to find some other detail about the youngest student
- often, you'll have to keep track of ID of the max or min, so that you can retrive other details about that data entry - often, you'll have to keep track of ID of the max or min, so that you can retrive other details about that data entry
%% Cell type:code id:a524873b tags: %% Cell type:code id:a524873b tags:
``` python ``` python
# we will do this in lecture on Wednesday # we will do this in lecture on Wednesday
# use None
# keep track of two variables, the min_age and index of the min_age
min_age = None
min_age_idx = None
for i in range(project.count()):
if project.get_lecture(i) != "LEC001":
continue
if project.get_age(i) == '':
continue
age = int(project.get_age(i))
if age < 0 or age > 100:
continue
if min_age == None or age < min_age:
min_age = age
min_age_idx = i
print(min_age, project.get_major(min_age_idx))
``` ```
%% Output %% Output
17 17 Statistics
%% Cell type:code id:84adf236 tags:
``` python
# help(project)
```
%% Cell type:markdown id:5294702a tags: %% Cell type:markdown id:5294702a tags:
### Considering current lecture students (example: LEC001), what is the age of the first student residing at zip code 53715? ### Considering current lecture students (example: LEC001), what is the age of the first student residing at zip code 53715?
%% Cell type:code id:fada2a40 tags: %% Cell type:code id:fada2a40 tags:
``` python ``` python
# REDO THIS USING NONE
target_zip = 53715
target_lecture = "LEC001"
age_of_target = None
for i in range(project.count()): for i in range(project.count()):
# put a print statement here if project.get_lecture(i) == target_lecture and project.get_zip_code(i) == target_zip:
if project.get_lecture(i) == "LEC002" and project.get_zip_code(i) == "53715": age_of_target = project.get_age(i)
print(project.get_age(i), type(project.get_age(i))) break # often in a search we break to be efficient
break print(age_of_target)
``` ```
%% Output %% Output
19 <class 'str'> None
%% Cell type:code id:772f7a58 tags: %% Cell type:code id:7a3fdbf6 tags:
``` python ``` python
help(project) help(project)
``` ```
%% Output %% Output
Help on module project: Help on module project:
NAME NAME
project project
FUNCTIONS FUNCTIONS
__init__() __init__()
count() count()
This function will return the number of records in the dataset This function will return the number of records in the dataset
get_age(idx) get_age(idx)
get_age(idx) returns the age of the student in row idx get_age(idx) returns the age of the student in row idx
get_latitude(idx) get_latitude(idx)
get_latitude(idx) returns the latitude of the student's favourite place in row idx get_latitude(idx) returns the latitude of the student's favourite place in row idx
get_lecture(idx) get_lecture(idx)
get_lecture(idx) returns the lecture of the student in row idx get_lecture(idx) returns the lecture of the student in row idx
get_longitude(idx) get_longitude(idx)
get_longitude(idx) returns the longitude of the student's favourite place in row idx get_longitude(idx) returns the longitude of the student's favourite place in row idx
get_major(idx) get_major(idx)
get_major(idx) returns the major of the student in row idx get_major(idx) returns the major of the student in row idx
get_pet_owner(idx) get_pet_owner(idx)
get_pet_owner(idx) returns the pet preference of student in row idx get_pet_owner(idx) returns the pet preference of student in row idx
get_piazza_topping(idx) get_piazza_topping(idx)
get_piazza_topping(idx) returns the preferred pizza toppings of the student in row idx get_piazza_topping(idx) returns the preferred pizza toppings of the student in row idx
get_procrastinator(idx) get_procrastinator(idx)
get_procrastinator(idx) returns whether student in row idx is a procrastinator get_procrastinator(idx) returns whether student in row idx is a procrastinator
get_runner(idx) get_runner(idx)
get_runner(idx) returns whether student in row idx is a runner get_runner(idx) returns whether student in row idx is a runner
get_sleep_habit(idx) get_sleep_habit(idx)
get_sleep_habit(idx) returns the sleep habit of the student in row idx get_sleep_habit(idx) returns the sleep habit of the student in row idx
get_zip_code(idx) get_zip_code(idx)
get_zip_code(idx) returns the residential zip code of the student in row idx get_zip_code(idx) returns the residential zip code of the student in row idx
DATA DATA
__student__ = [{'Age': '22', 'Latitude': '43.073051', 'Lecture': 'LEC0... __student__ = [{'Age': '22', 'Latitude': '43.073051', 'Lecture': 'LEC0...
FILE FILE
/Users/andrewkuemmel/cs220-f22/lectures/lec12_Oct03_IterationAlgorithms/project.py /Users/andrewkuemmel/cs220-f22/lectures/lec12_Oct03_IterationAlgorithms/project.py
%% Cell type:markdown id:68793d99 tags: %% Cell type:markdown id:68793d99 tags:
## Self-practice ## Self-practice
%% Cell type:markdown id:2eeed867 tags: %% Cell type:markdown id:2eeed867 tags:
### How many current lecture (example: LEC001) students are runners? ### How many current lecture (example: LEC001) students are runners?
%% Cell type:markdown id:1ea57e12 tags: %% Cell type:markdown id:1ea57e12 tags:
### How many current lecture (example: LEC001) students are procrastinators? ### How many current lecture (example: LEC001) students are procrastinators?
%% Cell type:markdown id:cf0ac7c8 tags: %% Cell type:markdown id:cf0ac7c8 tags:
### How many current lecture (example: LEC001) students own or have owned a pet? ### How many current lecture (example: LEC001) students own or have owned a pet?
%% Cell type:markdown id:ffd5e10f tags: %% Cell type:markdown id:ffd5e10f tags:
### What sleep habit does the youngest student in current lecture (example: LEC001) have? ### What sleep habit does the youngest student in current lecture (example: LEC001) have?
- try to solve this from scratch, instead of copy-pasting code to find mimimum age - try to solve this from scratch, instead of copy-pasting code to find mimimum age
%% Cell type:markdown id:f255b95a tags: %% Cell type:markdown id:f255b95a tags:
### What sleep habit does the oldest student in current lecture (example: LEC001) have? ### What sleep habit does the oldest student in current lecture (example: LEC001) have?
- try to solve this from scratch, instead of copy-pasting code to find mimimum age - try to solve this from scratch, instead of copy-pasting code to find mimimum age
%% Cell type:markdown id:70a8ac57 tags: %% Cell type:markdown id:70a8ac57 tags:
### What is the minimum latitude (& corresponding longitude) of a student's place of interest? ### What is the minimum latitude (& corresponding longitude) of a student's place of interest?
- What place is this -> try to enter the lat, long on Google maps? - What place is this -> try to enter the lat, long on Google maps?
%% Cell type:markdown id:581ea197 tags: %% Cell type:markdown id:581ea197 tags:
### What is the maximum latitude (& corresponding longitude) of a student's place of interest? ### What is the maximum latitude (& corresponding longitude) of a student's place of interest?
- What place is this -> try to enter the lat, long on Google maps? - What place is this -> try to enter the lat, long on Google maps?
%% Cell type:code id:fcee0994 tags: %% Cell type:code id:fcee0994 tags:
``` python ``` python
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment