def compute(no_of_itr, prev, itr, prev_err): #Here i am assuming each weight are the sigmoid of #print(x.max(axis=0)) v = test_score.score(prev) sig = sigmoid(v) sig *= 0.01 h = prev min_score = v n = 0 i = 0 train_error_list = [] while True: #weight normalization by the factor of itr/(itr+1); new_h = (generate_random_predictor(21283, itr) * sig + prev) * (itr / (itr + 1)) score = test_score.score(new_h) #[0]; #choose the best fit by computing the error for specified number of random weak learners such that error will decrease if score < min_score and score <= v: min_score = score h = new_h if min_score < v and n > no_of_itr: break #if after the creation of 100 random weak learners the error does not decrease decrease the multiplyer if (n + 1) % 100 == 0: sig = sig / 2 if n > 1000: print(score) print(v) print('break') break n += 1 return h
def model(no_of_trees): prev = np.zeros((21283, 1)) prev_err = np.zeros((21283, 1)) previous_vector = np.zeros((21283, 1)) error_list = [] #create no of weak learners for i in range(no_of_trees): prev_err = previous_vector previous_vector = prev prev = compute(100, prev, i, prev_err) error_list.append(test_score.score(prev)) print(test_score.score(prev)) return error_list
def blindboost(y, itr, learning_rate, N): minscore = ts.score(y) z = (itr + 1) / (itr + 2) for i in range(3000): y_random = np.random.uniform(50, size=(N, 1)) yp = ((y_random * learning_rate) + y) * z score = ts.score(yp) if score < minscore and i > 100: break elif score < minscore: minscore = score if (i + 1) % 50 == 0: learning_rate /= 2 return yp, score
def threshold_gen(n): s = 0 for i in range(n): np.random.shuffle(weak_tensor) #random s += score(weak_tensor) #applies the func return s / n
#get aggregated learner def find_weak_learner(learners, index): if index == 0: return np.copy(learners[0]) final = np.copy(learners[0]) for id_i in range(1, index): final += learners[id_i] final /= index final[final < np.percentile(final, percentage * 100)] = 0 return final num_weak_learner = 1000 # number of weak learners score_average = [0] * num_weak_learner weak_learner = [0] * num_weak_learner i = 0 while i < num_weak_learner: np.random.shuffle(weak_tensor) if score(weak_tensor) < threshold_bound: weak_learner[i] = np.copy(weak_tensor) agg_l = find_weak_learner(weak_learner, i) score_average[i] = score(agg_l) i += 1 plt.plot(score_average) plt.xlabel('Numbers of weak learners') plt.ylabel('Scores') plt.show()
""" from test_score import score import numpy as np import matplotlib.pyplot as plt # read data true_values = np.genfromtxt('true_values_regression.csv', delimiter=',') true_values = np.expand_dims(true_values, axis=1) # compute threshold for checking if weak learner is good threshold = 0 # compute the average threshold for i in range(100): np.random.shuffle(true_values) threshold += score(true_values) threshold = threshold / 100 # init data scores = [0] * 10000 # error stores data error rate h = [0] * 10000 # h is weak learner percentage = np.sum(true_values == 0) / len(true_values) # Keep adding weak learner to our model i = 0 while i < 10000: np.random.shuffle(true_values) if score(true_values) < threshold: h[i] = np.copy(true_values) new_h = np.copy(h[0]) if i != 0: