I have the following matrix:
我有以下矩阵:
a = array([
[100. , 100., 100.],
[175.2, 198., 32.],
[ 38. , 82. , 38.],
[155. , 32. , 23.],
[ 38. , 67. , 30.]])
How do I change the numbers in all rows, but except the last row into zero if a number of a row is not equal to 38 and 32? What I want to get is as follows:
如何更改所有行中的数字,但如果一行数不等于38和32,则除最后一行外的数字为零?我想得到的是如下:
a = array([
[ 0 , 0 , 0 ],
[ 0 , 0 , 32.],
[38., 0 , 38.],
[ 0 , 32., 0 ],
[38., 67., 30.]])
And then I want to leave just the earliest number that appears in each column. Something like this:
然后我想留下每列中出现的最早的数字。像这样的东西:
a = array([
[ 0 , 0 , 0 ],
[ 0 , 0 , 32.],
[38., 0 , 0 ],
[ 0 , 32., 0 ],
[ 0 , 0 , 0 ]])
3 个解决方案
#1
2
This should do it:
这应该这样做:
positions = [(y, x) for x, y in enumerate(np.argmax(np.isin(a, [32,38]), axis=0))]
result = np.zeros(a.shape)
for p in positions:
result[p] = a[p]
#[[ 0 , 0 , 0 ],
# [ 0 , 0 , 32.],
# [38., 0 , 0 ],
# [ 0 , 32., 0 ],
# [ 0 , 0 , 0 ]]
#2
1
Here is a vectorised and in place solution.
这是一个矢量化和就地解决方案。
import numpy as np
a = np.array([[100. , 100., 100.],
[175.2, 198., 32.],
[ 38. , 82. , 38.],
[155. , 32. , 23.],
[ 38. , 67. , 30.]])
a[:-1][(a[:-1]!=38) & (a[:-1]!=32)] = 0
# array([[ 0., 0., 0.],
# [ 0., 0., 32.],
# [ 38., 0., 38.],
# [ 0., 32., 0.],
# [ 38., 67., 30.]])
You can also use numpy
for your final result:
您也可以使用numpy作为最终结果:
idx = ((a!=0).argmax(axis=0), np.array(range(a.shape[1])))
result = np.empty(a.shape)
result[idx] = a[idx]
# array([[ 0., 0., 0.],
# [ 0., 0., 32.],
# [ 38., 0., 0.],
# [ 0., 32., 0.],
# [ 0., 0., 0.]])
#3
0
And one more (albeit inefficient) solution:
还有一个(尽管效率低下)解决方案:
import numpy as np
a = np.array([
[100. , 100., 100.],
[175.2, 198., 32.],
[ 38. , 82. , 38.],
[155. , 32. , 23.],
[ 38. , 67. , 30.]])
def what(y,lastRow):
found = False
for o in y:
if o in [32,38] and not found and not lastRow:
found = True
yield o
else:
yield 0
# probably inefficient due to list comp instead of np-own ops
b = np.array([list(what(y,idy==len(a)-1)) for idy,y in enumerate(a)])
print(b)
Output:
[[ 0. 0. 0.]
[ 0. 0. 32.]
[ 38. 0. 0.]
[ 0. 32. 0.]
[ 0. 0. 0.]]
#1
2
This should do it:
这应该这样做:
positions = [(y, x) for x, y in enumerate(np.argmax(np.isin(a, [32,38]), axis=0))]
result = np.zeros(a.shape)
for p in positions:
result[p] = a[p]
#[[ 0 , 0 , 0 ],
# [ 0 , 0 , 32.],
# [38., 0 , 0 ],
# [ 0 , 32., 0 ],
# [ 0 , 0 , 0 ]]
#2
1
Here is a vectorised and in place solution.
这是一个矢量化和就地解决方案。
import numpy as np
a = np.array([[100. , 100., 100.],
[175.2, 198., 32.],
[ 38. , 82. , 38.],
[155. , 32. , 23.],
[ 38. , 67. , 30.]])
a[:-1][(a[:-1]!=38) & (a[:-1]!=32)] = 0
# array([[ 0., 0., 0.],
# [ 0., 0., 32.],
# [ 38., 0., 38.],
# [ 0., 32., 0.],
# [ 38., 67., 30.]])
You can also use numpy
for your final result:
您也可以使用numpy作为最终结果:
idx = ((a!=0).argmax(axis=0), np.array(range(a.shape[1])))
result = np.empty(a.shape)
result[idx] = a[idx]
# array([[ 0., 0., 0.],
# [ 0., 0., 32.],
# [ 38., 0., 0.],
# [ 0., 32., 0.],
# [ 0., 0., 0.]])
#3
0
And one more (albeit inefficient) solution:
还有一个(尽管效率低下)解决方案:
import numpy as np
a = np.array([
[100. , 100., 100.],
[175.2, 198., 32.],
[ 38. , 82. , 38.],
[155. , 32. , 23.],
[ 38. , 67. , 30.]])
def what(y,lastRow):
found = False
for o in y:
if o in [32,38] and not found and not lastRow:
found = True
yield o
else:
yield 0
# probably inefficient due to list comp instead of np-own ops
b = np.array([list(what(y,idy==len(a)-1)) for idy,y in enumerate(a)])
print(b)
Output:
[[ 0. 0. 0.]
[ 0. 0. 32.]
[ 38. 0. 0.]
[ 0. 32. 0.]
[ 0. 0. 0.]]