-1

我想使用以下数组创建一个矩阵:

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 中的每个值都需要映射到一次中的所有值(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]
4

2 回答 2

1

看一下 :

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]])

从:

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

于 2016-07-01T15:30:56.527 回答
0

您可以使用该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]]
于 2016-07-01T15:40:32.227 回答