Skip to content
16 changes: 14 additions & 2 deletions homeworks/assignment01_knn/k_nearest_neighbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def compute_distances_two_loops(self, X):
# not use a loop over dimension, nor use np.linalg.norm(). #
#####################################################################
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

dists[i, j] = np.sqrt(np.sum((X[i] - self.X_train[j]) ** 2))
# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
return dists

Expand All @@ -97,7 +97,7 @@ def compute_distances_one_loop(self, X):
# Do not use np.linalg.norm(). #
#######################################################################
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

dists[i, :] = np.sqrt(np.sum((X[i] - self.X_train) ** 2, axis=1))
# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
return dists

Expand Down Expand Up @@ -126,6 +126,12 @@ def compute_distances_no_loops(self, X):
#########################################################################
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

test_squared = np.sum(X ** 2, axis=1, keepdims=True)
train_squared = np.sum(self.X_train ** 2, axis=1)
inner_product = np.dot(X, self.X_train.T)

# Вычислите квадрат расстояния Евклида между всеми парами точек
dists = np.sqrt(test_squared - 2 * inner_product + train_squared)
# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
return dists

Expand Down Expand Up @@ -156,6 +162,10 @@ def predict_labels(self, dists, k=1):
#########################################################################
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

closest_k_indices = np.argsort(dists[i])[:k]
closest_y = self.y_train[closest_k_indices]


# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
#########################################################################
# TODO: #
Expand All @@ -166,6 +176,8 @@ def predict_labels(self, dists, k=1):
#########################################################################
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

counts = np.bincount(closest_y)
y_pred[i] = np.argmax(counts)

# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

Expand Down
19 changes: 13 additions & 6 deletions homeworks/assignment02_laplace/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ def mean_abs_deviation_from_median(x: np.ndarray):
####
# Do not change the class outside of this block
# Your code here
median = np.median(x) # Находим медиану для каждой фичи
deviation = np.abs(x - median) # Находим абсолютное отклонение от медианы
mean_deviation = np.mean(deviation) # Находим среднее абсолютное отклонение
return mean_deviation
####

def __init__(self, features):
def __init__(self, features, loc=0.0, scale=1.0):
'''
Args:
feature: A numpy array of shape (n_objects, n_features). Every column represents all available values for the selected feature.
loc: The location (mean) parameter of the Laplace distribution.
scale: The scale (standard deviation) parameter of the Laplace distribution.
'''
####
# Do not change the class outside of this block
self.loc = # YOUR CODE HERE
self.scale = # YOUR CODE HERE
self.loc = loc
self.scale = scale
####


Expand All @@ -33,7 +37,10 @@ def logpdf(self, values):
'''
####
# Do not change the class outside of this block
return
exponent = -np.abs(values - self.loc) / self.scale
log_density = -np.log(2 * self.scale) + exponent
return log_density

####


Expand Down