如何调用整数的np.zeros?

时间:2016-07-12 01:28:47

标签: python

Musica = np.zeros((row*120,3))

for k in range(21, 90):
    for i in range(row):
        for j in range(Max[k], Min[k]):
            Musica[i*120 + j,:] = 0,i,j
            if np.all(data[i*col + j,:]==(255.000,0.000,0.000,i,j)):
                Music[i*120 + j,:] = 1,i,j

Max[k]Min[k]是属于数学整数组的数字,但是我从python收到此消息

  

" TypeError:range()期望整数结束参数,得到numpy.float64。"

如何调用整数类型的np.zeros

现在我正在尝试使用以下代码:

Musica = np.zeros((row*120,3))

for k in range(21, 90):
    for i in range(row):
        Max = int(Max[k])
        Min = int(Min[k])
        for j in range(Max, Min):
            Musica[i*120 + j,:] = 0,i,j
            if np.all(data[i*col + j,:]==(255.000,0.000,0.000,i,j)):
                Musica[i*120 + j,:] = 1,i,j

我收到的错误信息是:

Traceback (most recent call last):

  File "<ipython-input-115-4aef2441146c>", line 1, in <module>
    runfile('C:/Users/Arthur_USP/Desktop/Informação/Nova abordagem/untitled0.py', wdir='C:/Users/Arthur_USP/Desktop/Informação/Nova abordagem')

  File "C:\Users\Arthur_USP\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
    execfile(filename, namespace)

  File "C:\Users\Arthur_USP\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/Arthur_USP/Desktop/Informação/Nova abordagem/untitled0.py", line 181, in <module>

TypeError: 'int' object has no attribute '__getitem__'

第181行是&#34; Max = int(Max [k])&#34;

1 个答案:

答案 0 :(得分:12)

您可以尝试np.zeros(shape).astype(int)

编辑: 实际上,零接受dtype参数。所以更好的是np.zeros(shape, dtype=int)