def bernoulli_distribution(prob=0.0, size=1): """ Generates a bernoulli distribution based on an input probability. Args: prob (float): probability of distribution. size (int): size of array. Returns: A Bernoulli distribution array. """ # Creating bernoulli array bernoulli_array = np.zeros(size) # Generating random number r = generate_uniform_random_number(0, 1, size) # For each dimension for i in range(size): # If random generated number if smaller than probability if (r[i] < prob): # Mark as one bernoulli_array[i] = 1 else: # If not, mark as zero bernoulli_array[i] = 0 return bernoulli_array
import opfython.math.random as r # Generating a random uniform number array u = r.generate_uniform_random_number(low=0.0, high=1.0, size=10) print(u) # Generating a random gaussian number array g = r.generate_gaussian_random_number(mean=0.5, variance=1.0, size=10) print(g)
def learn(self, X_train, Y_train, X_val, Y_val, n_iterations=10): """Learns the best classifier over a validation set. Args: X_train (np.array): Array of training features. Y_train (np.array): Array of training labels. X_val (np.array): Array of validation features. Y_val (np.array): Array of validation labels. n_iterations (int): Number of iterations. """ logger.info('Learning the best classifier ...') # Defines the maximum accuracy max_acc = 0 # Defines the previous accuracy previous_acc = 0 # Defines the iterations counter t = 0 while True: logger.info('Running iteration %d/%d ...', t+1, n_iterations) # Fits training data into the classifier self.fit(X_train, Y_train) # Predicts new data preds = self.predict(X_val) # Calculating accuracy acc = g.opf_accuracy(Y_val, preds) if acc > max_acc: max_acc = acc best_opf = copy.deepcopy(self) # Saves the iteration number best_t = t # Gathers which samples were missclassified errors = np.argwhere(Y_val != preds) # Defining the initial number of non-prototypes as 0 non_prototypes = 0 for n in self.subgraph.nodes: if n.status != c.PROTOTYPE: non_prototypes += 1 for err in errors: # Counter will receive the number of non-prototypes ctr = non_prototypes # While the counter is bigger than zero while ctr > 0: # Generates a random index j = int(r.generate_uniform_random_number(0, len(X_train))) # If the node on that particular index is not a prototype if self.subgraph.nodes[j].status != c.PROTOTYPE: # Swap the input nodes X_train[j, :], X_val[err, :] = X_val[err, :], X_train[j, :] # Swap the target nodes Y_train[j], Y_val[err] = Y_val[err], Y_train[j] # Decrements the number of non-prototypes non_prototypes -= 1 # Resets the counter ctr = 0 # If the node on that particular index is a prototype else: # Decrements the counter ctr -= 1 # Calculating difference between current accuracy and previous one delta = np.fabs(acc - previous_acc) # Replacing the previous accuracy as current accuracy previous_acc = acc # Incrementing the counter t += 1 logger.info('Accuracy: %s | Delta: %s | Maximum Accuracy: %s', acc, delta, max_acc) # If the difference is smaller than 10e-4 or iterations are finished if delta < 0.0001 or t == n_iterations: # Replaces current class with the best OPF self = best_opf logger.info('Best classifier has been learned over iteration %d.', best_t+1) break
def test_generate_uniform_random_number(): uniform_array = random.generate_uniform_random_number(0, 1, 5) assert uniform_array.shape == (5, )