Итерация над набором данных и вычислить средний вектор.
In [5]:
# ===YOU SHOULD EDIT THIS FUNCTION===
def mean_naive(X):
"""Compute the mean for a dataset by iterating over the dataset
Arguments
---------
X: (N, D) ndarray representing the dataset.
N is the number of samples in the dataset
and D is the feature dimension of the dataset
Returns
-------
mean: (D, ) ndarray which is the mean of the dataset.
"""
N, D = X.shape
mean = np.zeros(D)
# The naive approach requires us to iterate over the whole dataset with a for loop.
for n in range(N):
mean = mean + X[n]/N
return mean
# ===YOU SHOULD EDIT THIS FUNCTION===
def cov_naive(X):
"""Compute the covariance for a dataset
Arguments
---------
X: (N, D) ndarray representing the dataset.
N is the number of samples in the dataset
and D is the feature dimension of the dataset
Returns
-------
covariance: (D, D) ndarray which is the covariance matrix of the dataset.
"""
N, D = X.shape
covariance = np.zeros((D, D))
for n in range(N):
covariance = covariance + np.dot((X[n]-mean_naive(X)).T, (X[n]-mean_naive(X)))/N
return covariance
samurai