Working with numpy arrays contains strings with different sizes

I have two lists that look like this:

a= [[1,2,3,4], [2,3,4,5],[3,4,5,6,7]], b= [[5,6,7,8], [9,1,2,3], [4,5,6,7,8]] 

which I want to subtract from the element for the element to output as follows:

 ab= [[-4,-4,-4,-4],[7,2,2,2],[-1,-1,-1,-1,-1]] 

To do this, I convert each of a and b into arrays and subtract them:

 np.array(a)-np.array(b) 

The result just gives me an error:

Unsupported operand type for-: 'list' and 'list'

What am I doing wrong? Should the np.array command provide conversion to an array?

+8
python arrays numpy
source share
7 answers

You can try:

 >>> a= [[1,2,3,4], [2,3,4,5],[3,4,5,6,7]] >>> b= [[5,6,7,8], [9,1,2,3], [4,5,6,7,8]] >>> >>> c =[] >>> for i in range(len(a)): c.append([A - B for A, B in zip(a[i], b[i])]) >>> print c [[-4, -4, -4, -4], [-7, 2, 2, 2], [-1, -1, -1, -1, -1]] 

Or the second method uses map :

 from operator import sub a= [[1,2,3,4], [2,3,4,5],[3,4,5,6,7]] b= [[5,6,7,8], [9,1,2,3], [4,5,6,7,8]] c =[] for i in range(len(a)): c.append(map(sub, a[i], b[i])) print c [[-4, -4, -4, -4], [-7, 2, 2, 2], [-1, -1, -1, -1, -1]] 
+1
source share

Here is the figure:

 >>> y = map(len, a) >>> a = np.hstack(np.array(a)) >>> b = np.hstack(np.array(b)) >>> np.split(ab, np.cumsum(y)) [array([-4, -4, -4, -4]), array([-7, 2, 2, 2]), array([-1, -1, -1, -1, -1]), array([], dtype=float64)] >>> 

Since you cannot subtract arrays with different shapes, you can flatten your arrays using np.hstack() and then subtract your flattened arrays and then modify based on the previous form.

+5
source share

The sizes of your two arrays do not match, i.e. the first two sublists of a have 4 elements, and the third has 5 and the same with b . If you convert lists to numpy arrays , numpy quietly gives you something like this:

 In [346]: aa = np.array(a) In [347]: bb = np.array(b) In [348]: aa Out[348]: array([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6, 7]], dtype=object) In [349]: bb Out[349]: array([[5, 6, 7, 8], [9, 1, 2, 3], [4, 5, 6, 7, 8]], dtype=object) 

You need to make sure that all of your sublisters have the same number of elements, then your code will work:

 In [350]: a = [[1,2,3,4], [2,3,4,5],[3,4,5,6]]; b = [[5,6,7,8], [9,1,2,3], [4,5,6,7]] # I removed the last element of third sublist in a and b In [351]: np.array(a) - np.array(b) Out[351]: array([[-4, -4, -4, -4], [-7, 2, 2, 2], [-1, -1, -1, -1]]) 
+1
source share

Without NumPy:

 result = [] for (m, n) in (zip(a, b)): result.append([i - j for i, j in zip(m, n)]) 

See also this question and this .

0
source share

What about a custom function, for example:

 import numpy as np a = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6, 7]] b = [[5, 6, 7, 8], [9, 1, 2, 3], [4, 5, 6, 7, 8]] def np_substract(l1, l2): return np.array([np.array(l1[i]) - np.array(l2[i]) for i in range(len(l1))]) print np_substract(a, b) 
0
source share

You get an error because your code is trying to subtract a subscription from subscriptions, if you want to make it work, you can do the same as follows:

 import numpy as np a= [[1,2,3,4], [2,3,4,5],[3,4,5,6,7]] b= [[5,6,7,8], [9,1,2,3], [4,5,6,7,8]] #You can apply different condition here, like (if (len(a) == len(b)), then only run the following code for each in range(len(a)): list = np.array(a[each])-np.array(b[each]) #for converting the output array in to list subList[each] = list.tolist() print subList 
0
source share

The nested list comprehension will do the job:

 In [102]: [[i2-j2 for i2,j2 in zip(i1,j1)] for i1,j1 in zip(a,b)] Out[102]: [[-4, -4, -4, -4], [-7, 2, 2, 2], [-1, -1, -1, -1, -1]] 

The problem with np.array(a)-np.array(b) is that the sublists differ in length, so the resulting arrays are object types - list arrays

 In [104]: np.array(a) Out[104]: array([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6, 7]], dtype=object) 

Subtracting an iteration over an external array is very simple, but the problem is when subtracting one subscription from another is therefore an error message.

If I made input arrays of arrays, subtraction will work

 In [106]: np.array([np.array(a1) for a1 in a]) Out[106]: array([array([1, 2, 3, 4]), array([2, 3, 4, 5]), array([3, 4, 5, 6, 7])], dtype=object) In [107]: aa=np.array([np.array(a1) for a1 in a]) In [108]: bb=np.array([np.array(a1) for a1 in b]) In [109]: aa-bb Out[109]: array([array([-4, -4, -4, -4]), array([-7, 2, 2, 2]), array([-1, -1, -1, -1, -1])], dtype=object) 

You cannot count on array operations that work with arrays of dtype objects. But in this case, subtraction is defined for subarrays, so it can handle nesting.

Another way to create nesting is to use np.subtract . This is the ufunc version - and will apply np.asarray to its inputs if necessary:

 In [103]: [np.subtract(i1,j1) for i1,j1 in zip(a,b)] Out[103]: [array([-4, -4, -4, -4]), array([-7, 2, 2, 2]), array([-1, -1, -1, -1, -1])] 

Note that these array calculations return arrays or a list of arrays. Returning internal arrays to lists requires iteration.

If you start with lists, converting to arrays often does not save time. Array calculation can be faster, but this does not compensate for the overhead of creating arrays in the first place.

If I insert inputs at an equal length, then a simple subtraction of the array is performed, creating a 2d array.

 In [116]: ao= [[1,2,3,4,0], [2,3,4,5,0],[3,4,5,6,7]]; bo= [[5,6,7,8,0], [9,1,2,3,0], [4,5,6,7,8]] In [117]: np.array(ao)-np.array(bo) Out[117]: array([[-4, -4, -4, -4, 0], [-7, 2, 2, 2, 0], [-1, -1, -1, -1, -1]]) 
0
source share

All Articles