Difference between numpy.array shape (R, 1) and (R,)

后端 未结 6 1558
甜味超标
甜味超标 2020-11-22 04:25

In numpy, some of the operations return in shape (R, 1) but some return (R,). This will make matrix multiplication more tedious since

6条回答
  •  别那么骄傲
    2020-11-22 04:52

    There are a lot of good answers here already. But for me it was hard to find some example, where the shape or array can break all the program.

    So here is the one:

    import numpy as np
    a = np.array([1,2,3,4])
    b = np.array([10,20,30,40])
    
    
    from sklearn.linear_model import LinearRegression
    regr = LinearRegression()
    regr.fit(a,b)
    

    This will fail with error:

    ValueError: Expected 2D array, got 1D array instead

    but if we add reshape to a:

    a = np.array([1,2,3,4]).reshape(-1,1)
    

    this works correctly!

提交回复
热议问题