带有numpy.random.permutation的未调整大小的对象错误? [英] Unsized object error with numpy.random.permutation?

查看:148
本文介绍了带有numpy.random.permutation的未调整大小的对象错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一些代码停留在一行上:

I have some code right now that is getting stuck on one line:

perm = numpy.random.permutation(128)

向其显示以下错误:"TypeError:未调整大小的对象的len()".我不知道问题出在哪里,因为128只是一个整数.我看到这是一个可能已经在这里解决的问题: http://mail.scipy.org/pipermail/numpy-discussion/2007-January/025592.html ,但是他们的解决方案对浮点数没有帮助,对我没有帮助.

To which it give the following error: "TypeError: len() of unsized object." I can't figure out what the issue is since 128 is just an integer. I see that this is a problem that has probably been resolved before here: http://mail.scipy.org/pipermail/numpy-discussion/2007-January/025592.html but their solution isn't helpful to me since it is about floats.

任何人都可以看到这里出了什么问题吗?

Can anyone see what's going wrong here?

推荐答案

在Sage中,输入由Sage预准备器准备.

In Sage, the input is preparsed by the Sage preparser.

我将使用12而不是128,因此示例可以放在一行中.

I'll use 12 instead of 128 so the examples fit in one line.

当您输入以下内容时:

sage: import numpy
sage: perm = numpy.random.permutation(12)

您收到的错误消息如下:

The error message you get looks like:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-38b6a5e3e889> in <module>()
----> 1 perm = numpy.random.permutation(Integer(12))

/opt/sage/local/lib/python2.7/site-packages/numpy/random/mtrand.so in mtrand.RandomState.permutation (numpy/random/mtrand/mtrand.c:21297)()

/opt/sage/local/lib/python2.7/site-packages/numpy/random/mtrand.so in mtrand.RandomState.shuffle (numpy/random/mtrand/mtrand.c:20965)()

TypeError: len() of unsized object

在其中特别看到的行:

----> 1 perm = numpy.random.permutation(Integer(12))

告诉您您的输入

perm = numpy.random.permutation(12)

已准备好

perm = numpy.random.permutation(Integer(12))

但是numpy对于被Sage Integer喂养并不满意, 宁愿使用Python int.

However numpy is not so happy being fed a Sage Integer, it would prefer a Python int.

输入原始Python整数的最简单方法是将r附加到该整数:

The simplest way to input a raw Python integer is to append r to it:

sage: perm = numpy.random.permutation(12r)

这将为您服务:

sage: perm = numpy.random.permutation(12r)
sage: perm    # random
array([ 9,  0, 11,  4,  2, 10,  3,  5,  7,  6,  1,  8])

另一种方法是让Sage将Python int转换为Sage Integer,然后强制其将其转换回Python整数:

Another way is to let Sage transform the Python int to a Sage Integer but then force it to convert it back to a Python integer:

sage: perm = numpy.random.permutation(int(12))
sage: perm    # random
array([ 5,  9,  1,  7,  0,  2, 10,  6,  3,  8,  4, 11])

您可以做的另一件事是关闭Sage预准备器.

Another thing you could do is to turn off the Sage preparser.

sage: preparser(False)
sage: perm = numpy.random.permutation(12)
sage: perm    # random
array([ 0,  2,  7,  5,  8, 11,  1,  6,  9, 10,  3,  4])

这篇关于带有numpy.random.permutation的未调整大小的对象错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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