title: Python/Numpy - return slope for simple linear regression in a new array
body: <p>I'm searching for an answer for the following problem:</p>
  
  <p>I want to create a numpy array where all the intercepts and slopes are stored. The slope is the increase of <code>means</code> over the <code>years</code>. I have found multiple ways to calculate the intercept/slope, but I really miss the link to get them in a new array (I'm new to Numpy so the logic is slowly getting there but I've now been stuck for a day..)</p>
  
  <p>So.. I have an array that is structured like this:</p>
  
  <pre><code>x = np.array([(2000, 'A', '1',5), (2001, 'A', '1', 10),
                (2003, 'A', '1',15), (2004, 'A', '1', 20),
                (2000, 'A', '2',1), (2001, 'A', '2', 2),
                (2002, 'A', '2', 3), (2003, 'A', '2', 4)],
               dtype=[('year', 'i4'), ('group1', 'U2'), ('group2', 'U2'), ('means', 'i2')])
  </code></pre>
  
  <p>And I would like to end up with an array like this:</p>
  
  <pre><code>&gt;desired_array
  array([('A', '1', 5, 5), 
         ('A', '2', 1, 1)],
         dtype=[('group1', '&lt;U2'), ('group2', '&lt;U2'), ('intercept', '&lt;i2'), ('slope', '&lt;i2')])
  </code></pre>
  
  <p>I have gotten to this point:</p>
  
  <pre><code>ans, indices = np.unique(x[['group1', 'group2']], return_inverse=True)
  desired_array = np.empty(2, dtype=[('group1', 'U2'), ('group2', 'U2'), ('intercept', '&lt;f8'),
                                     ('slope', '&lt;f8')])
  desired_array['group1'] = ans['group1']
  desired_array['group2'] = ans['group2']
  x = x[x['year'] == 2000]
  desired_array['intercept'] = x['means']
  </code></pre>
  
  <p>it's a bit rough which I can still improve but the main question for me where I get stuck is how to add the slope per regression line to the array.</p>
  
  <p>Would be great is someone could help me out :)</p>