2015-11-03 89 views
7

我正在研究Python代碼片段。 X = X[:, 1]在最後一行是什麼意思?Python中X = X [:,1]的含義

def linreg(X,Y): 
    # Running the linear regression 
    X = sm.add_constant(X) 
    model = regression.linear_model.OLS(Y, X).fit() 
    a = model.params[0] 
    b = model.params[1] 
    X = X[:, 1] 

回答

17
x = np.random.rand(3,2) 

x 
Out[37]: 
array([[ 0.03196827, 0.50048646], 
     [ 0.85928802, 0.50081615], 
     [ 0.11140678, 0.88828011]]) 

x = x[:,1] 

x 
Out[39]: array([ 0.50048646, 0.50081615, 0.88828011]) 

所以該行做了什麼是sliced陣列,把所有行(:),但保持第二列(1

+0

多謝了Leb – Taewan

+0

沒問題,很高興我的幫助。 – Leb

+0

您的鏈接不正確(它指向Python 2.3文檔)。改用'numpy'文檔中的[this one](http://docs.scipy.org/doc/numpy-1.10.1/reference/arrays.indexing.html#basic-slicing-and-indexing)。 – MattDMo