使用剪辑的模糊错误,在哪里用零替换负数

时间:2013-04-15 23:40:57

标签: python numpy

我想用零替换多维数组中的负值。

我一直在这里尝试建议:How to transform negative elements to zero without a loop?

我得到“具有多个元素的数组的真值是模糊错误。”

这是数组(a):

[ [  77.23262142  111.08317492  105.03704946   29.28612695   29.11454024
   10.07763987  -28.43527526  -41.68250773  -60.18250773  -73.61424205
-71.9592605   -74.25815349  -72.67697268  -61.01276604]
[ 346.32487234  346.35439263  332.45955869  305.13299042  253.4706288
 254.19387603  174.91896828  162.73446644  111.50014909   76.9614037
34.36361773  -25.55151142  -47.37254463  -57.63638227  -63.009076
-78.0385963   -56.38361475]
[ 217.45402363  172.89867308  158.19756607  159.74184651  142.31195721
153.54258452  158.28612695  133.56841478  159.88391293  131.50383913
152.97800887   60.40789817   93.2104812    71.13852548    2.02782437
-20.78398375  -52.2692236   -70.02752618  -76.46110552  -79.26184353
-68.36516456]
[ 268.77690186  246.15144061  199.3211823   229.61269522  175.28797197
219.35992769  194.95771367  197.6809609   202.8008871   194.34332252
201.19018599  140.29719706  164.36177271  100.68280592   96.90789817
62.38944799   19.59424503  -20.86700958  -47.14745238  -69.58472176
-74.28767379  -78.07365165  -89.37254463  -92.21387305]
[ 223.63668046  220.66804577  238.79535204  217.27690186  198.98723396
197.60162511  164.90605315  146.49461404  153.33225241  142.98538894
130.63852548  179.9706288   149.22708636  162.03704946  169.48723396
136.3008871   110.43926348   57.75845168   31.57579485  -25.19173283
-24.65667748  -56.25077342  -74.39652987  -78.69173283  -73.14745238
-71.89468486]
[ 205.54442954  236.53335942  220.39682806  210.16989079  163.2307764
177.45955869  161.84516754  161.58501994  186.19941109  174.29535204
201.33225241  211.2805919   193.33040739  206.32671736  190.24369153
160.91158821  164.12007529  125.9411085   113.82487234   83.49461404
35.88760297   -3.9795557   -21.52014611  -42.49800589  -62.73232323
-92.21387305  -78.48509076  -92.409445  ]
[ 269.6606657   280.96509374  273.66620075  272.83040739  252.07579485
263.64406053  272.48723396  257.49645906  240.12561035  250.14406053
241.07579485  237.02597935  226.8507026   206.71417123  173.3507026
174.34332252  188.71601625  149.40974319  191.18834097  153.50937418
113.72155131  131.25291662   89.53889448   68.59609005   41.67727086
10.68280592  -26.89283984  -35.4389653   -53.76368855  -61.76553356
-50.72494316]]

我试过了:

b = np.clip(a,0, 5000)
b = a.clip(min=0)

提前致谢。

2 个答案:

答案 0 :(得分:1)

首先,如果您可以发布一个可以证明您的问题的实际示例,那么当您提出问题时,这确实很有帮助。没有它,我们就会猜测。

似乎你可能正在使用数组数组而不是多维数组。例如:

import numpy as np
data = np.arange(3)

# Make an array of arrays
arrayOfArrays = np.empty(4, dtype=object)
arrayOfArrays.fill(data)
print arrayOfArrays
# [[0 1 2] [0 1 2] [0 1 2] [0 1 2]]

# Make a 2d array
array2d = np.empty((4, 3), dtype=int)
array2d[:] = data
print array2d
# [[0 1 2]
#  [0 1 2]
#  [0 1 2]
#  [0 1 2]]

# You can clip an ndarray of any dimenssion
array2d.clip(1)

# But clipping an array of arrays gives the error you describe
arrayOfArrays.clip(1)
# ValueError                                
# Traceback (most recent call last) <module>()
#      17 
#      18 # This will fail
#
# ---> 19 arrayOfArrays.clip(1)
#
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

如果你实际上正在使用一个数组数组,一个带有dtype对象的数组,而不是尝试使用多维数组。 dtype对象的数组容易出现各种类型的问题,我一般都试图避免它们。你可以通过检查形状和dtype来判断你是否正在使用带有dtype对象的数组:

print array2d.dtype
print array2d.shape
# int32
# (4, 3)

print arrayOfArrays.dtype
print arrayOfArrays.shape
# object
# (4,)

当然,在这种情况下,您可以遍历外部数组并在每个内部数组上调用剪辑。

for i in range(len(arrayOfArrays)):
    arrayOfArrays[i].clip(1)

答案 1 :(得分:0)

尝试将负值设置为零:

a[a<0] = 0