从numpy数组中删除选择索引处的行 [英] Delete rows at select indexes from a numpy array

查看:164
本文介绍了从numpy数组中删除选择索引处的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据集中,我已经接近200行,但是为了最小的工作,例如,让我们假设以下数组:

In my dataset I've close to 200 rows but for a minimal working e.g., let's assume the following array:

arr = np.array([[1,2,3,4], [5,6,7,8], 
               [9,10,11,12], [13,14,15,16], 
               [17,18,19,20], [21,22,23,24]])

我可以对以下3行进行随机采样:

I can take a random sampling of 3 of the rows as follows:

indexes = np.random.choice(np.arange(arr.shape[0]), int(arr.shape[0]/2), replace=False)

使用这些索引,我可以如下选择测试用例:

Using these indexes, I can select my test cases as follows:

testing = arr[indexes]

我想删除这些索引处的行,并且可以将其余元素用于我的训练集.

I want to delete the rows at these indexes and I can use the remaining elements for my training set.

从帖子此处,看来training = np.delete(arr, indexes)应该这样做.但是我却得到一维数组.

From the post here, it seems that training = np.delete(arr, indexes) ought to do it. But I get 1d array instead.

我也在此处使用training = arr[indexes.astype(np.bool)],但没有给出清晰的分隔.我在训练和测试集中都得到了元素[5,6,7,8].

I also tried the suggestion here using training = arr[indexes.astype(np.bool)] but it did not give a clean separation. I get element [5,6,7,8] in both the training and testing sets.

training = arr[indexes.astype(np.bool)]

testing
Out[101]: 
array([[13, 14, 15, 16],
       [ 5,  6,  7,  8],
       [17, 18, 19, 20]])

training
Out[102]: 
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

知道我在做什么错吗?谢谢.

Any idea what I am doing wrong? Thanks.

推荐答案

要从numpy数组中删除索引行:

To delete indexed rows from numpy array:

arr = np.delete(arr, indexes, axis=0)

这篇关于从numpy数组中删除选择索引处的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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