Search code examples
pythonpython-3.xnumpyscikit-learnlinear-regression

What does [i,:] mean in Python?


So I'm finished one part of this assignment I have to do. There's only one part of the assignment that doesn't make any sense to me.

I'm doing a LinearRegression model and according to others I need to apply ans[i,:] = y_poly at the very end, but I never got an answer as to why.

Can someone please explain to me what [i,:] means? I haven't found any explanations online.


Solution

  • It's specific to the numpy module, used in most data science modules.

    ans[i,:] = y_poly
    

    this is assigning a vector to a slice of numpy 2D array (slice assignment). Self-contained example:

    >>> import numpy
    >>> a = numpy.array([[0,0,0],[1,1,1]])
    >>> a[0,:] = [3,4,5]
    >>> a
    array([[3, 4, 5],
           [1, 1, 1]])
    

    There is also slice assignment in base python, using only one dimension (a[:] = [1,2,3])