python - Numpy assignment behaviour -
b
array([-0.06106568, -0.10843541, -0.0694688 , 0.02464023, -0.03686665, -0.0582096 , -0.13476669, -0.08505708, 0.00391955, -0.12300518, -0.01183732, -0.05374973, -0.12300518, -0.05312849, 0.01963862, 0.00155719, -0.10843541, -0.08490177, -0.08505708, -0.02026149, -0.01777489, 0.01183732, -0.11575136, 0.04278603, -0.0694688 , -0.06106568, -0.08755022, -0.01660802, -0.06087603, -0.06582411]) = b a[a <= 0] = 0 a[a > 0] = 1
with code above replace elements in a, b changes... please explain me mistake?
by using expression a = b
copy reference b
. if want copy value, should rather walk through b
's items , copy values a
.
in numpy should use copy
function.
>>> import numpy >>> b = numpy.array([-0.06106568, -0.10843541, -0.0694688 , 0.02464023, -0.03686665, ... -0.0582096 , -0.13476669, -0.08505708, 0.00391955, -0.12300518, ... -0.01183732, -0.05374973, -0.12300518, -0.05312849, 0.01963862, ... 0.00155719, -0.10843541, -0.08490177, -0.08505708, -0.02026149, ... -0.01777489, 0.01183732, -0.11575136, 0.04278603, -0.0694688 , ... -0.06106568, -0.08755022, -0.01660802, -0.06087603, -0.06582411]) >>> = numpy.copy(b) >>> array([-0.06106568, -0.10843541, -0.0694688 , 0.02464023, -0.03686665, -0.0582096 , -0.13476669, -0.08505708, 0.00391955, -0.12300518, -0.01183732, -0.05374973, -0.12300518, -0.05312849, 0.01963862, 0.00155719, -0.10843541, -0.08490177, -0.08505708, -0.02026149, -0.01777489, 0.01183732, -0.11575136, 0.04278603, -0.0694688 , -0.06106568, -0.08755022, -0.01660802, -0.06087603, -0.06582411]) >>> a[a<= 0] = 0 >>> a[a> 0] = 1 >>> array([ 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 1., 1., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0.]) >>> b array([-0.06106568, -0.10843541, -0.0694688 , 0.02464023, -0.03686665, -0.0582096 , -0.13476669, -0.08505708, 0.00391955, -0.12300518, -0.01183732, -0.05374973, -0.12300518, -0.05312849, 0.01963862, 0.00155719, -0.10843541, -0.08490177, -0.08505708, -0.02026149, -0.01777489, 0.01183732, -0.11575136, 0.04278603, -0.0694688 , -0.06106568, -0.08755022, -0.01660802, -0.06087603, -0.06582411])
there native ways it, if work mathematics, using numpy recommended.
update
i don't have in mind non-numpy way cause entire compatibility case.
Comments
Post a Comment