Esempio n. 1
0
 def test_linear_search(self):
 	test_list = utils.random_list(20, 1, 100)
 	test_list.append(1)
 	result = algorithm_easy.linear_search(1, test_list)
 	self.assertEqual(True, result)
 	result = algorithm_easy.linear_search(101, test_list)
 	self.assertEqual(False, result)
Esempio n. 2
0
 def test_quick_sort_rec(self):
     collection = utils.random_list(10000, 1, 1000)
     result = sorting.qs(collection)
     is_sorted = sorting.isSorted(result)
     self.assertEqual(True, is_sorted)
     collection = utils.reversed_sorted_list(1, 10000)
     result = sorting.quick_sort_rec(collection)
     is_sorted = sorting.isSorted(result)
     self.assertEqual(True, is_sorted)
     collection = utils.sorted_list(1, 10000)
     result = sorting.quick_sort_rec(collection)
     is_sorted = sorting.isSorted(result)
     self.assertEqual(True, is_sorted)
Esempio n. 3
0
 def test_merge_sort(self):
     collection = utils.random_list(10000, 1, 1000)
     result = sorting.merge_sort(collection)
     is_sorted = sorting.isSorted(result)
     self.assertEqual(True, is_sorted)
     collection = utils.reversed_sorted_list(1, 10000)
     result = sorting.merge_sort(collection)
     is_sorted = sorting.isSorted(result)
     self.assertEqual(True, is_sorted)
     collection = utils.sorted_list(1, 10000)
     result = sorting.merge_sort(collection)
     is_sorted = sorting.isSorted(result)
     self.assertEqual(True, is_sorted)
Esempio n. 4
0
 def test_quick_sort_inplace(self):
     collection = utils.random_list(10000, 1, 1000)
     sorting.qs_inplace(collection)
     is_sorted = sorting.isSorted(collection)
     self.assertEqual(True, is_sorted)
     collection = utils.reversed_sorted_list(1, 10000)
     sorting.qs_inplace(collection)
     is_sorted = sorting.isSorted(collection)
     self.assertEqual(True, is_sorted)
     collection = utils.sorted_list(1, 10000)
     sorting.qs_inplace(collection)
     is_sorted = sorting.isSorted(collection)
     self.assertEqual(True, is_sorted)
Esempio n. 5
0
def sgd(train_data,
        train_label,
        test_data,
        test_label,
        method,
        max_iteration=5,
        step=0.01):
    n, d = train_data.shape

    # beta = np.random.random((1, d))
    beta = np.zeros((1, d))
    beta[:, :] = 0.01
    train_errors = []
    test_errors = []
    losses = []
    iteration_count = 0
    sample = 0.0

    for i in range(max_iteration):
        index_list = random_list(n)

        for i in range(n):
            index = index_list.pop()

            if method == 'logistic':
                beta = lr_iteration(beta, train_data[index],
                                    train_label[index], lamda, gamma)
            elif method == 'ridge':
                beta = ridge_iteration(beta, train_data[index],
                                       train_label[index], lamda, gamma)

            if np.math.floor(max_iteration * n * sample) == iteration_count:
                train_error = error(beta, train_data, train_label)
                test_error = error(beta, test_data, test_label)
                loss = compute_loss(beta, train_data, train_label, method,
                                    lamda)
                train_errors.append(train_error)
                test_errors.append(test_error)
                losses.append(loss)

                print "t: {}, loss:{}, error: {}".format(
                    sample, loss, train_error)
                sample += float(step)

            iteration_count += 1
    return beta, train_errors, test_errors, losses
Esempio n. 6
0
    def __init__(self):
        # Tk init
        Tk.__init__(self)
        self.title("Visualized Sorting Algorithms")
        self.geometry("{}x{}".format(WIDTH, HEIGHT))
        self.resizable(False, False)

        # Sorting control variables
        self.sorting = False
        data = random_list(0, 100, 200)
        self.sort = list(self.ALGORITHMS.values())[0](data) # First algorithm

        self.sort_delay = 1000/DEFAULT_SORT_SPEED # delay, in milliseconds
        self.tick_timer = current_milli_time()

        # Visualizer init
        self.visualizer = Visualizer(self, width=CANVAS_WIDTH, height=CANVAS_HEIGHT, bd=0, highlightthickness=0)

        # Control panel init
        self.control_panel = ControlPanel(self)

        # Main loop
        while True:
            self.tick()
Esempio n. 7
0
 def new_algo(self, algo, min_val, max_val, size):
     data = random_list(min_val, max_val, size)
     self.sort = self.ALGORITHMS[algo](data)
     self.visualizer.algo_name = algo
     self.pause()
Esempio n. 8
0
# source: https://www.practicepython.org/exercise/2014/02/15/03-list-less-than-ten.html
from utils import random_list, input_number
a = random_list(100, 100)
num = input_number(
    'Give me a number and I\'ll print a list of numbers that are smaller than that given number:'
)
print('Original list:')
print(a)
print('Numbers that are smaller than {}:'.format(num))
print([x for x in a if x < num])
Esempio n. 9
0
def list_ends():
    a = random_list(200, 10000)
    print('List elements')
    print(a)
    print('First and last elements')
    return [a[0], a[-1]]
Esempio n. 10
0
for i in range(1000):
    results.append(simulate_gachapon(15))

q75, q25 = np.percentile(results, [75, 25])
iqr = q75 - q25
h = 2 * iqr * len(results)**(-1 / 3)
bins = int((max(results) - min(results)) / h)
"""plt.hist(results, bins=bins)
plt.title('Distribution of gachapon results')
plt.ylabel('Number of occurrences')
plt.xlabel('Number of iterations until all prizes collected')
plt.savefig('problem_2_plot.png')"""

length_list = []
runtimes = []

for i in range(50, 2500, 50):
    length_list.append(i)
    my_list = random_list(i)
    initial_time = time.time()
    get_element_counts(my_list)
    final_time = time.time()
    runtimes.append(final_time - initial_time)

plt.scatter(length_list, runtimes)
plt.title('Runtime vs. List Length')
plt.ylabel('Runtime (s)')
plt.xlabel('Length of list (number of elements)')
plt.savefig('problem_3_plot.png')
Esempio n. 11
0
# source: https://www.practicepython.org/exercise/2014/03/05/05-list-overlap.html
from utils import random_list

a = random_list(50, 50)
print('List a:')
print(sorted(a))

b = random_list(50, 50)
print('List b:')
print(sorted(b))

c = [x for x in set(a+b) if x in a and x in b]
print('List c:')
print(sorted(c))
Esempio n. 12
0
def cluster_gen(directory, clusters, data, raw_data, generator_network, \
                     isShown = False, rafd_kid = True, alpha=1,
                     isAdjustWeight = False,
                     isBackward = True,
                     isRandWeight = False,
                     weight_bound = [0.6, 1],
                     k = 100,
                    ):
    '''
    Inputs for this code block:
    - data
    - raw_data for calling 'imagePath'
    - path to latent vectors folder
    - functions:
    + hierarchical_partition
    + generate_images
    + face_recognition.face_locations
    + face_recognition.face_encodings
    + fakeid_quality

    Output for this code block:
    - fakeid_dist_avg
    - all_distances
    '''
    cluster_index_dict = cluster_idx_dict(clusters)
    filenames = get_filenames(directory, isFolder=isBackward)
    data_frame = []
    for i, vectorPath in enumerate(filenames):
        d = [{
            'cluster': clusters[i],
            'encoding': data[i],
            'latentVector': np.load(vectorPath).reshape((1, 18, -1))
        }]
        data_frame.extend(d)
    df_stats = pd.DataFrame(data_frame)
    labelList = np.unique(clusters)
    information_loss = []
    fakeid_dist_avg = []
    all_distances = []

    #for cluster_id in range(448,449):
    for cluster_id in tqdm_notebook(range(0, len(labelList)),
                                    desc='[Generating]: '):
        #for cluster_id in labelList:
        ################ print ids in cluster ##########
        if isShown:
            show_cluster_ids(cluster_id, cluster_index_dict, raw_data)
        #################################################
        cluster_vectors = [
            x['latentVector'] for x in data_frame if x['cluster'] == cluster_id
        ]
        if isRandWeight:
            weights = random_list(weight_bound[0], weight_bound[1],
                                  len(cluster_vectors))
            mix = alpha * mix_function(cluster_vectors, weights)
        else:
            mix = alpha * sum(cluster_vectors) / len(cluster_vectors)

        df_stats.loc[df_stats.cluster == cluster_id, 'mix_latent'] = \
            df_stats.loc[df_stats.cluster == cluster_id].apply(lambda row: mix, axis = 1)
        # print(mix.shape)
        img = generate_images(generator_network, mix, z=False)[0]
        quality_fakeid, distances, df_stats = \
             dist_to_fakeid(img, cluster_id, clusters, data, df_stats)

        information_loss.append(quality_fakeid)
        fakeid_dist_avg.append(quality_fakeid / len(cluster_vectors))
        all_distances.append(distances)
        if isShown:
            plt.imshow(img)
            plt.axis('off')
            plt.title("Generated image for cluster %d" % cluster_id)
            plt.show()
    df_stats['disclosure'] = df_stats['fakeid_dist'].map(lambda row: 1
                                                         if row > 0.6 else 0)
    disclosure_prob = len(
        df_stats[df_stats['disclosure'] == 0]) / len(clusters)
    #Disclosure Risks Assessment
    if isAdjustWeight:
        previous_alpha = alpha
        while disclosure_prob > 1 / k:
            current_alpha = previous_alpha * 0.8
            df_stats = adjust_weights(df_stats,
                                      clusters,
                                      data,
                                      raw_data,
                                      cluster_index_dict,
                                      generator_network,
                                      alpha=current_alpha,
                                      beta=0.8,
                                      isShown_re_gen=isShown)
            disclosure_prob = len(
                df_stats[df_stats['disclosure'] == 0]) / len(clusters)
            previous_alpha = current_alpha
        #calculating below variables according to new df_stats
        information_loss = []
        all_distances = []
        fakeid_dist_avg = []
        for label in labelList:
            cluster_stats = df_stats[df_stats.cluster == label]
            information_loss.append(cluster_stats.information_loss.unique()[0])
            fakeid_dist_avg.append(cluster_stats.avg_IL.unique()[0])
            all_distances.append(cluster_stats.fakeid_dist.values.tolist())
        disclosure_prob = len(
            df_stats[df_stats['disclosure'] == 0]) / len(clusters)

    return fakeid_dist_avg, all_distances, information_loss, \
                labelList, disclosure_prob, df_stats