Normalize The Rows Of Numpy Array Based On A Custom Function
I have an numpy array. I want to normalized each rows based on this formula
x_norm = (x-x_min)/(x_max-x_min)
x_min is the minimum of each row and
x_max is the maximum of each row. Here is a simple example:
a = np.array( [[0, 1 ,2], [2, 4 ,7], [6, 10,5] ])
and desired output:
a = np.array([ [0, 0.5 ,1], [0, 0.4 ,1], [0.2, 1 ,0] ])
IIUC, you can use raw numpy operations:
x = np.array( [[0, 1 ,2], [2, 4 ,7], [6, 10,5] ]) x_norm = ((x.T-x.min(1))/(x.max(1)-x.min(1))).T # OR x_norm = (x-x.min(1)[:,None])/(x.max(1)-x.min(1))[:,None]
array([[0. , 0.5, 1. ], [0. , 0.4, 1. ], [0.2, 1. , 0. ]])
NB. if efficiency matters, save the result of
x.min(1) in a variable as it is used twice
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module