normalize by column

31 views
Skip to first unread message

Brent Pedersen

unread,
Mar 13, 2018, 11:28:48 AM3/13/18
to gonum-dev
What would be the equivalent of this numpy syntax which normalizes each column by its mean?

```Python
 m /= m.mean(axis=0) 
```

Here is the go I've come up with and I'm wondering if there's some utility that I'm missing either within gonum or in some user packages.

```Go
    r, c := m.Dims()
    col := make([]float64, r) 
    
    for j := 0; j < c; j++ {
        _ = mat.Col(col, j, m)
        mu := stat.Mean(col, nil)
        
        for i, v := range col {
            col[i] = v / mu
        }
        m.SetCol(j, col) 
    }         
```

thanks,
-Brent

Brendan Tracey

unread,
Mar 13, 2018, 11:37:57 AM3/13/18
to gonum-dev
You can replace the range over `col` with floats.Scale(1/mu, col), and (minor point) you don't need the underscore in mat.Col

Can I ask why that particular operation? I'm wondering if there's a higher-level way to make your code easier.

Brent Pedersen

unread,
Mar 13, 2018, 11:43:50 AM3/13/18
to gonum-dev


On Tuesday, 13 March 2018 09:37:57 UTC-6, Brendan Tracey wrote:
You can replace the range over `col` with floats.Scale(1/mu, col), and (minor point) you don't need the underscore in mat.Col

cheers
 

Can I ask why that particular operation? I'm wondering if there's a higher-level way to make your code easier.

Sure, I have a matrix with rows of samples and columns of genes. I need to normalize in both dimensions. Scaling by the mean was just an example.
I am actually using StdScore to get the z-score so I can't use floats.Scale, but can utilize that elsewhere.
Reply all
Reply to author
Forward
0 new messages