Skip to content

Instantly share code, notes, and snippets.

@titipata
Created May 12, 2015 05:44
Show Gist options
  • Save titipata/6fcea5c60bc664e542c4 to your computer and use it in GitHub Desktop.
Save titipata/6fcea5c60bc664e542c4 to your computer and use it in GitHub Desktop.

Can you test this?

def normalize(X):
    count = X.shape[1]
    for i in range(count):
        x = X[:,i]
        # There may be some nans, from missing values in data. 
        # A better strategy would be interpolate, but for now we just
        # use the mean.
        mean = np.nanmean(x)
        for i in range(len(x)):
            if x[i] == np.nan and i == 0:
                x[i] = x[i+1]
            elif x[i] == np.nan and i == len(x):
                x[i] = x[i-1]
            else:
                try:
                    x[i] = (x[i-1] + x[i+1])/2
                else:
                    x[i] = x[i-1]
        std = x.std() or 1.0
        X[:,i] = (x - mean)/std
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment