שאלות ממבחני עבר

שימו לב: על מנת להריץ את התאים ב-Live Code, יש לייבא תחילה את ספרית numpy ע”י הרצת השורת הבאה

import numpy as np
import utils.load_auxilary_files # This will load the files used for this notebook
['orders_2_2024.csv', 'erosion.png', 'infile1.txt', 'countries-of-the-world.csv', 'monday.txt', 'StudentsGrades.csv', 'notebook_resources.zip', 'erosion_2.png', '__MACOSX', 'dog_noised.png', 'foods.txt', 'witcher_2.csv', 'dialation.png', 'evil_morty_change.png', 'ship.png', 'products2.csv', 'ex1.csv', 'evil_morty_change_3.png', 'evil_morty_1.png', 'baby.png', 'orders_3_2024.csv', 'koala.png', 'woman_noised.png', 'infile2.txt', 'orders_4_2024.csv', 'dog.png', 'witcher_1.csv', 'evil_morty_segmentation.png', 'out.txt', 'sunday.txt', 'ex2.csv', 'evil_morty_change_noised.png', 'dialation_2.png']

שאלות ממבחני עבר#

Exam 2023-2024 semester A Moed A

3.A

def get_nth_mask(im, n, idx):
    size = (im.max() - im.min()) / n
    mask_high = im >= (im.min() + size * idx)
    mask_low = im <= (im.min() + size * (idx + 1))
    return mask_high * mask_low
im = np.array([[3,5,9],[8,1,2],[7,6,4]])
print(get_nth_mask(im, 3, 0))
[[ True False False]
 [False  True  True]
 [False False False]]

3.B

def get_n_valued_image(im, n):
    out = np.zeros(im.shape)
    for i in range(n):
        mask = get_nth_mask(im, n, i)
        out[mask] = im[mask].mean()
    return out
im = np.array([[3,5,9],[8,1,2],[7,6,4]])
print(get_n_valued_image(im, 3))
[[2. 5. 8.]
 [8. 2. 2.]
 [8. 5. 5.]]

3.C

def compute_entropy(im):
    image_pixels = im.flatten()
    bin_prob = np.bincount(image_pixels) / (image_pixels.shape[0])
    bin_prob = bin_prob[bin_prob != 0]
    return (-bin_prob * np.log2(bin_prob)).sum()
im = np.array([[1,4],[4,8]])
compute_entropy(im)
np.float64(1.5)

what is entropy https://en.wikipedia.org/wiki/Entropy_(information_theory):

  • you are not required to understand the math behind such formulas

  • you are required to be able to implement a given formula such as entropy

3.D

im = np.arange(6).reshape((2, 3))
print((im[:, ::2] ** 2).sum())
38