Skip to content

Instantly share code, notes, and snippets.

@gpedote
Forked from perrygeo/normalize_numpy.py
Created June 23, 2017 20:51
Show Gist options
  • Save gpedote/d8dcd9d6ce2186744eee4e766b5334e3 to your computer and use it in GitHub Desktop.
Save gpedote/d8dcd9d6ce2186744eee4e766b5334e3 to your computer and use it in GitHub Desktop.
Normalize a 2D numpy array so that each "column" is on the same scale (Linear stretch from lowest value = 0 to highest value = 100)
import numpy as np
rawpoints = np.array(
[[2500, 0.15, 12],
[1200, 0.65, 20],
[6200, 0.35, 19]]
)
# Scale the rawpoints array so that each "column" is
# normalized to the same scale
# Linear stretch from lowest value = 0 to highest value = 100
high = 100.0
low = 0.0
mins = np.min(rawpoints, axis=0)
maxs = np.max(rawpoints, axis=0)
rng = maxs - mins
scaled_points = high - (((high - low) * (maxs - rawpoints)) / rng)
"""
scaled points ->
[[ 26., 0., 0., ],
[ 0., 100., 100., ],
[ 100., 40., 87.5,]]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment