def test_four(in_sample, out_sample): training_set = random_set(in_sample, moved_circle) noisy_indices = np.random.choice(in_sample, size=round(0.1 * in_sample), replace=False) training_set.y[noisy_indices] *= -1 weight = linear_percepton(training_set.z, training_set.y) in_error_no_transform = weight_error(weight, training_set.z, training_set.y) training_set.z = second_order(training_set.z) weight = linear_percepton(training_set.z, training_set.y) in_error_transform = weight_error(weight, training_set.z, training_set.y) testing_set = random_set(out_sample, moved_circle, second_order) noisy_indices = np.random.choice(out_sample, size=round(0.1 * out_sample), replace=False) testing_set.y[noisy_indices] *= -1 out_error_transform = weight_error(weight, testing_set.z, testing_set.y) return in_error_no_transform, weight, out_error_transform
def test1(training_data, testing_data): training_set = DataML(training_data, transform) weight = linear_percepton(training_set.z, training_set.y) testing_set = DataML(testing_data, transform) in_error, out_error = [ weight_error(weight, data_set.z, data_set.y) for data_set in [training_set, testing_set] ] return in_error, out_error
def get_line_to_parameters(trials): """line_to : line through origin""" gradients = np.array([ linear_percepton(training_set.x, training_set.y) for training_set in trials ]) return gradients
def test_two(in_sample, out_sample): target_function = random_target_function() training_set = random_set(in_sample, target_function) weight = linear_percepton(training_set.z, training_set.y) in_error = weight_error(weight, training_set.z, training_set.y) testing_set = random_set(out_sample, target_function) out_error = weight_error(weight, testing_set.z, testing_set.y) return in_error, out_error
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 get_line_parameters(trials): new_trials = [ DataML((np.insert(training_set.z, 0, 1, axis=1), training_set.y)) for training_set in trials ] weights = np.array([ linear_percepton(training_set.z, training_set.y) for training_set in new_trials ]) return weights
def generate_quadratic_to_parameters(trials): """line_to : line through origin function of form ax^2 """ def transform(x): return x ** 2 new_trials = [ DataML((training_set.z, training_set.y), transform) for training_set in trials ] weights = np.array([ linear_percepton(training_set.x, training_set.y) for training_set in new_trials ]) return weights
def generate_quadratic_to_parameters(trials): """line_to : line through origin function of form ax^2 """ def transform(x): return x**2 new_trials = [ DataML((training_set.z, training_set.y), transform) for training_set in trials ] weights = np.array([ linear_percepton(training_set.x, training_set.y) for training_set in new_trials ]) return weights
def generate_quadratic_parameters(trials): """ax^2 + b""" def transform(x): """ transform x1 ---> 1 x1**2 """ ones = np.ones(len(x)) x1 = x[:, 0] x1_sqr = x1 ** 2 return np.stack([ones, x1_sqr], axis=1) new_trials = [ DataML((training_set.z, training_set.y), transform) for training_set in trials ] weights = [ linear_percepton(training_set.z, training_set.y) for training_set in new_trials ] return np.array(weights)
def generate_quadratic_parameters(trials): """ax^2 + b""" def transform(x): """ transform x1 ---> 1 x1**2 """ ones = np.ones(len(x)) x1 = x[:, 0] x1_sqr = x1**2 return np.stack([ones, x1_sqr], axis=1) new_trials = [ DataML((training_set.z, training_set.y), transform) for training_set in trials ] weights = [ linear_percepton(training_set.z, training_set.y) for training_set in new_trials ] return np.array(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