My code combines values from two matrices and lists them side by side. T works as I need properly.
We are trying to remove the field where 2 identical values are located. This can be better seen in the example below
my code
import os
import numpy as np
import sys
b=np.array([[13,14,15],
[22,23,24],
[31,32,33]])
#print(b)
d=np.array([100,200,300,400,500])
b[-1,:] = d[:b.shape[1]] # last row
b[:-1,-1] = d[b.shape[1]:]
val1 = np.hstack(b[::-1])
val2 = np.hstack([d[i:i+b.shape[1]] for i in range(b.shape[0])])
res = zip(val1, val2)
for i, j in res:
l=[i, j]
print(l)
my output
[100, 100]
[200, 200]
[300, 300]
[22, 200]
[23, 300]
[500, 400]
[13, 300]
[14, 400]
[400, 500]
My code combines values from two matrices and lists them side by side. T works as I need properly.
We are trying to remove the field where 2 identical values are located. This can be better seen in the example below
I would need to remove matrices in my output that contain the same numbers. As you can see in the output below
The matrices do not always have to be the same and do not have to match the same iterations
required output
[22, 200]
[23, 300]
[500, 400]
[13, 300]
[14, 400]
[400, 500]
Find where the values are different and only concatenate those values.
>>> # using val1 and val2 from the question
>>> mask = np.where(val1!=val2)
>>> mask
(array([3, 4, 5, 6, 7, 8], dtype=int64),)
>>> np.vstack((val1[mask],val2[mask]))
array([[ 22, 23, 500, 13, 14, 400],
[200, 300, 400, 300, 400, 500]])
>>> np.vstack((val1[mask],val2[mask])).T
array([[ 22, 200],
[ 23, 300],
[500, 400],
[ 13, 300],
[ 14, 400],
[400, 500]])
>>>
It is as simple as comparing the two arrays and using the result as a boolean index:
np.stack([val1, val2], axis=1)[val1 != val2]
Related
Looking for a way to add value 555 right after 6000 to make it and display it.
mylist = [4, 11, [300, 400, [5000, 6000, 555], 500], 30, 40]
Original list below.
mylist = [4, 11, [300, 400, [5000, 6000], 500], 30, 40]
Can you try adding append to corresponding index
mylist[2][2].append(555)
>>> aa = [10, 20, 30]
>>> aa[1:2] = 100, 200
[10, 100, 200, 30]
>>> aa = [10, 20, 30]
>>> aa[1:2] = [100, 200]
[10, 100, 200, 30]
>>> aa = [10, 20, 30]
>>> aa[1:2] = (100, 200)
[10, 100, 200, 30]
I'm a beginner to Python. I tried to change 20 into 100, 200, so I tried 3 ways of inserting these 2 numbers: ints, a list, and a tuple. Why is the result the same when I insert ints or a list or a tuple in aa[1:2]?
By using aa[1:2], you are modifying a list item using slice assignment. Slice assignment replaces the specified item (or items), in this case the second item in aa, with whatever new item(s) that is specified. I'll go through each type to clarify what is happening
Ints - aa[1:2] = 100, 200: this example is the most clear. We are replacing aa[1:2] with two ints that go into the list in that spot.
List/Tuple: this example of how slice assignment works -- instead of adding a list to the list, it extends the new list into the old list. The tuple works the same way. To replace the item and add a list or tuple, wrap the list or tuple in another list first:
>>> aa = [10, 20, 30]
>>> aa[1:2] = [[100, 200]]
[10, [100, 200], 30]
>>> aa = [10, 20, 30]
>>> aa[1:2] = [(100, 200)]
[10, (100, 200), 30]
I have a matrix M:
M = [[10, 1000],
[11, 200],
[15, 800],
[20, 5000],
[28, 100],
[32, 3000],
[35, 3500],
[38, 100],
[50, 5000],
[51, 100],
[55, 2000],
[58, 3000],
[66, 4000],
[90, 5000]]
And a matrix R:
[[10 20]
[32 35]
[50 66]
[90 90]]
I want to use the values in column 0 of matrix R as start value of a slice and the value in column 1 as end of a slice.
I want to calculate the sum between and including the ranges of these slices from the right column in matrix M.
Basically doing
M[0:4][:,1].sum() # Upper index +1 as I need upper bound including
M[5:7][:,1].sum() # Upper index +1 as I need upper bound including
and so on. 0 is the index of 10 and 3 is the index of 20. 5 would be the index of 32, 6 the index of 35.
I'm stuck at how to get the start/end values from matrix R into indeces by column 0 of matrix M. And then calculate the sum between the index range including upper/lower bound.
Expected output:
[[10, 20, 7000], # 7000 = 1000+200+800+5000
[32, 35, 6500], # 6500 = 3000+3500
[50, 66, 14100], # 14100 = 5000+100+2000+3000+4000
[90, 90, 5000]] # 5000 = just 5000 as upper=lower boundary
Update, I can get the indices now using searchsorted. Now I just need to use sum at column 1 of matrix M within the start and end.
start_indices = [0,5,8,13]
end_indices = [3,6,12,13]
Wondering if there is a more efficient way than applying a for loop?
EDIT: Found the answer here. Numpy sum of values in subarrays between pairs of indices
Use searchsorted to determine the correct indices and add.reduceat to perform the summation:
>>> idx = M[:, 0].searchsorted(R) + (0, 1)
>>> idx = idx.ravel()[:-1] if idx[-1, 1] == M.shape[0] else idx.ravel()
>>> result = np.add.reduceat(M[:, 1], idx)[::2]
>>> result
array([ 7000, 6500, 14100, 5000])
Details:
Since you want to include the upper boundaries but Python excludes them we have to add 1.
reduceat cannot handle len(arg0) as an index, we have to special case that
reduceat computes all stretches between consecutive boundaries, we have to discard every other one
I think it would be better to show an example of the output you are expecting. If what you want to calculate using M[0:4][:,1].sum() is the sum of 1000 + 200 + 800 + 5000. Then this code might help:
import numpy as np
M = np.matrix([[10, 1000],
[11, 200],
[15, 800],
[20, 5000],
[28, 100],
[32, 3000],
[35, 3500],
[38, 100],
[50, 5000],
[51, 100],
[55, 2000],
[58, 3000],
[66, 4000],
[90, 5000]])
print(M[0:4][:,1].sum())
I have two lists like this:
list_1 = [100,100,50,40,40,10,20]
list_2 = [5,25,50,120]
I want to take all the elements from list_2 and add it to the end of list_1, also making it a nested list. The output should be like this:
[[100,100,50,40,40,10,20,5],[100,100,50,40,40,10,20,25],[100,100,50,40,40,10,20,50],[100,100,50,40,40,10,20,5,120]]
Is there any possible way of doing this in Python3?
just create a list of lists with list_1 added to a single element list made of each element of list_2:
list_1 = [100,100,50,40,40,10,20]
list_2 = [5,25,50,120]
list_3 = [list_1+[x] for x in list_2]
print(list_3)
result:
[[100, 100, 50, 40, 40, 10, 20, 5], [100, 100, 50, 40, 40, 10, 20, 25], [100, 100, 50, 40, 40, 10, 20, 50], [100, 100, 50, 40, 40, 10, 20, 120]]
I have two arrays A and B in numpy. A holds cartesian coordinates, each row is one point in 3D space and has the shape (r, 3). B has the shape (r, n) and holds integers.
What I would like to do is multiply each element of B with each row in A, so that the resulting array has the shape (r, n, 3). So for example:
# r = 3
A = np.array([1,1,1, 2,2,2, 3,3,3]).reshape(3,3)
# n = 2
B = np.array([10, 20, 30, 40, 50, 60]).reshape(3,2)
# Result with shape (3, 2, 3):
# [[[10,10,10], [20,20,20]],
# [[60,60,60], [80,80,80]]
# [[150,150,150], [180,180,180]]]
I'm pretty sure this can be done with np.einsum, but I've been trying this for quite a while now and can't get it to work.
Use broadcasting -
A[:,None,:]*B[:,:,None]
Since np.einsum also supports broadcasting, you can use that as well (thanks to #ajcr for suggesting this concise version) -
np.einsum('ij,ik->ikj',A,B)
Sample run -
In [22]: A
Out[22]:
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
In [23]: B
Out[23]:
array([[10, 20],
[30, 40],
[50, 60]])
In [24]: A[:,None,:]*B[:,:,None]
Out[24]:
array([[[ 10, 10, 10],
[ 20, 20, 20]],
[[ 60, 60, 60],
[ 80, 80, 80]],
[[150, 150, 150],
[180, 180, 180]]])
In [25]: np.einsum('ijk,ij->ijk',A[:,None,:],B)
Out[25]:
array([[[ 10, 10, 10],
[ 20, 20, 20]],
[[ 60, 60, 60],
[ 80, 80, 80]],
[[150, 150, 150],
[180, 180, 180]]])