-1

I want to create a matrix using the following arrays:

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]

Each value in A need to be mapped to all values in be once (each element in A will be attached to all four values in b) and from there, I want the resulting 12,2 matrix to become a 12,3 matrix by adding array C

It should look like the following:

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

My first idea is to use a couple for loops, but I'm having a lot of trouble implementing the values in the 1st column

In my actual code the values will be assign as follows:

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 will be non random

B = [2000,2500,2600,2700]
  • where is it random? if it has fixed number in input? you should use random(https://docs.python.org/2/library/random.html) – Destrif Jul 01 '16 at 15:27
  • For the first part you can use itertools.product, but I see nothing random here, so it's hard to be sure that's what you need. – polku Jul 01 '16 at 15:30
  • I just gave some example arrays, but when I make them in my code there I use random.randint() to make the values in A , B, C – user6538795 Jul 01 '16 at 15:32
  • I'll make the edits to show what I'll be using – user6538795 Jul 01 '16 at 15:32
  • You should show exactly your purpose. Why you need to create this type of matrix, precise example(shorter will be better). Example a= [1,2,3] b=[4,5,6] I want : [[1,2,3][4,5,6]] – Destrif Jul 01 '16 at 15:33
  • I just finished editing, let me know if this helps. Sorry for being unclear before, but thanks for the help! – user6538795 Jul 01 '16 at 15:43

2 Answers2

1

Have a look at :

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

But really, you should probably use 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]])

From:

Simple way to create matrix of random numbers

Community
  • 1
  • 1
Destrif
  • 2,104
  • 1
  • 14
  • 22
0

You may use the itertools package

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

which gives

[[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]]
desiato
  • 1,122
  • 1
  • 9
  • 16