np.transpose() and np.reshape() combination gives different results in pure numpy and in numba
NickName:P. Camilleri Ask DateTime:2018-12-03T21:37:24

np.transpose() and np.reshape() combination gives different results in pure numpy and in numba

The following code produces different outputs:

import numpy as np

from numba import njit


@njit
def resh_numba(a):
    res = a.transpose(1, 0, 2)
    res = res.copy().reshape(2, 6)
    return res

x = np.arange(12).reshape(2, 2, 3)

print("numpy")
x_numpy = x.transpose(1, 0, 2).reshape(2, 6)
print(x_numpy)

print("numba:")
x_numba = resh_numba(x)
print(x_numba)

Output:

numpy
[[ 0  1  2  6  7  8]
 [ 3  4  5  9 10 11]]
numba:
[[ 0  4  8  2  6 10]
 [ 1  5  9  3  7 11]]

What is the reason for this? I'm suspecting some order='C' vs order='F' happening somewhere, but I expected both numpy and numba to use order='C' by default everywhere.

Copyright Notice:Content Author:「P. Camilleri」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53595093/np-transpose-and-np-reshape-combination-gives-different-results-in-pure-nump

More about “np.transpose() and np.reshape() combination gives different results in pure numpy and in numba” related questions

np.transpose() and np.reshape() combination gives different results in pure numpy and in numba

The following code produces different outputs: import numpy as np from numba import njit @njit def resh_numba(a): res = a.transpose(1, 0, 2) res = res.copy().reshape(2, 6) return res...

Show Detail

Numba on pure python VS Numpa on numpy-python

Using numba results in much faster programs than using pure python: https://www.ibm.com/developerworks/community/blogs/jfp/entry/

Show Detail

Can my numba code be faster than numpy

I am new to Numba and am trying to speed up some calculations that have proved too unwieldy for numpy. The example I've given below compares a function containing a subset of my calculations using a

Show Detail

Numba basic example slower than pure python

I'm trying to compare numba and pure python using the basic example and I'm getting odd results. This is the numba example: from numba import jit from numpy import arange from time import time # ...

Show Detail

numba gives error when reshaping numpy array

I am trying to optimize some code which has some loops and matrix operations. However, I am running into some errors. Please find the code and output below. Code: @njit def list_of_distance(d1):...

Show Detail

Why numba and numpy performs differently on inplace operations?

Functions decorated with numba njit which is an alias to jit(nopython=True) yields different result from numpy on inplace operations (simple @jit(nopython=False) also gives different results from n...

Show Detail

Numba and numpy array allocation: why is it so slow?

I recently played with Cython and Numba to accelerate small pieces of a python that does numerical simulation. At first, developing with numba seems easier. Yet, I found difficult to understand when

Show Detail

Where are the gains using numba coming from for pure numpy code?

I would like to understand where the gains are coming from when using Numba to accelerate pure numpy code in a for loop. Are there any profiling tools that allow you to look into jitted functions? ...

Show Detail

Numba code slower than pure python

I've been working on speeding up a resampling calculation for a particle filter. As python has many ways to speed it up, I though I'd try them all. Unfortunately, the numba version is incredibly sl...

Show Detail

Python code timings using numba

I am trying to do some timing comparisons using numba. What I don't understand in the following mwe.py is why I get different results from __future__ import print_function import numpy as np from...

Show Detail