Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

iterate over the dataset and compute the mean vector.

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
Comment

PREVIOUS NEXT
Code Example
Python :: python as integer ratio 
Python :: dataframe corr p value 
Python :: how to change the title of the top bar in python 
Python :: what is cls and args in python classmethod 
Python :: Generate bootstrap replicate of 1D data that return a particular operation on a range 
Python :: binning continuous values in pyspark 
Python :: Python RegEx re.compile() Set class [s,.] will match any whitespace character ‘,’ or ‘.’ 
Python :: genrate requirments.txt pytohn 
Python :: python lister éléments enum 
Python :: python datediff days 
Python :: ValueError: y_true and y_pred contain different number of classes 6, 2. Please provide the true labels explicitly through the labels argument. Classes found in y_true: [0 1 2 3 4 5] 
Python :: python project structure 
Python :: Normalize basic list data 
Python :: looping through the dict. and return the key with the highest value 
Python :: python package for facial emotion recognition 
Python :: appropriate graph for dataset visualization 
Python :: read sharepoint list using python 
Python :: python check if variable is module 
Python :: Select non-NaN rows and replace column value 
Python :: how to read file from terminal in python inside code 
Python :: pandas fast way to view distribution by group 
Python :: python copy file create intermediate directories 
Python :: reate the "soup." This is a beautiful soup object: 
Python :: qlabel click python 
Python :: frogenset ito list pandas 
Python :: how to convert variable in Python ? 
Python :: qt line edit set text python 
Python :: related name django 
Python :: Return an RDD containing all pairs of elements with matching keys in self and other. 
Python :: Prints out the schema in the tree format 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =