gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:17:49 25 4
gpt4 key购买 nike

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

所以那条线的作用是sliced数组,取所有行 (:) 但保留第二列 (1)

关于python - X = X[:, 1]在Python中的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33491703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com