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):

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement