(伪)随机生成的矩阵
(psuedo) Randomly generated Matrix
我想使用以下数组创建矩阵:
A = [1111,2222,3333]
B = [20,25,26,27]
C = [1.5,2.6,3.7,4.5,5.4,6.7,7.2,8.8,9.0,10.0,11.0,12.2]
A 中的每个值都需要映射到 be 中的所有值一次(A 中的每个元素将附加到 b 中的所有四个值)并且从那里,我希望生成的 12,2 矩阵成为 12 ,3矩阵加数组C
它应该如下所示:
1111 20 1.5
1111 25 2.6
1111 26 3.7
1111 27 4.5
2222 20 5.4
2222 25 6.7
2222 26 7.2
2222 27 8.8
...........
3333 27 12.2
我的第一个想法是使用几个 for 循环,但我在实施第一列中的值时遇到了很多麻烦
在我的实际代码中,值将分配如下:
A = [random.randint(1250,14180) for x in range(len(5))]
C = [round(random.uniform(1.0,4.0),1) for x in range(len(15))]
B 将是非随机的
B = [2000,2500,2600,2700]
看看:
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]])
发件人:
Simple way to create matrix of random numbers
您可以使用 itertools
包
import itertools
A = [1111,2222,3333]
B = [20,25,26,27]
C = [1.5,2.6,3.7,4.5,5.4,6.7,7.2,8.8,9.0,10.0,11.0,12.2]
# create the product of A and B, i.e., [(1111,20), (1111,25), ..., (3333,27)]
AB = itertools.product(A,B)
# zip with C
ABC = itertools.izip( AB, C )
# flatten sublists, since ABC is an iterator for [((1111,20),1.5), ..., ((3333,27),12,2)]
ABC = [ list(ab)+[c] for ab,c in ABC ]
print ABC
这给出了
[[1111, 20, 1.5],
[1111, 25, 2.6],
[1111, 26, 3.7],
[1111, 27, 4.5],
[2222, 20, 5.4],
[2222, 25, 6.7],
[2222, 26, 7.2],
[2222, 27, 8.8],
[3333, 20, 9.0],
[3333, 25, 10.0],
[3333, 26, 11.0],
[3333, 27, 12.2]]
我想使用以下数组创建矩阵:
A = [1111,2222,3333]
B = [20,25,26,27]
C = [1.5,2.6,3.7,4.5,5.4,6.7,7.2,8.8,9.0,10.0,11.0,12.2]
A 中的每个值都需要映射到 be 中的所有值一次(A 中的每个元素将附加到 b 中的所有四个值)并且从那里,我希望生成的 12,2 矩阵成为 12 ,3矩阵加数组C
它应该如下所示:
1111 20 1.5
1111 25 2.6
1111 26 3.7
1111 27 4.5
2222 20 5.4
2222 25 6.7
2222 26 7.2
2222 27 8.8
...........
3333 27 12.2
我的第一个想法是使用几个 for 循环,但我在实施第一列中的值时遇到了很多麻烦
在我的实际代码中,值将分配如下:
A = [random.randint(1250,14180) for x in range(len(5))]
C = [round(random.uniform(1.0,4.0),1) for x in range(len(15))]
B 将是非随机的
B = [2000,2500,2600,2700]
看看:
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]])
发件人:
Simple way to create matrix of random numbers
您可以使用 itertools
包
import itertools
A = [1111,2222,3333]
B = [20,25,26,27]
C = [1.5,2.6,3.7,4.5,5.4,6.7,7.2,8.8,9.0,10.0,11.0,12.2]
# create the product of A and B, i.e., [(1111,20), (1111,25), ..., (3333,27)]
AB = itertools.product(A,B)
# zip with C
ABC = itertools.izip( AB, C )
# flatten sublists, since ABC is an iterator for [((1111,20),1.5), ..., ((3333,27),12,2)]
ABC = [ list(ab)+[c] for ab,c in ABC ]
print ABC
这给出了
[[1111, 20, 1.5],
[1111, 25, 2.6],
[1111, 26, 3.7],
[1111, 27, 4.5],
[2222, 20, 5.4],
[2222, 25, 6.7],
[2222, 26, 7.2],
[2222, 27, 8.8],
[3333, 20, 9.0],
[3333, 25, 10.0],
[3333, 26, 11.0],
[3333, 27, 12.2]]