0

I am working on the CS50AI "Shopping" project and received the following comments from the grader. I have checked the specification carefully but didn't spot any issue. The output seems to work as well.

Extracted my code below for reference. Any guidance / hint would be appreciated. This is my only pending project. Thank you!

Comments from grader:

The load_data function does not accept/process all of the data types it should be processing. Be sure everything is handled correctly! https://cs50.harvard.edu/ai/2023/projects/4/shopping/#specification

Code:

def load_data(filename):
    evidence = []
    label = []
    month = {'Jan': 0,
             'Feb': 1,
             'Mar': 2,
             'Apr': 3,
             'May': 4,
             'June': 5,
             'Jul': 6,
             'Aug': 7,
             'Sep': 8,
             'Oct': 9,
             'Nov': 10,
             'Dec': 11}

    counter = 0
    with open(sys.argv[1], 'r') as file:
        csvreader = csv.reader(file)
        for row in csvreader:
            if counter == 0: #I have used this if-else to exclude the first header row
                counter += 1
            else:
                evidence.append([
                    int(row[0]),
                    float(row[1]),
                    int(row[2]),
                    float(row[3]),
                    int(row[4]),
                    float(row[5]),
                    float(row[6]),
                    float(row[7]),
                    float(row[8]),
                    float(row[9]),
                    month[row[10]],
                    int(row[11]),
                    int(row[12]),
                    int(row[13]),
                    int(row[14]),
                    1 if row[15] == "Returning_Visitor" else 0,
                    1 if row[16] == "TRUE" else 0
                ])

                label.append(
                    1 if row[17] == "TRUE" else 0
                )
    return (evidence, label)

1 Answer 1

0

your load_data function takes in a parameter filename but you don't use it. Instead you appear to try to use sys.argv[1] You should not be looking at sys.argv[1] per the instructions, you should be using the filename passed in. This is the second sentence in specification instructions. Regarding the grader, what happens if one of the input data fields can't be converted to the type you desire?

second I would strongly consider using a csv.DictReader instead to avoid having to play games with the counter. I'd consider extracting each value as needed into a variable and then building a list from the variables and then appending that list to the target. That way you can be sure about the conversion of each of the values.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .