更改结构化数组和重新数组的打印方式

时间:2018-07-24 13:43:00

标签: python numpy ipython recarray structured-array

Numpy汇总了大arrays,在进行交互式会话时很方便。不幸的是,默认情况下,结构化的arraysrecarrays总结得不是很好。有办法改变吗?

默认情况下,如果有1000个或更少的项目,则显示完整的array。如果项目多于此,则array会被汇总。可以使用np.set_printoptions(threshold=<number of items to trigger summarization>, edgeitems=<number of items to show in summary>)进行设置。 对于标准数据类型,这可以很好地工作,例如:

np.set_printoptions(threshold=3, edgeitems=1)
print(np.zeros(3))
print(np.zeros(4))

产生

[ 0.  0.  0.]
[ 0. ...,  0.]

但是,当使用更复杂的数据类型时,汇总没有那么有用

print(np.zeros(4, dtype=[('test', 'i4', 3)]))
print(np.zeros(4, dtype=[('test', 'i4', 4)]))

[([0, 0, 0],) ..., ([0, 0, 0],)]
[([0, 0, 0, 0],) ..., ([0, 0, 0, 0],)]

该数组已汇总,但子数据类型未汇总。对于使用复杂数据类型的大型arrays来说,这将成为一个问题。例如array np.zeros(1000, dtype=[('a', float, 3000), ('b', float, 10000)])挂断了我的ipython实例。

有两种解决方法,而不是直接使用np.array类型,可以继承并编写自定义__repr__。这将适用于大型项目,但不能解决根本问题,并且不便于在交互式python会话中快速浏览数据。 我还在编辑器中实现了一个自定义过滤器,该过滤器会截断很长的控制台输出。这有点hack,当我在其他地方启动python会话时无济于事。

是否存在我不知道的numpy设置,或者可以解决此问题的python或ipython设置?

1 个答案:

答案 0 :(得分:0)

这是我想出的一种解决方法,可以合理地打印记录数组。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<body data-spy="scroll" data-target=".navbar" data-offset="15">
  <nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
    <a class="navbar-brand" href="#">Logo</a>
    <ul class="navbar-nav ml-auto">
      <li class="nav-item">
        <a class="nav-link" href="about.html">About Us</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="services.html">Services</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="contact.html">Contact</a>
      </li>
    </ul>
  </nav>

  <div class="container-fluid bg-light page-section">
    <h1>Section 1</h1>
    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
  </div>
  <div class="container-fluid bg-dark page-section">
    <h1>Section 2</h1>
    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
  </div>
  <div class="container-fluid bg-light page-section">
    <h1>Section 3</h1>
    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
  </div>
</body>