aa.astype(int) truncates and doesn't round
just something I bumped into and wasn't aware of
aa array([ 1., 1., 1., 1., 1.]) aa.astype(int) array([0, 1, 0, 0, 0]) aa - 1 array([ -2.22044605e-16, 2.22044605e-16, -2.22044605e-16, -3.33066907e-16, -3.33066907e-16]) np.round(aa).astype(int) array([1, 1, 1, 1, 1])
Josef
On Thu, Jan 6, 2011 at 2:14 AM, <josef.pktd@gmail.com> wrote:
just something I bumped into and wasn't aware of
aa array([ 1., 1., 1., 1., 1.]) aa.astype(int) array([0, 1, 0, 0, 0]) aa - 1 array([ -2.22044605e-16, 2.22044605e-16, -2.22044605e-16, -3.33066907e-16, -3.33066907e-16]) np.round(aa).astype(int) array([1, 1, 1, 1, 1])
a = np.ones(100) a.astype(int) array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
My default numpy int is 64 bits. Try 32 bits:
a = np.ones(100, np.int32) a.astype(int)
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
On Thu, Jan 6, 2011 at 10:40 AM, Keith Goodman <kwgoodman@gmail.com> wrote:
On Thu, Jan 6, 2011 at 2:14 AM, <josef.pktd@gmail.com> wrote:
just something I bumped into and wasn't aware of
aa array([ 1., 1., 1., 1., 1.]) aa.astype(int) array([0, 1, 0, 0, 0]) aa - 1 array([ -2.22044605e-16, 2.22044605e-16, -2.22044605e-16, -3.33066907e-16, -3.33066907e-16]) np.round(aa).astype(int) array([1, 1, 1, 1, 1])
a = np.ones(100) a.astype(int) array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
My default numpy int is 64 bits. Try 32 bits:
a = np.ones(100, np.int32) a.astype(int)
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
the full exercise includes some calculations first, so we are not precisely at 1
d_ array([[ 1., 0., 0., 0., 0.], [ 1., -1., 0., 0., 0.], [ 1., 0., -1., 0., 0.], [ 1., 0., 0., -1., 0.], [ 1., 0., 0., 0., -1.]]) np.set_printoptions(precision=2) np.linalg.pinv(d_) array([[ 1.00e+00, -2.34e-16, -8.50e-17, -7.63e-17, -8.50e-17], [ 1.00e+00, -1.00e+00, -1.06e-16, 2.19e-16, -6.18e-17], [ 1.00e+00, -2.21e-16, -1.00e+00, -2.27e-16, 9.38e-18], [ 1.00e+00, -6.40e-17, -2.84e-17, -1.00e+00, -7.65e-17], [ 1.00e+00, -1.70e-16, -9.55e-17, -1.52e-17, -1.00e+00]]) np.linalg.pinv(d_).astype(int) array([[ 0, 0, 0, 0, 0], [ 0, -1, 0, 0, 0], [ 0, 0, -1, 0, 0], [ 0, 0, 0, -1, 0], [ 0, 0, 0, 0, -1]]) np.linalg.inv(d_).astype(int) array([[ 1, 0, 0, 0, 0], [ 1, -1, 0, 0, 0], [ 1, 0, -1, 0, 0], [ 1, 0, 0, -1, 0], [ 1, 0, 0, 0, -1]])
Josef
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Thu, Jan 6, 2011 at 09:40, Keith Goodman <kwgoodman@gmail.com> wrote:
On Thu, Jan 6, 2011 at 2:14 AM, <josef.pktd@gmail.com> wrote:
just something I bumped into and wasn't aware of
aa array([ 1., 1., 1., 1., 1.]) aa.astype(int) array([0, 1, 0, 0, 0]) aa - 1 array([ -2.22044605e-16, 2.22044605e-16, -2.22044605e-16, -3.33066907e-16, -3.33066907e-16]) np.round(aa).astype(int) array([1, 1, 1, 1, 1])
a = np.ones(100) a.astype(int) array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
My default numpy int is 64 bits. Try 32 bits:
a = np.ones(100, np.int32) a.astype(int)
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
He's not pointing out a bug. His array does not have 1s in them, but values very close to 1, some slightly above and some slightly below, such that numpy's default printing rounds them to 1. See the "aa - 1" line. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On Thu, Jan 6, 2011 at 04:14, <josef.pktd@gmail.com> wrote:
just something I bumped into and wasn't aware of
aa array([ 1., 1., 1., 1., 1.]) aa.astype(int) array([0, 1, 0, 0, 0]) aa - 1 array([ -2.22044605e-16, 2.22044605e-16, -2.22044605e-16, -3.33066907e-16, -3.33066907e-16]) np.round(aa).astype(int) array([1, 1, 1, 1, 1])
This is behavior inherited from C and matches Python's behavior. int(aa[0]) == 0. Similarly, inside the C code, (int)(1.0 - 2.22e-16) == 0. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (3)
-
josef.pktd@gmail.com -
Keith Goodman -
Robert Kern