多维空间中的随机单位向量 [英] random unit vector in multi-dimensional space

查看:170
本文介绍了多维空间中的随机单位向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种数据挖掘算法,我想从特征空间中的特定点选择一个随机方向.

I'm working on a data mining algorithm where i want to pick a random direction from a particular point in the feature space.

如果我从[-1,1]中为n个维中的每一个选择一个随机数,然后将向量归一化为1,我将在所有可能的方向上获得均匀分布吗?

If I pick a random number for each of the n dimensions from [-1,1] and then normalize the vector to a length of 1 will I get an even distribution across all possible directions?

由于计算机生成的随机数实际上不是随机的,因此我在这里仅是理论上的发言.

I'm speaking only theoretically here since computer generated random numbers are not actually random.

推荐答案

一个简单的技巧是从高斯分布中选择每个维,然后进行归一化:

One simple trick is to select each dimension from a gaussian distribution, then normalize:

from random import gauss

def make_rand_vector(dims):
    vec = [gauss(0, 1) for i in range(dims)]
    mag = sum(x**2 for x in vec) ** .5
    return [x/mag for x in vec]

例如,如果要使用7维随机向量,请选择7个随机值(从均值为0且标准偏差为1的高斯分布中).然后,使用勾股公式计算所得向量的大小(对每个值求平方,加上平方,并取结果的平方根).最后,将每个值除以幅度即可获得归一化的随机向量.

For example, if you want a 7-dimensional random vector, select 7 random values (from a Gaussian distribution with mean 0 and standard deviation 1). Then, compute the magnitude of the resulting vector using the Pythagorean formula (square each value, add the squares, and take the square root of the result). Finally, divide each value by the magnitude to obtain a normalized random vector.

如果维度数量很大,那么这样做的好处是始终可以立即工作,同时生成随机向量,直到发现一个大小恰好小于一个的向量,这将导致计算机仅悬挂十几个维度之所以如此,是因为他们中任何一个人晋级的可能性越来越小.

If your number of dimensions is large then this has the strong benefit of always working immediately, while generating random vectors until you find one which happens to have magnitude less than one will cause your computer to simply hang at more than a dozen dimensions or so, because the probability of any of them qualifying becomes vanishingly small.

这篇关于多维空间中的随机单位向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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