Skip to content

Instantly share code, notes, and snippets.

@galenseilis
Created October 16, 2022 15:41
Show Gist options
  • Select an option

  • Save galenseilis/e8d8776e9807d7950937887933c38984 to your computer and use it in GitHub Desktop.

Select an option

Save galenseilis/e8d8776e9807d7950937887933c38984 to your computer and use it in GitHub Desktop.
import numpy as np
def scale(x, a=0, b=1):
if a > b:
a, b = b, a
min_x = np.min(x)
xout = (x - min_x) / (np.max(x) - min_x)
xout *= (b - a)
xout += a
sigma_in = np.std(x, ddof=1)
mu_in = np.mean(x)
sigma_out = np.std(xout, ddof=1)
mu_out = np.mean(xout)
return (x - mu_in) * sigma_out / sigma_in + mu_out
if __name__ == '__main__':
x = np.array([1, 3, 4, 5, -1, -7])
print(scale(x, a=-1, b=1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment