规范范围为0和1的矩阵行 [英] Normalize rows of a matrix within range 0 and 1

查看:200
本文介绍了规范范围为0和1的矩阵行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一次归一化矩阵数据的所有行(范围为0和1),但是我不知道该怎么做. 例如,我要标准化每个"obs1","obs2","obs3".因此,将使用每个"obs1","obs2","obs3"的最小值,最大值和总和. 我的数据格式是

I am trying to normalize all rows of my matrix data at once within range 0 and 1. But I don't know how to do it.. For example, I want to normalize each "obs1", "obs2", "obs3". Thus, minimum, maximum, and sum of each "obs1", "obs2", "obs3" will be used. My data format is,

`mydata

             a        b        c         d         e
obs1   8.15609  11.5379  11.1401   8.95186   7.95722
obs2 339.89800 856.3470 691.3490 590.28600 543.67200
obs3   2.12776  46.4561 136.8860 118.09100 119.86400

`

此外,当我搜索执行此操作时,人们使用了"function()".什么时候/什么用?

Also, When I searched to perform this, people used "function()". When/for what does this used?

非常感谢您的提前帮助! :)

Thank you very much for your help in advance! :)

推荐答案

要对每一行进行规范化,可以使用apply,然后从每一列中减去最小值,然后除以最大值和最小值之间的差:

To normalize for each row, you can use apply and then subtract the minimum from each column and divide by the difference between maximum and minimum:

t(apply(mydata, 1, function(x)(x-min(x))/(max(x)-min(x))))

给您

              a         b         c         d         e
obs1 0.05553973 1.0000000 0.8889038 0.2777796 0.0000000
obs2 0.00000000 1.0000000 0.6805144 0.4848262 0.3945675
obs3 0.00000000 0.3289472 1.0000000 0.8605280 0.8736849

发生的事情是您应用了功能

What happens is that you apply the function

function(x){
   (x-min(x))/(max(x)-min(x))
}

到数据框的每一行.

这篇关于规范范围为0和1的矩阵行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆