Skip to content

Instantly share code, notes, and snippets.

@perrygeo
Created January 11, 2013 17:10
Show Gist options
  • Save perrygeo/4512375 to your computer and use it in GitHub Desktop.
Save perrygeo/4512375 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,]]
"""
@gpedote
Copy link

gpedote commented Jun 23, 2017

Thanks, works perfectly 👍 👌

@omiqbaltawakkal
Copy link

Hi! i'm copying this code and its working perfectly at example, but in my real code, sometimes it outputting NaN, because my UB (or high) and LB (or low) goes 1.0 and 0.0. Does this code work if i set for all array (not by column like yours) ?

@Z223I
Copy link

Z223I commented Jul 26, 2020

Unfortunately, the formula is wrong. Try putting [100, 50, 0] into a column. You should get it back exactly the same, i.e., [100, 50, 0]. You will find the formula you want at Feature Scaling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment