''' DS2000 Spring 2023 Sample code from class -- classifier to use in a nearest-neighbor prediction model Our goal -- determine whether a show will win an emmy Two categories only: emmy / no emmy ''' from emmyshow import EmmyShow import csv SHOW_FILE = "shows.csv" def read_csv(filename): ''' Function: read_csv Parameters: filename, a string Returns: 2d list of strings Does: loops over each line in the file, which is saved as a list of strings, skips the header ''' data = [] with open(filename, "r") as infile: csvfile = csv.reader(infile) next(csvfile) for row in csvfile: data.append(row) return data def make_show_objs(lst): ''' Function: make_show_objs Parameters: 2d lst of strings Returns: lst of emmyShow objects Does: iterates over the list, turns each row into an object ''' objs = [] for row in lst[1:]: for i in range(len(row)): if row[i].isdigit(): row[i] = float(row[i]) tv = EmmyShow(row) objs.append(tv) return objs def find_min(distances): ''' Function: find_min Parameters: list of floats Return: tuple of float, int Does: finds smallest distance in the list, returns its value and position ''' min_val = float("inf") min_pos = -1 for i in range(len(distances)): if distances[i] < min_val: min_val = distances[i] min_pos = i return min_val, min_pos def main(): # gather data - read in the tv show info from the file raw_shows = read_csv(SHOW_FILE) # computation - turn the 2d list of strings into show objects tv_objs = make_show_objs(raw_shows) main()