Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that your problem is that you are expecting <code>np.append</code> to add the column in-place, but what it does, because of how numpy data is stored, is create a copy of the joined arrays</p> <pre><code>Returns ------- append : ndarray A copy of `arr` with `values` appended to `axis`. Note that `append` does not occur in-place: a new array is allocated and filled. If `axis` is None, `out` is a flattened array. </code></pre> <p>so you need to save the output <code>all_data = np.append(...)</code>:</p> <pre><code>my_data = np.random.random((210,8)) #recfromcsv('LIAB.ST.csv', delimiter='\t') new_col = my_data.sum(1)[...,None] # None keeps (n, 1) shape new_col.shape #(210,1) all_data = np.append(my_data, new_col, 1) all_data.shape #(210,9) </code></pre> <p>Alternative ways:</p> <pre><code>all_data = np.hstack((my_data, new_col)) #or all_data = np.concatenate((my_data, new_col), 1) </code></pre> <p>I believe that the only difference between these three functions (as well as <code>np.vstack</code>) are their default behaviors for when <code>axis</code> is unspecified:</p> <ul> <li><code>concatenate</code> assumes <code>axis = 0</code></li> <li><code>hstack</code> assumes <code>axis = 1</code> unless inputs are 1d, then <code>axis = 0</code></li> <li><code>vstack</code> assumes <code>axis = 0</code> after adding an axis if inputs are 1d</li> <li><code>append</code> flattens array</li> </ul> <hr> <p>Based on your comment, and looking more closely at your example code, I now believe that what you are probably looking to do is add a <em>field</em> to a <a href="http://www.scipy.org/Cookbook/Recarray" rel="noreferrer"><em>record array</em></a>. You imported both <code>genfromtxt</code> which returns a <a href="http://docs.scipy.org/doc/numpy/user/basics.rec.html" rel="noreferrer"><em>structured array</em></a> and <code>recfromcsv</code> which returns the subtly different <a href="http://www.scipy.org/Cookbook/Recarray" rel="noreferrer"><em>record array</em> (<code>recarray</code>)</a>. You used the <code>recfromcsv</code> so right now <code>my_data</code> is actually a <code>recarray</code>, which means that most likely <code>my_data.shape = (210,)</code> since recarrays are 1d arrays of records, where each record is a tuple with the given dtype.</p> <p>So you could try this:</p> <pre><code>import numpy as np from numpy.lib.recfunctions import append_fields x = np.random.random(10) y = np.random.random(10) z = np.random.random(10) data = np.array( list(zip(x,y,z)), dtype=[('x',float),('y',float),('z',float)]) data = np.recarray(data.shape, data.dtype, buf=data) data.shape #(10,) tot = data['x'] + data['y'] + data['z'] # sum(axis=1) won't work on recarray tot.shape #(10,) all_data = append_fields(data, 'total', tot, usemask=False) all_data #array([(0.4374783740738456 , 0.04307289878861764, 0.021176067323686598, 0.5017273401861498), # (0.07622262416466963, 0.3962146058689695 , 0.27912715826653534 , 0.7515643883001745), # (0.30878532523061153, 0.8553768789387086 , 0.9577415585116588 , 2.121903762680979 ), # (0.5288343561208022 , 0.17048864443625933, 0.07915689716226904 , 0.7784798977193306), # (0.8804269791375121 , 0.45517504750917714, 0.1601389248542675 , 1.4957409515009568), # (0.9556552723429782 , 0.8884504475901043 , 0.6412854758843308 , 2.4853911958174133), # (0.0227638618687922 , 0.9295332854783015 , 0.3234597575660103 , 1.275756904913104 ), # (0.684075052174589 , 0.6654774682866273 , 0.5246593820025259 , 1.8742119024637423), # (0.9841793718333871 , 0.5813955915551511 , 0.39577520705133684 , 1.961350170439875 ), # (0.9889343795296571 , 0.22830104497714432, 0.20011292764078448 , 1.4173483521475858)], # dtype=[('x', '&lt;f8'), ('y', '&lt;f8'), ('z', '&lt;f8'), ('total', '&lt;f8')]) all_data.shape #(10,) all_data.dtype.names #('x', 'y', 'z', 'total') </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.
    3. 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