title: Return an std::vector to python as a numpy array
body: <p>Using Pybind11, I am trying to pass a numpy array to c++ into a <code>std::vector</code>, multiply it by 2, and return this <code>std::vector</code> to python as a numpy array.</p>
  <p>I have achieved the first step but the third is doing some strange things. For passing it back I have used: <code>py::array ret =  py::cast(vect_arr);</code> By strange things I mean that the vector returned in Python doesn't have the correct dimensions nor the correct order.</p>
  <p>As example, I have as array:</p>
  <pre><code>[[ 0.78114362  0.06873818  1.00364053  0.93029671]
   [ 1.50885413  0.38219005  0.87508337  2.01322396]
   [ 2.19912915  2.47706644  1.16032292 -0.39204517]]
  </code></pre>
  <p>and the code returns:</p>
  <pre><code>array([[ 1.56228724e+000,  3.01770826e+000,  4.39825830e+000,
           5.37804299e+161],
         [ 1.86059342e+000,  4.02644793e+000, -7.84090347e-001,
           1.38298992e-309],
         [ 1.75016674e+000,  2.32064585e+000,  0.00000000e+000,
           1.01370255e-316]])
  </code></pre>
  <p>I have read the documentation but I am having trouble understanding most of it.</p>
  <p>Here an example to try:</p>
  <pre><code>#include &lt;pybind11/pybind11.h&gt;
  #include &lt;pybind11/numpy.h&gt;
  #include &lt;pybind11/stl.h&gt;
  #include &lt;Python.h&gt;
  namespace py = pybind11;
  py::module nn = py::module::import(&quot;iteration&quot;);
  
  
  py::array nump(py::array arr){
  
      auto arr_obj_prop = arr.request();
      //initialize values
      double *vals = (double*) arr_obj_prop.ptr;
  
      unsigned int shape_1 = arr_obj_prop.shape[0];
      unsigned int shape_2 = arr_obj_prop.shape[1];
  
  
      std::vector&lt;std::vector &lt;double&gt;&gt; vect_arr( shape_1, std::vector&lt;double&gt; (shape_2));
  
      for(unsigned int i = 0; i &lt; shape_1; i++){
        for(unsigned int j = 0; j &lt; shape_2; j++){
          vect_arr[i][j] = vals[i*shape_1 + j*shape_2] * 2;
        }
      }   
  
      py::array ret =  py::cast(vect_arr); //py::array(vect_arr.size(), vect_arr.data());
      return ret;
  
  }
  
  PYBIND11_MODULE(iteration_mod, m) {
  
      m.doc() = &quot;pybind11 module for iterating over generations&quot;;
  
      m.def(&quot;nump&quot;, &amp;nump,
        &quot;the function which loops over a numpy array&quot;);
  }
  </code></pre>
  <p>And the Python code:</p>
  <pre><code>import numpy as np
  import iteration_mod as i_mod
  
  class iteration(object):
      def __init__(self):
          self.iterator = np.random.normal(0,1,(3,4))
  
      def transform_to_dict(self):
          self.dict = {}
          for i in range(self.iterator.shape[0]):
              self.dict[&quot;key_number_{}&quot;.format(i)] = self.iterator[i,:]
          return self.dict
  
      def iterate_iterator(self):
          return i_mod.nump(self.iterator)
  
      def iterate_dict(self):
          return i_mod.dict(self)
  
  a = iteration()
  print(a.iterator)
  print(a.iterate_iterator())
  </code></pre>
  <p>All of this compiled with: <code>c++ -O3 -Wall -fopenmp -shared -std=c++11 -fPIC </code>python3 -m pybind11 --includes<code> iteration_mod.cpp -o iteration_mod.so</code></p>