numpy ravel函数返回问题

时间:2018-02-14 11:42:36

标签: python numpy

当ravel()返回nD数组中所有元素的连续1D数组时,我发现它只返回了所提到的X1的唯一元素。我错过了什么吗?



tst
Out[44]: 
array([[1, 2, 3, 4],
       [1, 2, 3, 4]])

tst.ravel()
Out[45]: array([1, 2, 3, 4, 1, 2, 3, 4])

X1
Out[46]: 
array([[-2.99318916, -2.98318916, -2.97318916, ...,  3.13681084,
         3.14681084,  3.15681084],
       [-2.99318916, -2.98318916, -2.97318916, ...,  3.13681084,
         3.14681084,  3.15681084],
       [-2.99318916, -2.98318916, -2.97318916, ...,  3.13681084,
         3.14681084,  3.15681084],
       ..., 
       [-2.99318916, -2.98318916, -2.97318916, ...,  3.13681084,
         3.14681084,  3.15681084],
       [-2.99318916, -2.98318916, -2.97318916, ...,  3.13681084,
         3.14681084,  3.15681084],
       [-2.99318916, -2.98318916, -2.97318916, ...,  3.13681084,
         3.14681084,  3.15681084]])

X1.ravel()
Out[47]: 
array([-2.99318916, -2.98318916, -2.97318916, ...,  3.13681084,
        3.14681084,  3.15681084])




1 个答案:

答案 0 :(得分:0)

这是一个印刷问题。 您可以使用numpy.set_printoptions调整此行为:

In [404]: np.array([arange(10)]*10).ravel()
Out[404]: 
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
       3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
       6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8,
       9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
       2, 3, 4, 5, 6, 7, 8, 9])

In [405]: np.set_printoptions(threshold=90)

In [406]: np.array([arange(10)]*10).ravel()
Out[406]: array([0, 1, 2, ..., 7, 8, 9])