Created
January 11, 2013 17:10
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,]] | |
""" |
Thank You! Is there a faster method using skicit-learn or any numpy normalize function to directly normalize 2D array to given range? If no, is this the fastest way to normalize?
Thanks, works perfectly ๐ ๐
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) ?
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
Thank you for the function!