Skip to content
Advertisement

How to substract from each element in a matrix, with a condition

I have a matrix, and i don’t know the size, because the matrix was created from a dataframe. I have 2 arrays, min_cols and max_cols, first one is for each minimum from each column, and same with the max_cols.
I want to recalculate each element from the columns, with this formula:

Element[line][column] = Element – min_cols[column]/ (max_cols[column] – min_cols [column])

(Element – min_cols) means that we substract the value from array min_cols that is on element’s column position, from the element, and like that for each element on that column.

Tehnically i have to substract from each element, the minimum from element’s column. EX: I have my element on second column, i have to substract the minimum from second column, from my element. Element = Element – min_cols[1] (minimum from second position)

The problem is i want to use a numpy function, and i don’t know how to work with this.

Or, I have to scale a matrix, between range [0,1]

Advertisement

Answer

I think it’s more like this (numpy will do exactly what you imagine, provided the arrays’ dimensions match); I assumed it was the same min_col for all columns, but it will work too if min_col and max_col have the same shape as matrix, or if there one min value and one max value for each column (in which case min_col and max_col will be “horizontal” matrices):

matrix = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
min_col = np.array([[-1],[-1],[-1],[-1]])
max_col = np.array([[9],[99],[999],[9999]])

(matrix - min_col) / (max_col - min_col)

# array([[0.2   , 0.3   , 0.4   ],
#       [0.05  , 0.06  , 0.07  ],
#       [0.008 , 0.009 , 0.01  ],
#       [0.0011, 0.0012, 0.0013]])
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement