<commit_before>import os
import sys

tools_dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, tools_dir_path)

from tools import *

hw6_dir_path = os.path.join(tools_dir_path,"ass6")
sys.path.insert(0, hw6_dir_path)

from hw6 import transform

import numpy as np 

# import cvxopt
from cvxopt import matrix, solvers
solvers.options['show_progress'] = False

# from mpmath import mpf

# from itertools import chain

np.random.seed(0)

# VALIDATION

def restricted_training(data, training_total):
    training_set = DataML(data[:training_total], transform)
    model_weights = gen_models(training_set)
    return model_weights

def gen_models(training_set):
    k_values = range(3,8) # k values from question. 8 instead of 7 because range is not inclsuive
    weights = [ linear_percepton(training_set.z[:,:k + 1], training_set.y)
            for k in k_values ] # k+1 as bound is not inclusive
    return weights

def best_model(model_weights, testing_set):
    errors =  [ weight_error(
                    weight, testing_set.z[:,:len(weight)], testing_set.y)
        for weight in model_weights ]
    return len(model_weights[np.argmin(errors)]) - 1, errors # return k value that yields least error. see k_values

def trial(in_sample, out_of_sample):
    target_function = random_target_function()
    training_set = random_set(in_sample, target_function)
    pla_weight = pla(training_set.z, training_set.y) 
    # print("pla_weight : ", pla_weight)
    svm_weight = svm(training_set.z, training_set.y)
    # print("svm_weight : ", svm_weight)
    testing_set = random_set(out_of_sample, target_function)
    pla_error, svm_error = [ weight_error(weight, testing_set.z, testing_set.y)
            for weight in
            [ pla_weight, svm_weight] ]
    def helper(x):
        if x > 0:
            return 0
        else:
            return 1
    difference = pla_error - svm_error
    svm_better = helper(difference)
    total_support_vectors = sum([ 1 for x in svm_weight if x >= 10*-3 ])
    return svm_better, total_support_vectors


# def trial(in_sample, out_sample):
#     raw_data = pct.n_random_datapoint(out_sample)
#     data, linear_target_function = classify_data_linear_binary_random(raw_data)
#     training_set = DataML(data.z_y[:in_sample])
#     testing_set = DataML(data.z_y[in_sample:])
#     pla_weight = binary_percepton(training_set.z, training_set.y)
#     pla_error = check_error(testing_set.z, testing_set.y, pla_weight)
#     # print("pla_weight")
#     # print(pla_weight)
#     # print("pla_error")
#     # print(pla_error)
#     svm_weight = svm(training_set.z, training_set.y).flatten()
#     # print("svm_weight")
#     # print(svm_weight)
#     svm_error = check_error(testing_set.z, testing_set.y, svm_weight)
#     # print("svm_error")
#     # print(svm_error)
#     def helper(x):
#         if x <= 0:
#             return 0
#         else:
#             return 1
#     difference = pla_error - svm_error
#     svm_better = helper(difference)
#     total_support_vectors = sum([ 1 for x in svm_weight if x >= 10*-3 ])
#     return svm_better, total_support_vectors

    
def svm(x, y):<commit_msg>Remove unnecessary lines from hw7<commit_after>import os
import sys

tools_dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, tools_dir_path)

from tools import *

hw6_dir_path = os.path.join(tools_dir_path,"ass6")
sys.path.insert(0, hw6_dir_path)

from hw6 import transform

import numpy as np 

from cvxopt import matrix, solvers
solvers.options['show_progress'] = False

np.random.seed(0)

# VALIDATION

def restricted_training(data, training_total):
    training_set = DataML(data[:training_total], transform)
    model_weights = gen_models(training_set)
    return model_weights

def gen_models(training_set):
    k_values = range(3,8) # k values from question. 8 instead of 7 because range is not inclsuive
    weights = [ linear_percepton(training_set.z[:,:k + 1], training_set.y)
            for k in k_values ] # k+1 as bound is not inclusive
    return weights

def best_model(model_weights, testing_set):
    errors =  [ weight_error(
                    weight, testing_set.z[:,:len(weight)], testing_set.y)
        for weight in model_weights ]
    return len(model_weights[np.argmin(errors)]) - 1, errors # return k value that yields least error. see k_values

def trial(in_sample, out_of_sample):
    target_function = random_target_function()
    training_set = random_set(in_sample, target_function)
    pla_weight = pla(training_set.z, training_set.y) 
    # print("pla_weight : ", pla_weight)
    svm_weight = svm(training_set.z, training_set.y)
    # print("svm_weight : ", svm_weight)
    testing_set = random_set(out_of_sample, target_function)
    pla_error, svm_error = [ weight_error(weight, testing_set.z, testing_set.y)
            for weight in
            [ pla_weight, svm_weight] ]
    def helper(x):
        if x > 0:
            return 0
        else:
            return 1
    difference = pla_error - svm_error
    svm_better = helper(difference)
    total_support_vectors = sum([ 1 for x in svm_weight if x >= 10*-3 ])
    return svm_better, total_support_vectors

def svm(x, y):
{'fraction_non_alphanumeric': 0.1013578122011857, 'fraction_numerical': 0.009562057754828839, 'mean_word_length': 3.5478260869565217, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7502243', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1774, 'n_words': 399}