Note that there are some explanatory texts on larger screens.

plurals
  1. POCython error message: Buffer has wrong number of dimensions (expected 1, got 2)
    primarykey
    data
    text
    <p>I'm trying to code the least squares estimator in Cython for learning purposes. I got this basic version working:</p> <pre><code>import cython import numpy as np from scipy.linalg import inv cimport numpy as np def ols_c(np.ndarray x, np.ndarray y): cdef int nrowx = x.shape[0] cdef int ncolx = x.shape[1] cdef np.ndarray beta = np.zeros([ncolx,1], dtype=float) cdef np.ndarray a1 = np.zeros([ncolx, ncolx], dtype=float) cdef np.ndarray a2 = np.zeros([ncolx, nrowx], dtype=float) a1 = inv(np.dot(x.T,x)) a2 = np.dot(a1,x.T) beta = np.dot(a2,y) return(beta) </code></pre> <p>which is slightly slower than this Numpy version:</p> <pre><code>import numpy as np from scipy.linalg import inv def ols(x,y): a1 = inv(np.dot(x.T,x)) a2 = np.dot(a1,x.T) beta = np.dot(a2,y) return(beta) </code></pre> <p>I guess this is likely due to inefficient array indexing. Following tutorials on the internet, I modified the basic Cython version like this:</p> <pre><code>import cython import numpy as np from scipy.linalg import inv cimport numpy as np DTYPE = np.float ctypedef np.float_t DTYPE_t def ols_c(np.ndarray[DTYPE_t, ndim=2] x, np.ndarray[DTYPE_t, ndim=1] y): cdef int nrowx = x.shape[0] cdef int ncolx = x.shape[1] cdef np.ndarray[DTYPE_t, ndim=1] beta = np.zeros([ncolx,1], dtype=float) cdef np.ndarray[DTYPE_t, ndim=2] a1 = np.zeros([ncolx, ncolx], dtype=float) cdef np.ndarray[DTYPE_t, ndim=2] a2 = np.zeros([ncolx, nrowx], dtype=float) a1 = inv(np.dot(x.T,x)) a2 = np.dot(a1,x.T) beta = np.dot(a2,y) return(beta) </code></pre> <p>But now it doesn't work, I get the following error message:</p> <pre><code>ValueError: Buffer has wrong number of dimensions (expected 1, got 2) </code></pre> <p>What causes this error? I also have some other questions:</p> <p>What do these 2 lines actually do?</p> <pre><code>DTYPE = np.float ctypedef np.float_t DTYPE_t </code></pre> <p>Also, if I understand correctly typing this cdef np.ndarray[DTYPE_t, ndim=2] x = np.zeros([ncol, nrow], dtype=float) creates a two-dimensional array x with number of columns equal to ncol and row equal to nrow, that contain floats. But what does [DTYPE_t, ndim=2] actually does? I haven't found any documentation on this.</p> <p>Thank you in advance for your answers!</p> <p>EDIT: looks like if I replace DTYPE_t with double and comment these two lines: </p> <pre><code>DTYPE = np.float ctypedef np.float_t DTYPE_t </code></pre> <p>HOwever, execution is still slow. What can I do to speed things up?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload