Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to scale an array between two values python

import numpy as np

a = np.random.rand(3,2)

# Normalised [0,1]
b = (a - np.min(a))/np.ptp(a)

# Normalised [0,255] as integer: don't forget the parenthesis before astype(int)
c = (255*(a - np.min(a))/np.ptp(a)).astype(int)        

# Normalised [-1,1]
d = 2.*(a - np.min(a))/np.ptp(a)-1
Comment

PREVIOUS NEXT
Code Example
Python :: python random number generator no duplicates 
Python :: group by month and day pandas 
Python :: check multiple keys in python dict 
Python :: big comments python 
Python :: array creation method in numpy 
Python :: while input is not empty python 
Python :: datetime column only extract date pandas 
Python :: compare multiple columns in pandas 
Python :: jupyter matplotlib 
Python :: swap 2 no in one line python 
Python :: python print every n loops 
Python :: self in python 
Python :: keras model save 
Python :: username python 
Python :: Python | Pandas DataFrame.where() 
Python :: add a tuple to a dictionary python 
Python :: how to set pandas dataframe as global 
Python :: composition in python 
Python :: python if true 
Python :: axios django post 
Python :: python working with files and dirs 
Python :: python decorator 
Python :: pil format multiline text 
Python :: raise_for_status() requests 
Python :: Javascript rendering html 
Python :: Disctionary to Array 
Python :: how to declare private attribute in python 
Python :: binary to decimal in python without inbuilt function 
Python :: python spawn process 
Python :: convert string to int dataframe column 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =