Python 创建随机数矩阵的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15451958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple way to create matrix of random numbers
提问by user2173836
I am trying to create a matrix of random numbers, but my solution is too long and looks ugly
我正在尝试创建一个随机数矩阵,但我的解决方案太长而且看起来很丑
random_matrix = [[random.random() for e in range(2)] for e in range(3)]
this looks ok, but in my implementation it is
这看起来不错,但在我的实现中它是
weights_h = [[random.random() for e in range(len(inputs[0]))] for e in range(hiden_neurons)]
which is extremely unreadable and does not fit on one line.
这是非常不可读的,不适合一行。
采纳答案by root
Take a look at numpy.random.rand:
Docstring: rand(d0, d1, ..., dn)
Random values in a given shape.
Create an array of the given shape and propagate it with random samples from a uniform distribution over
[0, 1).
文档字符串: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 ]])
回答by Pavel Anossov
You can drop the range(len()):
您可以删除range(len()):
weights_h = [[random.random() for e in inputs[0]] for e in range(hiden_neurons)]
But really, you should probably use numpy.
但实际上,您可能应该使用 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]])
回答by GodMan
An answer using map-reduce:-
使用 map-reduce 的答案:-
map(lambda x: map(lambda y: ran(),range(len(inputs[0]))),range(hiden_neurons))
回答by PythonUser
random_matrix = [[random.random for j in range(collumns)] for i in range(rows)
for i in range(rows):
print random_matrix[i]
回答by Cartesian Theater
Looks like you are doing a Python implementation of the Coursera Machine Learning Neural Network exercise. Here's what I did for randInitializeWeights(L_in, L_out)
看起来您正在执行 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
回答by Rajat Subhra Bhowmick
x = np.int_(np.random.rand(10) * 10)
For random numbers out of 10. For out of 20 we have to multiply by 20.
对于 10 中的随机数。 对于 20 中的随机数,我们必须乘以 20。
回答by nk911
use np.random.randint()as numpy.random.random_integers()is deprecated
使用np.random.randint()的numpy.random.random_integers()是过时
random_matrix = numpy.random.randint(min_val,max_val,(<num_rows>,<num_cols>))
回答by Marquistador
When you say "a matrix of random numbers", you can use numpy as Pavel https://stackoverflow.com/a/15451997/6169225mentioned above, in this case I'm assuming to you it is irrelevant what distribution these (pseudo) random numbers adhere to.
当您说“随机数矩阵”时,您可以将 numpy 用作上面提到的Pavel https://stackoverflow.com/a/15451997/6169225,在这种情况下,我假设对您而言这些分布无关紧要(伪) 随机数坚持。
However, if you require a particular distribution (I imagine you are interested in the uniform distribution), numpy.randomhas very useful methods for you. For example, let's say you want a 3x2 matrix with a pseudo random uniform distribution bounded by [low,high]. You can do this like so:
但是,如果您需要特定的分布(我想您对均匀分布感兴趣),那么numpy.random有非常有用的方法。例如,假设您想要一个具有以 [low,high] 为界的伪随机均匀分布的 3x2 矩阵。你可以这样做:
numpy.random.uniform(low,high,(3,2))
Note, you can replace uniformby any number of distributions supported by this library.
请注意,您可以替换uniform为该库支持的任意数量的发行版。
Further reading: https://docs.scipy.org/doc/numpy/reference/routines.random.html
进一步阅读:https: //docs.scipy.org/doc/numpy/reference/routines.random.html
回答by Lokesh Sharma
First, create numpyarray then convert it into matrix. See the code below:
首先,创建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)
回答by Runner
A simple way of creating an array of random integers is:
创建随机整数数组的一种简单方法是:
matrix = np.random.randint(maxVal, size=(rows, columns))
The following outputs a 2 by 3 matrix of random integers from 0 to 10:
下面输出一个由 0 到 10 的随机整数组成的 2 x 3 矩阵:
a = np.random.randint(10, size=(2,3))

