2 votes

Python: 'numpy.int32' object is not iterable

I am constantly getting this error and I don't know how to fix it.

This is the code:

import numpy as np
import itertools
import random

g_row=10
g_col=10

m_prob = np.ones((g_row, g_col), dtype=np.int)

s_acomulada = list(itertools.accumulate(itertools.chain.from_iterable(m_prob)))

papeletas = np.ones((g_row, g_col), dtype=np.int)

for i, fila in enumerate(s_acomulada):
  for j, n in enumerate(fila):
    papeletas[i][j] = tuple(range(inicio, inicio+n))
    inicio += n

print(papeletas)

And when I run it, it returns this:

TypeError: 'numpy.int32' object is not iterable

I would also like m_prob, s_acomulada and ballots to be the three 10x10 matrices, like m_prob, if possible.

Best regards, thank you.

1voto

FJSevilla Points 29084

The error occurs in the second for nested because fila is an integer. This occurs because s_acumulada is a one-dimensional list of integers since the accumulated sum is flattened to calculate the cumulative sum m_prob previously.

Note that NumPy has its own function to perform cumulative addition, numpy.cumsum . When working with NumPy arrays use the methods it provides whenever possible to preserve efficiency. To preserve dimensions simply apply the reshape after carrying out the accumulated sum.

On the other hand, if a papeletas you are going to assign python tuples you can't initialize it with integer type, NumPy arrays, unlike a python list, can't change type or mix types happily. You must initialize it with dtype object .

Finally, you need to declare inicio before for .

import numpy as np

g_row=200
g_col=200

m_prob = np.ones((g_row, g_col), dtype=np.int)
papeletas = np.empty((g_row, g_col), dtype=object)
s_acumulada = np.cumsum(m_prob, axis=None, dtype=np.int).reshape(m_prob.shape)

inicio=1
for i, fila in enumerate(s_acumulada):
  for j, n in enumerate(fila):
    papeletas[i][j] = tuple(range(inicio, inicio+n))
    inicio += n
>>> m_prob
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])

>>> m_prob.shape
(10, 10)

>>> s_acumulada
array([[  1,   2,   3,   4,   5,   6,   7,   8,   9,  10],
       [ 11,  12,  13,  14,  15,  16,  17,  18,  19,  20],
       [ 21,  22,  23,  24,  25,  26,  27,  28,  29,  30],
       [ 31,  32,  33,  34,  35,  36,  37,  38,  39,  40],
       [ 41,  42,  43,  44,  45,  46,  47,  48,  49,  50],
       [ 51,  52,  53,  54,  55,  56,  57,  58,  59,  60],
       [ 61,  62,  63,  64,  65,  66,  67,  68,  69,  70],
       [ 71,  72,  73,  74,  75,  76,  77,  78,  79,  80],
       [ 81,  82,  83,  84,  85,  86,  87,  88,  89,  90],
       [ 91,  92,  93,  94,  95,  96,  97,  98,  99, 100]], dtype=int32)

>>> s_acumulada.shape
(10, 10)

>>> papeletas
array([[(1,), (2, 3), (4, 5, 6), (7, 8, 9, 10), (11, 12, 13, 14, 15),
        (16, 17, 18, 19, 20, 21), (22, 23, 24, 25, 26, 27, 28),
        (29, 30, 31, 32, 33, 34, 35, 36),
        (37, 38, 39, 40, 41, 42, 43, 44, 45),
        (46, 47, 48, 49, 50, 51, 52, 53, 54, 55)],
       [(56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66),
        ...]...], dtype=object)

>>> papeletas.shape
(10, 10)

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X