0

I would like to build a matrix by adding the rows, starting from:

[ 5 20 31 32 36  3 10 25 27 40]
[ 3 10 25 27 40 18 19 20 40 41]
[18 19 20 40 41  6 22 26 29 48]
[ 6 22 26 29 48  8 11 23 27 35]

to get:

[[ 5 20 31 32 36  3 10 25 27 40],
[ 3 10 25 27 40 18 19 20 40 41],
[18 19 20 40 41  6 22 26 29 48],
[ 6 22 26 29 48  8 11 23 27 35]]

using this code:

#initialize the target matrix
arr = np.empty((0, 10), int)

for current_line in statistics.values:
    if len(previous_line) > 0:
        #build an array row of 10 elements by 2 array with lenght 5
        row = np.append(np.array(previous_line), np.array(current_line))

        #arr = np.append(arr, row, axis=0)   
        #fails with following Exception ValueError: all the input arrays must have same number of dimensions

    print(row)

    previous_line = current_line

print(row.shape) gives (10,) back. I tried using row.T but it fails as well. Have you any suggestion?

Thank you very much! Bye Fabio

1
  • What is previous_line? You create arr but don't use it. Same for row. Commented Dec 3, 2016 at 17:28

2 Answers 2

1

According to docs:

It must be of the correct shape (the same shape as arr, excluding axis). If axis is not specified, values can be any shape and will be flattened before use.

Try to just add square brackets:

arr = np.append(arr, [row], axis=0)

In this case, the number of dimensions for arr and [row] will be the same.

Sign up to request clarification or add additional context in comments.

Comments

0

Starting with a copy-n-paste of your start

In [418]: vstr = """[[5 20 31 32 36  3 10 25 27 40]
     ...: [3 10 25 27 40 18 19 20 40 41]
     ...: [18 19 20 40 41  6 22 26 29 48]
     ...: [6 22 26 29 48  8 11 23 27 35]]"""
In [420]: vstr=','.join(vstr.split())
In [421]: vstr
Out[421]: '[[5,20,31,....]]'
In [422]: vlist=json.loads(vstr)
In [423]: vlist
Out[423]: 
[[5, 20, 31, 32, 36, 3, 10, 25, 27, 40],
 [3, 10, 25, 27, 40, 18, 19, 20, 40, 41],
 [18, 19, 20, 40, 41, 6, 22, 26, 29, 48],
 [6, 22, 26, 29, 48, 8, 11, 23, 27, 35]]

I now have a list of lists.

The easiest way to make that into an array is just pass it to np.array

In [424]: arr=np.array(vlist)
In [425]: arr
Out[425]: 
array([[ 5, 20, 31, 32, 36,  3, 10, 25, 27, 40],
       [ 3, 10, 25, 27, 40, 18, 19, 20, 40, 41],
       [18, 19, 20, 40, 41,  6, 22, 26, 29, 48],
       [ 6, 22, 26, 29, 48,  8, 11, 23, 27, 35]])

If the source of those sublists is some process, do

alist=[]
for line in vlist:
    alist.append(line)
arr = np.array(alist)

Using array append is possible, but slower, and you have to be careful with dimensions. append is just concatenate that takes 2 arguments, and makes sure they are at least 1d. It's really meant for adding one value to end of an array.

Here's what you should be doing if using a row by row concatenation:

arr = np.empty((0,10),int)
for line in vlist:
    arr = np.concatenate([arr, np.atleast_2d(line)],axis=0)

arr = np.append(arr, np.atleast_2d(line), axis=0) also works, but obscures the essential details. So does arr = np.vstack((arr, line)). There are other ways of turning line into the required 2d array. [line] that @SphDev suggests is another.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.