Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the trick is to <code>reshape(-1, nbcols)</code> what you get from <code>np.genfromtxt</code>, so your assignment should look like:</p> <pre><code>a[:, ind:ind + nbcols] = np.genfromtxt('file_1000x10.csv', usecols = range(nbcols)).reshape(-1, nbcols) </code></pre> <p>On a separate note, looping over <code>ind</code>, and reading the file every time is unnecessary. You can do a little bit of higher dimensionality voodoo as follows:</p> <pre class="lang-py prettyprint-override"><code>import numpy as np from StringIO import StringIO def make_data(rows, cols) : data = ((str(k + cols * j) for k in xrange(cols)) for j in xrange(rows)) data = '\n'.join(map(lambda x: ' '.join(x), data)) return StringIO(data) def read_data(f, rows, cols, nbcols) : a = np.zeros((rows, (cols + nbcols - 1) // nbcols, nbcols)) a[...] = np.genfromtxt(f, usecols=range(nbcols)).reshape(-1, 1, nbcols) return a.reshape(rows, -1)[:, :cols] &gt;&gt;&gt; read_data(make_data(3, 6), 3, 6, 2) array([[ 0., 1., 0., 1., 0., 1.], [ 6., 7., 6., 7., 6., 7.], [ 12., 13., 12., 13., 12., 13.]]) &gt;&gt;&gt; read_data(make_data(3, 6), 3, 6, 1) array([[ 0., 0., 0., 0., 0., 0.], [ 6., 6., 6., 6., 6., 6.], [ 12., 12., 12., 12., 12., 12.]]) &gt;&gt;&gt; read_data(make_data(3, 6), 3, 6, 4) array([[ 0., 1., 2., 3., 0., 1.], [ 6., 7., 8., 9., 6., 7.], [ 12., 13., 14., 15., 12., 13.]]) </code></pre> <hr> <p><strong>ORIGINAL ANSWER</strong> You can add that extra dimension of size 1 to <code>your_array</code> using:</p> <pre><code>your_array.reshape(your_array.shape + (1,)) </code></pre> <p>or the equivalent</p> <pre><code>your_array.reshape(-1, 1) </code></pre> <p>The same can be achieved with</p> <pre><code>your_array[..., np.newaxis] </code></pre> <p>or the equivalent</p> <pre><code>your_array[..., None] </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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