0

I have to scale between [0,1] a matrix. So, for each element from matrix i have to do this formula:

(Element - min_cols) / (max_cols - min_cols)

min_cols -> array with every minimum of each column from the matrix. max_cols -> same but with max

My problem is, i want to calculate result with this:

result = (Element- min_cols) / (max_cols - min_cols)

Or, from each element from the matrix i have to do difference between that element and the minimum from element's column, and do the difference between (maximum element's column and the minimum).*

but when i have for example the value from min_cols negative and the value from max_cols also negative, it results the sum between both.

I want to specify that the matrix is: _mat = np.random.randn(1000, 1000) * 50

  • Please show some code, input data and actual and expected output. – Dennis Williamson Nov 16 '22 at 16:45
  • the difference between them is the distance, so it doesn't matter if both are negative. having `max_cols = -2` and `min_cols = -5` will result in `max_cols - mixn_cols = 3`. Which is the distance between them. This is not a problem, Your are trying to normalize a matrix, so that's the formula – Sembei Norimaki Nov 16 '22 at 16:45
  • @DennisWilliamson i have a random matrix, and i want to make every element from matrix to be 0.xxxx (so between [0,1]) using that formula. – Lucian Schipor Nov 16 '22 at 16:50
  • Are you trying to normalize within the column? or within the entire matrix? – RagingRoosevelt Nov 16 '22 at 16:52
  • 1
    Does this answer your question? [Normalize numpy array columns in python](https://stackoverflow.com/questions/29661574/normalize-numpy-array-columns-in-python) – RagingRoosevelt Nov 16 '22 at 16:53
  • @RagingRoosevelt i am trying to normalize the entire matrix, based on 2 arrays, min_cols, wich contains the minimum from each column, and max_cols wich contains the maximum from each column – Lucian Schipor Nov 16 '22 at 16:54
  • How is that different than normalizing each column? Did you look at the accepted answer in that question I linked? because it sounds a lot like what you're describing. And you haven't provided any example matrices for people to ensure they're thinking about the same thing you are. – RagingRoosevelt Nov 16 '22 at 16:55

1 Answers1

0

Use numpy

Example

import numpy as np

x = 50*np.random.rand(6,4)

array([[26.7041017 , 46.88118463, 41.24541748, 31.17881807],
       [47.57036124, 16.49040094,  6.62454156, 37.15976348],
       [46.7157895 ,  8.53357717, 39.01399714,  5.14287858],
       [24.36012016,  5.67603151, 40.7697121 , 13.09877845],
       [21.69045322, 12.61989002,  8.74692768, 46.23368735],
       [ 3.9058066 , 35.50845507,  4.66785679,  2.34177134]])

Apply your formula

np.divide(np.subtract(x, x.min(axis=0)), x.max(axis=0)-x.min(axis=0))

array([[0.52212361, 1.        , 1.        , 0.65700132],
       [1.        , 0.26245187, 0.05349413, 0.79326663],
       [0.98042871, 0.06934923, 0.93899483, 0.06381829],
       [0.46844205, 0.        , 0.98699461, 0.24507946],
       [0.40730168, 0.16851918, 0.1115184 , 1.        ],
       [0.        , 0.7239974 , 0.        , 0.        ]])

The max value of each column is mapped to 1, the min value of each column is mapped to 0 an the intermediate values have are linearly mapped between 0 and 1

Sembei Norimaki
  • 745
  • 1
  • 4
  • 11
  • Although this is a good answer, when questions are duplicate, it is better to flag as a duplicate with a link to a question asking the same thing. – RagingRoosevelt Nov 16 '22 at 16:57
  • 1
    The linked answer normalizes the matrix (which is just dividing by the max value, not substracting the min value) – Sembei Norimaki Nov 16 '22 at 16:58