Fill a particular value in the place of number satisfying certain condition by another number in an array.
Dear sir, How can we fill a particular value in the place of number satisfying certain condition by another number in an array. Example: A=[[[ 9.42233087e-42 - 4.71116544e-42 0.00000000e+00 ..., 1.48303127e+01 1.31524124e+01 1.14745111e+01] [ 3.91788793e+00 1.95894396e+00 0.00000000e+00 ..., 1.78252487e+01 1.28667984e+01 7.90834856e+00] [ 7.83592510e+00 -3.91796255e+00 0.00000000e+00 ..., 2.08202991e+01 1.25811749e+01 4.34205008e+00] ..., [ -8.51249974e-03 7.00901222e+00 -1.40095119e+01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 4.26390441e-03 3.51080871e+00 -7.01735353e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]] [[ 9.42233087e-42 -4.71116544e-42 0.00000000e+00 ..., 8.48242474e+00 7.97146845e+00 7.46051216e+00] [ 5.16325808e+00 2.58162904e+00 0.00000000e+00 ..., 8.47719383e+00 8.28024673e+00 8.08330059e+00] [ 1.03267126e+01 5.16335630e+00 0.00000000e+00 ..., 8.47196198e+00 8.58903694e+00 8.70611191e+00] ..., [ 0.00000000e+00 2.74500012e-01 5.49000025e-01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 1.37496844e-01 -2.74993688e-01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]] [[ 9.42233087e-42 4.71116544e-42 0.00000000e+00 ..., 1.18437748e+01 9.72778034e+00 7.61178637e+00] [ 2.96431869e-01 1.48215935e-01 0.00000000e+00 ..., 1.64031239e+01 1.32768812e+01 1.01506386e+01] [ 5.92875004e-01 2.96437502e-01 0.00000000e+00 ..., 2.09626484e+01 1.68261185e+01 1.26895866e+01] ..., [ 1.78188753e+00 -8.90943766e-01 0.00000000e+00 ..., 0.00000000e+00 1.27500005e-03 2.55000009e-03] [ 9.34620261e-01 -4.67310131e-01 0.00000000e+00 ..., 0.00000000e+00 6.38646539e-04 1.27729308e-03] [ 8.43000039e-02 4.21500020e-02 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]] A contain some negative value i want to change the negative numbers to '0'. I used 'masked_where', command but I failed. Please help me -- DILEEPKUMAR. R J R F, IIT DELHI
Dear Dileep, the numpy.where function returns the elements from A or 0 depending if the condition in the first argument is satisfied: B = np.where(A >= 0, A, 0) Miguel On Mon, Aug 01, 2011 at 03:01:13PM +0530, dileep kunjaai wrote:
Dear sir, How can we fill a particular value in the place of number satisfying certain condition by another number in an array.
Example: A=[[[ 9.42233087e-42 - 4.71116544e-42 0.00000000e+00 ..., 1.48303127e+01 1.31524124e+01 1.14745111e+01] [ 3.91788793e+00 1.95894396e+00 0.00000000e+00 ..., 1.78252487e+01 1.28667984e+01 7.90834856e+00] [ 7.83592510e+00 -3.91796255e+00 0.00000000e+00 ..., 2.08202991e+01 1.25811749e+01 4.34205008e+00] ..., [ -8.51249974e-03 7.00901222e+00 -1.40095119e+01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 4.26390441e-03 3.51080871e+00 -7.01735353e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
[[ 9.42233087e-42 -4.71116544e-42 0.00000000e+00 ..., 8.48242474e+00 7.97146845e+00 7.46051216e+00] [ 5.16325808e+00 2.58162904e+00 0.00000000e+00 ..., 8.47719383e+00 8.28024673e+00 8.08330059e+00] [ 1.03267126e+01 5.16335630e+00 0.00000000e+00 ..., 8.47196198e+00 8.58903694e+00 8.70611191e+00] ..., [ 0.00000000e+00 2.74500012e-01 5.49000025e-01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 1.37496844e-01 -2.74993688e-01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
[[ 9.42233087e-42 4.71116544e-42 0.00000000e+00 ..., 1.18437748e+01 9.72778034e+00 7.61178637e+00] [ 2.96431869e-01 1.48215935e-01 0.00000000e+00 ..., 1.64031239e+01 1.32768812e+01 1.01506386e+01] [ 5.92875004e-01 2.96437502e-01 0.00000000e+00 ..., 2.09626484e+01 1.68261185e+01 1.26895866e+01] ..., [ 1.78188753e+00 -8.90943766e-01 0.00000000e+00 ..., 0.00000000e+00 1.27500005e-03 2.55000009e-03] [ 9.34620261e-01 -4.67310131e-01 0.00000000e+00 ..., 0.00000000e+00 6.38646539e-04 1.27729308e-03] [ 8.43000039e-02 4.21500020e-02 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]] A contain some negative value i want to change the negative numbers to '0'. I used 'masked_where', command but I failed.
Please help me
-- DILEEPKUMAR. R J R F, IIT DELHI
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Le lundi 01 août 2011 à 15:01 +0530, dileep kunjaai a écrit :
Dear sir, How can we fill a particular value in the place of number satisfying certain condition by another number in an array. A contain some negative value i want to change the negative numbers to '0'. I used 'masked_where', command but I failed.
Does np.clip fulfill your requirements ? http://docs.scipy.org/doc/numpy/reference/generated/numpy.clip.html Be aware that it needs an upper limit (which can be np.inf). Another option A[A<0] = 0. -- Fabrice Silva
Depends where it is contained but another option is and I find it to typically be faster: B = zeros(A.shape) maximum(A,B,A) On 08/01/2011 07:31 PM, dileep kunjaai wrote:
Dear sir, How can we fill a particular value in the place of number satisfying certain condition by another number in an array.
Example: A=[[[ 9.42233087e-42 - 4.71116544e-42 0.00000000e+00 ..., 1.48303127e+01 1.31524124e+01 1.14745111e+01] [ 3.91788793e+00 1.95894396e+00 0.00000000e+00 ..., 1.78252487e+01 1.28667984e+01 7.90834856e+00] [ 7.83592510e+00 -3.91796255e+00 0.00000000e+00 ..., 2.08202991e+01 1.25811749e+01 4.34205008e+00] ..., [ -8.51249974e-03 7.00901222e+00 -1.40095119e+01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 4.26390441e-03 3.51080871e+00 -7.01735353e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
[[ 9.42233087e-42 -4.71116544e-42 0.00000000e+00 ..., 8.48242474e+00 7.97146845e+00 7.46051216e+00] [ 5.16325808e+00 2.58162904e+00 0.00000000e+00 ..., 8.47719383e+00 8.28024673e+00 8.08330059e+00] [ 1.03267126e+01 5.16335630e+00 0.00000000e+00 ..., 8.47196198e+00 8.58903694e+00 8.70611191e+00] ..., [ 0.00000000e+00 2.74500012e-01 5.49000025e-01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 1.37496844e-01 -2.74993688e-01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
[[ 9.42233087e-42 4.71116544e-42 0.00000000e+00 ..., 1.18437748e+01 9.72778034e+00 7.61178637e+00] [ 2.96431869e-01 1.48215935e-01 0.00000000e+00 ..., 1.64031239e+01 1.32768812e+01 1.01506386e+01] [ 5.92875004e-01 2.96437502e-01 0.00000000e+00 ..., 2.09626484e+01 1.68261185e+01 1.26895866e+01] ..., [ 1.78188753e+00 -8.90943766e-01 0.00000000e+00 ..., 0.00000000e+00 1.27500005e-03 2.55000009e-03] [ 9.34620261e-01 -4.67310131e-01 0.00000000e+00 ..., 0.00000000e+00 6.38646539e-04 1.27729308e-03] [ 8.43000039e-02 4.21500020e-02 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]] A contain some negative value i want to change the negative numbers to '0'. I used 'masked_where', command but I failed.
Please help me
-- DILEEPKUMAR. R J R F, IIT DELHI
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Hi
On Mon, Aug 1, 2011 at 3:14 PM, Jeffrey Spencer
Depends where it is contained but another option is and I find it to typically be faster:
B = zeros(A.shape) maximum(A,B,A)
Since maximum(.) can handle broadcasting maximum(A, 0, A) will be even faster. -eat
On 08/01/2011 07:31 PM, dileep kunjaai wrote:
Dear sir, How can we fill a particular value in the place of number satisfying certain condition by another number in an array.
Example: A=[[[ 9.42233087e-42 - 4.71116544e-42 0.00000000e+00 ..., 1.48303127e+01 1.31524124e+01 1.14745111e+01] [ 3.91788793e+00 1.95894396e+00 0.00000000e+00 ..., 1.78252487e+01 1.28667984e+01 7.90834856e+00] [ 7.83592510e+00 -3.91796255e+00 0.00000000e+00 ..., 2.08202991e+01 1.25811749e+01 4.34205008e+00] ..., [ -8.51249974e-03 7.00901222e+00 -1.40095119e+01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 4.26390441e-03 3.51080871e+00 -7.01735353e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
[[ 9.42233087e-42 -4.71116544e-42 0.00000000e+00 ..., 8.48242474e+00 7.97146845e+00 7.46051216e+00] [ 5.16325808e+00 2.58162904e+00 0.00000000e+00 ..., 8.47719383e+00 8.28024673e+00 8.08330059e+00] [ 1.03267126e+01 5.16335630e+00 0.00000000e+00 ..., 8.47196198e+00 8.58903694e+00 8.70611191e+00] ..., [ 0.00000000e+00 2.74500012e-01 5.49000025e-01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 1.37496844e-01 -2.74993688e-01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
[[ 9.42233087e-42 4.71116544e-42 0.00000000e+00 ..., 1.18437748e+01 9.72778034e+00 7.61178637e+00] [ 2.96431869e-01 1.48215935e-01 0.00000000e+00 ..., 1.64031239e+01 1.32768812e+01 1.01506386e+01] [ 5.92875004e-01 2.96437502e-01 0.00000000e+00 ..., 2.09626484e+01 1.68261185e+01 1.26895866e+01] ..., [ 1.78188753e+00 -8.90943766e-01 0.00000000e+00 ..., 0.00000000e+00 1.27500005e-03 2.55000009e-03] [ 9.34620261e-01 -4.67310131e-01 0.00000000e+00 ..., 0.00000000e+00 6.38646539e-04 1.27729308e-03] [ 8.43000039e-02 4.21500020e-02 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]] A contain some negative value i want to change the negative numbers to '0'. I used 'masked_where', command but I failed.
Please help me
-- DILEEPKUMAR. R J R F, IIT DELHI
_______________________________________________ NumPy-Discussion mailing listNumPy-Discussion@scipy.orghttp://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
This method is probably simpler:
In [1]: import numpy as N
In [2]: A = N.random.random_integers(-10, 10, 25).reshape((5, 5))
In [3]: A
Out[3]:
array([[ -5, 9, 1, 9, -2],
[ -8, 0, 9, 7, -10],
[ 2, -3, -1, 5, -7],
[ 0, -2, -2, 9, 1],
[ -7, -9, -4, -1, 6]])
In [4]: A[A < 0] = 0
In [5]: A
Out[5]:
array([[0, 9, 1, 9, 0],
[0, 0, 9, 7, 0],
[2, 0, 0, 5, 0],
[0, 0, 0, 9, 1],
[0, 0, 0, 0, 6]])
~Brett
On Mon, Aug 1, 2011 at 4:31 AM, dileep kunjaai
Dear sir, How can we fill a particular value in the place of number satisfying certain condition by another number in an array.
Example: A=[[[ 9.42233087e-42 - 4.71116544e-42 0.00000000e+00 ..., 1.48303127e+01 1.31524124e+01 1.14745111e+01] [ 3.91788793e+00 1.95894396e+00 0.00000000e+00 ..., 1.78252487e+01 1.28667984e+01 7.90834856e+00] [ 7.83592510e+00 -3.91796255e+00 0.00000000e+00 ..., 2.08202991e+01 1.25811749e+01 4.34205008e+00] ..., [ -8.51249974e-03 7.00901222e+00 -1.40095119e+01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 4.26390441e-03 3.51080871e+00 -7.01735353e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
[[ 9.42233087e-42 -4.71116544e-42 0.00000000e+00 ..., 8.48242474e+00 7.97146845e+00 7.46051216e+00] [ 5.16325808e+00 2.58162904e+00 0.00000000e+00 ..., 8.47719383e+00 8.28024673e+00 8.08330059e+00] [ 1.03267126e+01 5.16335630e+00 0.00000000e+00 ..., 8.47196198e+00 8.58903694e+00 8.70611191e+00] ..., [ 0.00000000e+00 2.74500012e-01 5.49000025e-01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 1.37496844e-01 -2.74993688e-01 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00] [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
[[ 9.42233087e-42 4.71116544e-42 0.00000000e+00 ..., 1.18437748e+01 9.72778034e+00 7.61178637e+00] [ 2.96431869e-01 1.48215935e-01 0.00000000e+00 ..., 1.64031239e+01 1.32768812e+01 1.01506386e+01] [ 5.92875004e-01 2.96437502e-01 0.00000000e+00 ..., 2.09626484e+01 1.68261185e+01 1.26895866e+01] ..., [ 1.78188753e+00 -8.90943766e-01 0.00000000e+00 ..., 0.00000000e+00 1.27500005e-03 2.55000009e-03] [ 9.34620261e-01 -4.67310131e-01 0.00000000e+00 ..., 0.00000000e+00 6.38646539e-04 1.27729308e-03] [ 8.43000039e-02 4.21500020e-02 0.00000000e+00 ..., 0.00000000e+00 0.00000000e+00 0.00000000e+00]]] A contain some negative value i want to change the negative numbers to '0'. I used 'masked_where', command but I failed.
Please help me
-- DILEEPKUMAR. R J R F, IIT DELHI
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (6)
-
Brett Olsen
-
dileep kunjaai
-
eat
-
Fabrice Silva
-
Jeffrey Spencer
-
Miguel de Val-Borro