コード例 #1
0
ファイル: main.py プロジェクト: obiorahm/EECS348
def run_experiment2(filename):
    training_data = common.init_data(common.constants.TRAINING_SIZE,
                                     common.constants.DATA_DIM + 1)
    test_data = common.init_data(common.constants.TEST_SIZE,
                                 common.constants.DATA_DIM + 1)
    gold_data = [0 for x in range(common.constants.TEST_SIZE)]

    print("Linear Classifier : Dataset 1")
    #generating data should be hidden from students!
    read_data(training_data, test_data, gold_data, filename)
    #this is one of the two student functions
    student_code.part_two_classifier(training_data, test_data)
    #part 1 grading

    error = 0
    for i in range(common.constants.TEST_SIZE):
        if (test_data[i][common.constants.DATA_DIM] != gold_data[i]):
            error += 1

    print("Incorrect classificattions is " + str(error) + " out of " +
          str(common.constants.TEST_SIZE))

    success = True

    if (error <= float(common.constants.TEST_SIZE) * .05):
        print("(" + bcolors.GREEN + "SUCCESS" + bcolors.NORMAL + ")")
    else:
        success = False
        print("(" + bcolors.RED + "FAIL" + bcolors.NORMAL + ") maximum " +
              str(float(common.constants.TEST_SIZE) * .05))

    print
    return success
コード例 #2
0
ファイル: student_code.py プロジェクト: newmayor/cs348
def part_two_classifier(data_train, data_test):
    # PUT YOUR CODE HERE
    # Access the training data using "data_train[i][j]"
    # Training data contains 3 cols per row: X in
    # index 0, Y in index 1 and Class in index 2
    # Access the test data using "data_test[i][j]"
    # Test data contains 2 cols per row: X in
    # index 0 and Y in index 1, and a blank space in index 2
    # to be filled with class
    # The class value could be a 0 or a 8

    w = [[0, 0] for x in range(10)]
    all_labeled = False
    c = common.init_data(common.constants.TRAINING_SIZE, 1)
    p = common.init_data(common.constants.TRAINING_SIZE, 1)

    while all_labeled != True:
        i = 0

        # print(i)
        for row in data_train:

            # print(i)
            # print(f"data: {row}, weight: {w}")
            c[i] = multi_classify_train(row, w)
            # print(f"class = {c}")
            if c[i] != row[2]:
                w[c[i]][0] -= (row[0]) * (1 / 100)
                w[c[i]][1] -= (row[1]) * (1 / 100)

                w[int(row[2])][0] += (row[0]) * (1 / 100)
                w[int(row[2])][1] += (row[1]) * (1 / 100)

            i += 1

        # print(f"checking trained labelling")

        for i in range(len(data_train)):
            p[i] = data_train[i][2]

        if check_all_labels(c, p):
            print(f"all classes are labeled correctly")
            all_labeled = True
    i = 0
    for row in data_test:
        data_test[i][2] = multi_classify_train(row, w)
        i += 1

    return