创建随机数矩阵的简单方法

问题描述 投票:0回答:13

我正在尝试创建一个随机数矩阵,但我的解决方案太长而且看起来很难看

random_matrix = [[random.random() for e in range(2)] for e in range(3)]

这看起来不错,但在我的实现中是这样的

weights_h = [[random.random() for e in range(len(inputs[0]))] for e in range(hiden_neurons)]

这是极其难以阅读的,并且无法放在一行中。

python random coding-style
13个回答
116
投票

您可以删除

range(len())

weights_h = [[random.random() for e in inputs[0]] for e in range(hiden_neurons)]

但实际上,你应该使用 numpy。

In [9]: numpy.random.random((3, 3))
Out[9]:
array([[ 0.37052381,  0.03463207,  0.10669077],
       [ 0.05862909,  0.8515325 ,  0.79809676],
       [ 0.43203632,  0.54633635,  0.09076408]])

106
投票

看看 numpy.random.rand

文档字符串:rand(d0, d1, ..., dn)

给定形状的随机值。

创建给定形状的数组并随机传播它 来自

[0, 1)
上均匀分布的样本。


>>> import numpy as np
>>> np.random.rand(2,3)
array([[ 0.22568268,  0.0053246 ,  0.41282024],
       [ 0.68824936,  0.68086462,  0.6854153 ]])

35
投票

使用

np.random.randint()
,因为
np.random.random_integers()
已弃用

random_matrix = np.random.randint(min_val,max_val,(<num_rows>,<num_cols>))

6
投票

看起来您正在使用 Python 实现 Coursera 机器学习神经网络练习。这是我为 randInitializeWeights(L_in, L_out) 所做的

#get a random array of floats between 0 and 1 as Pavel mentioned 
W = numpy.random.random((L_out, L_in +1))

#normalize so that it spans a range of twice epsilon
W = W * 2 * epsilon

#shift so that mean is at zero
W = W - epsilon

5
投票

为了创建随机数数组,NumPy 提供了使用以下方法创建数组的功能:

  1. 实数

  2. 整数

用于使用随机实数创建数组: 有2个选择

  1. random.rand(用于生成随机数的均匀分布)
  2. random.randn(用于生成随机数的正态分布)

随机.rand

import numpy as np 
arr = np.random.rand(row_size, column_size) 

随机.randn

import numpy as np 
arr = np.random.randn(row_size, column_size) 

用于使用随机整数创建数组:

import numpy as np
numpy.random.randint(low, high=None, size=None, dtype='l')

哪里

  • low = 从分布中提取的最低(有符号)整数
  • high(可选)= 如果提供,则为从分布中提取的最大(有符号)整数之上的一个
  • size(可选)=输出形状,即如果给定的形状是(m,n,k),则绘制 m * n * k 样本
  • dtype(可选)=结果所需的数据类型。

例如:

给定的示例将生成一个 0 到 4 之间的随机整数数组,其大小为 5*5,有 25 个整数

arr2 = np.random.randint(0,5,size = (5,5))

为了创建 5 x 5 矩阵,应将其修改为

arr2 = np.random.randint(0,5,size = (5,5)),将乘号*改为逗号,#

[[2 1 1 0 1][3 2 1 4 3][2 3 0 3 3][1 3 1 0 0][4 1 2 0 1]]

eg2:

给定的示例将生成一个 0 到 1 之间的随机整数数组,其大小为 1*10,并且有 10 个整数

arr3= np.random.randint(2, size = 10)

[0 0 0 0 1 1 0 0 1 1]


4
投票

首先,创建

numpy
数组,然后将其转换为
matrix
。请参阅下面的代码:

import numpy

B = numpy.random.random((3, 4)) #its ndArray
C = numpy.matrix(B)# it is matrix
print(type(B))
print(type(C)) 
print(C)

2
投票
x = np.int_(np.random.rand(10) * 10)

对于 10 个随机数。对于 20 个随机数,我们必须乘以 20。


2
投票

当你说“随机数矩阵”时,你可以使用 numpy 作为 Pavel https://stackoverflow.com/a/15451997/6169225 上面提到的,在这种情况下我假设你与什么分布无关这些(伪)随机数遵循。

但是,如果您需要特定的分布(我想您对均匀分布感兴趣),

numpy.random
为您提供了非常有用的方法。例如,假设您想要一个具有以 [low,high] 为界的伪随机均匀分布的 3x2 矩阵。你可以这样做:

numpy.random.uniform(low,high,(3,2))

注意,您可以将

uniform
替换为该库支持的任意数量的发行版。

进一步阅读:https://docs.scipy.org/doc/numpy/reference/routines.random.html


2
投票

创建随机整数数组的一个简单方法是:

matrix = np.random.randint(maxVal, size=(rows, columns))

以下输出由 0 到 10 之间的随机整数组成的 2 x 3 矩阵:

a = np.random.randint(10, size=(2,3))

1
投票
random_matrix = [[random.random for j in range(collumns)] for i in range(rows)
for i in range(rows):
    print random_matrix[i]

1
投票

使用map-reduce的答案:-

map(lambda x: map(lambda y: ran(),range(len(inputs[0]))),range(hiden_neurons))

0
投票

numpy.random.rand(row, column) 根据给定的指定 (m,n) 参数生成 0 到 1 之间的随机数。因此,用它创建一个 (m,n) 矩阵,并将该矩阵乘以范围限制,并将其与上限相加。

分析:如果产生 0,则仅保持下限,如果产生 1,则仅保持上限。换句话说,使用 rand numpy 生成限制,您可以生成所需的极端数字。

import numpy as np

high = 10
low = 5
m,n = 2,2

a = (high - low)*np.random.rand(m,n) + low

输出:

a = array([[5.91580065, 8.1117106 ],
          [6.30986984, 5.720437  ]])

0
投票
#this is a function for a square matrix so on the while loop rows does not have to be less than cols.
#you can make your own condition. But if you want your a square matrix, use this code.

import random
import numpy as np

def random_matrix(R, cols):    
        matrix = []    
        rows =  0    
        while  rows < cols:    
            N = random.sample(R, cols)    
            matrix.append(N)    
            rows = rows + 1    
            return np.array(matrix)    
print(random_matrix(range(10), 5))
#make sure you understand the function random.sample
© www.soinside.com 2019 - 2024. All rights reserved.