[英]Meaning of X = X[:, 1] in 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]
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])
因此,该行所做的是对数组进行切片,取所有行( :
)但保留第二列( 1
)
您需要搜索的术语是 slice。 x[start:end:step]是完整的形式,这里我们可以省略使用默认值: start 默认为 0 , end 默认为列表的长度, step 默认为 1。因此 x[:] 表示与 x[0:len(x):1] 相同
就像您正在指定轴一样。 将起始列视为 0,然后在经历 1,2 时,依此类推。
语法是x[row_index,column_index]
您还可以根据需要在 row_index 中指定一系列行值,例如:1:13 提取前 13 行以及列中指定的任何内容
x[:,1] 这是二维切片,这里是 x[row_index, column_index]
Python 中 X = X[:, 1] 的含义是:
例如:
x = array([[0.69859393, 0.1042432 ],
[0.55138493, 0.18639614],
[0.27338772, 0.80351282]])
x[:,1] = array([0.1042432 , 0.18639614, 0.80351282])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.