Note that there are some explanatory texts on larger screens.

plurals
  1. POndarray field names for both row and column?
    primarykey
    data
    text
    <p>I'm a computer science teacher trying to create a little gradebook for myself using NumPy. But I think it would make my code easier to write if I could create an ndarray that uses field names for both the rows and columns. Here's what I've got so far:</p> <pre><code>import numpy as np num_stud = 23 num_assign = 2 grades = np.zeros(num_stud, dtype=[('assign 1','i2'), ('assign 2','i2')]) #etc gv = grades.view(dtype='i2').reshape(num_stud,num_assign) </code></pre> <p>So, if my first student gets a 97 on 'assign 1', I can write either of:</p> <pre><code>grades[0]['assign 1'] = 97 gv[0][0] = 97 </code></pre> <p>Also, I can do the following:</p> <pre><code>np.mean( grades['assign 1'] ) # class average for assignment 1 np.sum( gv[0] ) # total points for student 1 </code></pre> <p>This all works. But what I <strong>can't</strong> figure out how to do is use a student id number to refer to a particular student (assume that two of my students have student ids as shown):</p> <pre><code>grades['123456']['assign 2'] = 95 grades['314159']['assign 2'] = 83 </code></pre> <p>...or maybe create a second view with the different field names?</p> <pre><code>np.sum( gview2['314159'] ) # total points for the student with the given id </code></pre> <p>I know that I could create a dict mapping student ids to indices, but that seems fragile and crufty, and I'm hoping there's a better way than:</p> <pre><code>id2i = { '123456': 0, '314159': 1 } np.sum( gv[ id2i['314159'] ] ) </code></pre> <p>I'm also willing to re-architect things if there's a cleaner design. I'm new to NumPy, and I haven't written much code yet, so starting over isn't out of the question if I'm Doing It Wrong.</p> <p>I <em>am</em> going to be needing to sum all the assignment points for over a hundred students once a day, as well as run standard deviations and other stats. Plus, I'll be waiting on the results, so I'd like it to run in only a couple of seconds.</p> <p>Thanks in advance for any suggestions.</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.
 

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