0

因此,我一直在以下列方式在 2 个 ndarray 矩阵之间使用 set 方法“symmetric_difference”:

x_set = list(set(tuple(i) for i in x_spam_matrix.tolist()).symmetric_difference(
                 set(tuple(j) for j in partitioned_x[i].tolist())))

x = np.array([list(i) for i in x_set])

这种方法对我来说很好用,但感觉有点笨拙……无论如何,有没有更优雅的方式来进行呢?

4

2 回答 2

0

Python之禅

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
[...]

您的代码没有任何问题。虽然如果我必须对其进行代码审查,我会建议以下内容

spam_matrix_set = set(tuple(item) for item in x_spam_matrix.tolist())
partitioned_set = set(tuple(item) for item in partitioned_x[index].tolist())
disjunctive_union = spam_matrix_set.symmetric_difference(partitioned_set)

x = np.array([list(item) for item in disjunctive_union])
于 2018-06-20T06:51:31.233 回答
0

一个简单的元组列表:

In [146]: alist = [(1,2),(3,4),(2,1),(3,4)]

把它放在一组:

In [147]: aset = set(alist)
In [148]: aset
Out[148]: {(1, 2), (2, 1), (3, 4)}

np.array只是将集合包装在一个对象 dtype 中:

In [149]: np.array(aset)
Out[149]: array({(1, 2), (3, 4), (2, 1)}, dtype=object)

但把它变成一个列表,并得到一个二维数组:

In [150]: np.array(list(aset))
Out[150]: 
array([[1, 2],
       [3, 4],
       [2, 1]])

既然是元组列表,也可以做成结构化数组:

In [151]: np.array(list(aset),'i,f')
Out[151]: array([(1, 2.), (3, 4.), (2, 1.)], dtype=[('f0', '<i4'), ('f1', '<f4')])

如果元组的长度不同,则元组列表将转换为元组的一维数组(对象 dtype):

In [152]: np.array([(1,2),(3,4),(5,6,7)])
Out[152]: array([(1, 2), (3, 4), (5, 6, 7)], dtype=object)
In [153]: _.shape
Out[153]: (3,)
于 2018-06-20T07:38:11.230 回答