שימו לב: על מנת להריץ את התאים ב-Live Code, יש לייבא תחילה את ספרית numpy ע”י הרצת השורת הבאה
import numpy as np
שאלות ממבחני עבר#
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