Home > Net >  The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (33,) in
The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (33,) in

Time:06-02

I stuck in convert list to numpy. Convert list size is (33, n, 428). N is randomly difference that I don't know how numbers are consist. Here is error.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\HILAB_~1\AppData\Local\Temp/ipykernel_22960/872733971.py in <module>
----> 1 X_train = np.array(X_train, dtype=np.float64)
      2 
      3 for epoch in range(EPOCH):
      4     X_train_ten, y_train_ten = Variable(torch.from_numpy(X_train)), Variable(torch.tensor(y_train, dtype=torch.float32, requires_grad=True))
      5     print(X_train_ten.size())

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (33,)   inhomogeneous part.

and problem code is here.

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42, shuffle=True
)

print("[SIZE]\t\tTrain X size : {}, Train y size : {}\n\t\tTest X size : {}, Test y size : {}"\
        .format(len(X_train), len(y_train), len(X_test), len(y_test)))

train_dataloadloader = DataLoader(X_train)
test_dataloader = DataLoader(X_test)

X_train = np.array(X_train, dtype=np.float64)

I can't understand what does error means. Please help. thanks :D

CodePudding user response:

It means that whatever sequences X contains, they are not of the same length. You can check {len(e) for e in X); this is the set of all different lengths found in X.

Consider the following example:

>>> import numpy as np
>>> x = [[1, 2], [2, 3, 4]]
>>> np.array(x, dtype=np.float64)
[...]
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,)   inhomogeneous part.

Here, the list x contains two other lists, one of length 2 and the other of length 3. They can't be combined into one array since the "column" dimension doesn't match.

  • Related