how to scale an array between two values python

Solutions on MaxInterview for how to scale an array between two values python by the best coders in the world

showing results for - "how to scale an array between two values python"
Lou
02 Jul 2018
1import numpy as np
2
3a = np.random.rand(3,2)
4
5# Normalised [0,1]
6b = (a - np.min(a))/np.ptp(a)
7
8# Normalised [0,255] as integer: don't forget the parenthesis before astype(int)
9c = (255*(a - np.min(a))/np.ptp(a)).astype(int)        
10
11# Normalised [-1,1]
12d = 2.*(a - np.min(a))/np.ptp(a)-1