Python 中 X = X[:, 1] 的含义 [英] Meaning of X = X[:, 1] in Python

查看:70
本文介绍了Python 中 X = X[:, 1] 的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究这段 Python 代码.X = X[:, 1] 在最后一行是什么意思?

I am studying this snippet of python code. What does X = X[:, 1] mean in last line?

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)

So what that line did is sliced the array, taking all rows (:) but keeping the second column (1)

这篇关于Python 中 X = X[:, 1] 的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆