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

use*_*836 74 python random coding-style

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

random_matrix = [[random.random() for e in range(2)] for e in range(3)]
Run Code Online (Sandbox Code Playgroud)

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

weights_h = [[random.random() for e in range(len(inputs[0]))] for e in range(hiden_neurons)]
Run Code Online (Sandbox Code Playgroud)

这是非常难以理解的,不适合一条线.

Pav*_*sov 89

你可以放弃range(len()):

weights_h = [[random.random() for e in inputs[0]] for e in range(hiden_neurons)]
Run Code Online (Sandbox Code Playgroud)

但实际上,你应该使用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]])
Run Code Online (Sandbox Code Playgroud)

  • `numpy.random.random_integers(low,high,shape)`,例如`numpy.random.random_integers(0,100,(3,3))` (32认同)
  • `numpy.random.random_integers()` 已弃用。改用`numpy.random.randint()`。https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randint.html (3认同)

roo*_*oot 57

看看numpy.random.rand:

Docstring: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 ]])
Run Code Online (Sandbox Code Playgroud)

  • 对于随机*复值*数组:`np.random.rand(2,3) * 1j + np.random.rand(2,3)` (2认同)

小智 7

使用np.random.randint()numpy.random.random_integers()是过时

random_matrix = numpy.random.randint(min_val,max_val,(<num_rows>,<num_cols>))
Run Code Online (Sandbox Code Playgroud)


Car*_*ter 6

看起来您正在执行 Coursera 机器学习神经网络练习的 Python 实现。这是我为 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
Run Code Online (Sandbox Code Playgroud)


SUJ*_*NGH 5

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

  1. 实数

  2. 整数

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

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

随机.rand

import numpy as np 
arr = np.random.rand(row_size, column_size) 
Run Code Online (Sandbox Code Playgroud)

随机.randn

import numpy as np 
arr = np.random.randn(row_size, column_size) 
Run Code Online (Sandbox Code Playgroud)

使用随机整数创建数组

import numpy as np
numpy.random.randint(low, high=None, size=None, dtype='l')
Run Code Online (Sandbox Code Playgroud)

在哪里

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

例如:

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

arr2 = np.random.randint(0,5,size = (5,5))
Run Code Online (Sandbox Code Playgroud)

为了创建 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]]

例如2:

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

arr3= np.random.randint(2, size = 10)
Run Code Online (Sandbox Code Playgroud)

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