简体   繁体   English

获取一维 np 数组的片段

[英]Getting pieces of an 1D np array

I am very new to python and machine learning.我对 python 和机器学习非常陌生。

Let's say that I have a 1D np array (with both numbers and NaN) with one column and 1308 rows and want to create two variables:假设我有一个一列 1308 行的一维 np 数组(同时包含数字和 NaN),并且想要创建两个变量:

train_outcome = outcome[0:891, 0]
y_pred =  outcome[891:, 0]

I tried this and got the obvious <IndexError: too many indices for the array: array is 1-dimensional, but 2 were indexed>.我尝试了这个并得到了明显的 <IndexError: too many indices for the array: array is 1-dimensional, but 2 was indexed>。

I was so desperate that I converted it back to a DF to make the operation.我非常绝望,以至于将其转换回 DF 进行操作。 There must be an easier way to achieve this.必须有一种更简单的方法来实现这一点。

If the array has 1 dimension, there is no need for a comma.如果数组有 1 维,则不需要逗号。 Here is how I'd do it:这是我的做法:

train_outcome = outcome[:891]
y_pred = outcome[891:]

Use np.split for a one-liner:np.split用于单行:

train_outcome, y_pred = np.split(outcome, [891])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM