Python中X=X[:,1]的含义

2024-05-16 07:50:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究这段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]

Tags: the代码addmodeldefparamsrunningfit
2条回答

这就像你在指定轴一样。将起始列视为0,然后在进行1、2等操作时。

语法是x[row_index,column_index]

您还可以根据行索引中的需要指定行值的范围,例如:1:13提取前13行以及列中指定的内容

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

相关问题 更多 >