0

I'm currently trying to build a practice linear parameter estimation python program using numpy but I've never written in python before and I'm very loose with numpy. I have a series of x,y data points that I want to loop over and build a new vector with that is a 5x1 vector containing only the y values.

Here is what I have so far that isn't working:

def data_loader():
    ## edit to have an i/o feature for retrieving data points later ##
    data_points = np.array([[1,5.7],[2,19.2],[3,37.8],[4,67.3],[5,86.4]])
    return data_points

def build_b(data_points):
    b = np.empty((0,1), int)
    for x in data_points:
        for y in x:
            b = np.append(y, axis=0)
    return b

In addition I would also like to eventually have a user input for data points but that is down the road I guess.

  • Learn python first, then learn numpy. So first a python tutorial, then a numpy one. – Andras Deak -- Слава Україні Apr 07 '22 at 21:46
  • `b = np.append(y, axis=0)` - how much time did you spend reading `np.append` docs? It is not a list append clone; and is slower when it does work. That `build_b` loop looks like you have used, or at least read about, a list append loop. Why aren't you using that? – hpaulj Apr 07 '22 at 23:33

1 Answers1

0

To fetch just the Y values, use data_points[:,1]. That says "use all rows, but only element 1 (counting from 0) of the rows".

That's a 5-element vector. Not sure why you're expecting (3,1).

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • I meant to say 5 not 3. So I would just use data_points[:,1] in the for each statement? – Eliot Shedlock Apr 07 '22 at 21:41
  • You would not use a `for` statement at all. `data_points[:,1]` returns to you a 5-element array. That's the only statement you need. The BIG advantage of numpy is that you work with whole arrays, or columns, or rows at a time. No looping. – Tim Roberts Apr 07 '22 at 21:44