MEHUL PATEL
MEHUL PATEL

Reputation: 1

ValueError:The requested array has an inhomogeneous shape after 2 dimensions.The detected shape was(318, 20)+inhomogeneous part

from sklearn.neural_network import MLPClassifier
#Initialise Multi Layer Perceptron Classifier (MLP)
model = MLPClassifier(alpha = 0.01, batch_size = 256, epsilon = 1e-08, hidden_layer_sizes = (400,), learning_rate = 'adaptive', max_iter = 500)
model.fit(X_train, y_train)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-41-d768f88d541e> in <module>
----> 1 model.fit(X_train, y_train)

5 frames
/usr/local/lib/python3.8/dist-packages/sklearn/neural_network/_multilayer_perceptron.py in fit(self, X, y)
    750             Returns a trained MLP model.
    751         """
--> 752         return self._fit(X, y, incremental=False)
    753 
    754     def _check_solver(self):

/usr/local/lib/python3.8/dist-packages/sklearn/neural_network/_multilayer_perceptron.py in _fit(self, X, y, incremental)
    391         )
    392 
--> 393         X, y = self._validate_input(X, y, incremental, reset=first_pass)
    394 
    395         n_samples, n_features = X.shape

/usr/local/lib/python3.8/dist-packages/sklearn/neural_network/_multilayer_perceptron.py in _validate_input(self, X, y, incremental, reset)
   1098 
   1099     def _validate_input(self, X, y, incremental, reset):
-> 1100         X, y = self._validate_data(
   1101             X,
   1102             y,

/usr/local/lib/python3.8/dist-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
    579                 y = check_array(y, **check_y_params)
    580             else:
--> 581                 X, y = check_X_y(X, y, **check_params)
    582             out = X, y
    583 

/usr/local/lib/python3.8/dist-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator)
    962         raise ValueError("y cannot be None")
    963 
--> 964     X = check_array(
    965         X,
    966         accept_sparse=accept_sparse,

/usr/local/lib/python3.8/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
    744                     array = array.astype(dtype, casting="unsafe", copy=False)
    745                 else:
--> 746                     array = np.asarray(array, order=order, dtype=dtype)
    747             except ComplexWarning as complex_warning:
    748                 raise ValueError(

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (318, 20) + inhomogeneous part.
for i in  X_train:
  print(i.shape)
(20, 100)
(20, 80)
(20, 69)
(20, 60)
(20, 67)
(20, 60)
(20, 60)
(20, 69)
(20, 63)
(20, 60)
(20, 92)
(20, 70)
(20, 63)
(20, 79)
(20, 53)
(20, 81)
(20, 86)
(20, 69)
(20, 87)
(20, 79)
(20, 76)
(20, 58)
(20, 84)
(20, 89)
(20, 90)
(20, 94)
(20, 117)
(20, 57)
(20, 83)
(20, 63)
(20, 81)
(20, 76)
(20, 84)
(20, 80)
(20, 73)
(20, 120)
(20, 79)
(20, 73)
(20, 81)
(20, 84)
(20, 81)
(20, 86)
(20, 74)
(20, 92)
(20, 84)
(20, 104)
(20, 73)
(20, 54)
(20, 81)
(20, 84)
(20, 76)
(20, 81)
(20, 107)
(20, 109)
(20, 107)
(20, 86)
(20, 94)
(20, 80)
(20, 73)
(20, 69)
(20, 107)
(20, 67)
(20, 79)
(20, 76)
(20, 70)
(20, 56)
(20, 70)
(20, 66)
(20, 71)
(20, 83)
(20, 74)
(20, 64)
(20, 76)
(20, 94)
(20, 69)
(20, 71)
(20, 103)
(20, 80)
(20, 97)
(20, 83)
(20, 79)
(20, 70)
(20, 70)
(20, 99)
(20, 90)
(20, 67)
(20, 64)
(20, 73)
(20, 87)
(20, 71)
(20, 84)
(20, 69)
(20, 84)
(20, 66)
(20, 92)
(20, 90)
(20, 67)
(20, 104)
(20, 71)
(20, 63)
(20, 96)
(20, 79)
(20, 84)
(20, 104)
(20, 54)
(20, 97)
(20, 81)
(20, 61)
(20, 79)
(20, 81)
(20, 79)
(20, 93)
(20, 102)
(20, 107)
(20, 76)
(20, 106)
(20, 94)
(20, 79)
(20, 99)
(20, 66)
(20, 100)
(20, 70)
(20, 83)
(20, 87)
(20, 93)
(20, 76)
(20, 77)
(20, 76)
(20, 73)
(20, 66)
(20, 107)
(20, 71)
(20, 79)
(20, 699)
(20, 57)
(20, 57)
(20, 70)
(20, 107)
(20, 84)
(20, 71)
(20, 71)
(20, 99)
(20, 67)
(20, 77)
(20, 92)
(20, 81)
(20, 76)
(20, 54)
(20, 77)
(20, 63)
(20, 64)
(20, 83)
(20, 66)
(20, 64)
(20, 110)
(20, 81)
(20, 74)
(20, 64)
(20, 76)
(20, 71)
(20, 71)
(20, 87)
(20, 146)
(20, 96)
(20, 97)
(20, 103)
(20, 70)
(20, 60)
(20, 61)
(20, 77)
(20, 70)
(20, 104)
(20, 83)
(20, 96)
(20, 53)
(20, 86)
(20, 64)
(20, 90)
(20, 92)
(20, 64)
(20, 84)
(20, 69)
(20, 63)
(20, 69)
(20, 46)
(20, 50)
(20, 56)
(20, 60)
(20, 100)
(20, 50)
(20, 51)
(20, 96)
(20, 92)
(20, 87)
(20, 84)
(20, 63)
(20, 64)
(20, 90)
(20, 71)
(20, 54)
(20, 126)
(20, 80)
(20, 79)
(20, 63)
(20, 89)
(20, 94)
(20, 77)
(20, 87)
(20, 69)
(20, 67)
(20, 90)
(20, 84)
(20, 117)
(20, 77)
(20, 70)
(20, 80)
(20, 90)
(20, 81)
(20, 81)
(20, 64)
(20, 79)
(20, 56)
(20, 60)
(20, 79)
(20, 73)
(20, 58)
(20, 67)
(20, 89)
(20, 80)
(20, 57)
(20, 96)
(20, 83)
(20, 70)
(20, 81)
(20, 69)
(20, 83)
(20, 80)
(20, 58)
(20, 93)
(20, 64)
(20, 63)
(20, 60)
(20, 64)
(20, 92)
(20, 63)
(20, 80)
(20, 106)
(20, 93)
(20, 63)
(20, 80)
(20, 96)
(20, 90)
(20, 112)
(20, 80)
(20, 90)
(20, 94)
(20, 86)
(20, 94)
(20, 79)
(20, 80)
(20, 76)
(20, 47)
(20, 60)
(20, 76)
(20, 90)
(20, 70)
(20, 96)
(20, 142)
(20, 92)
(20, 89)
(20, 84)
(20, 69)
(20, 71)
(20, 81)
(20, 106)
(20, 63)
(20, 80)
(20, 69)
(20, 86)
(20, 92)
(20, 69)
(20, 83)
(20, 80)
(20, 57)
(20, 61)
(20, 67)
(20, 97)
(20, 94)
(20, 94)
(20, 54)
(20, 76)
(20, 89)
(20, 70)
(20, 79)
(20, 69)
(20, 67)
(20, 53)
(20, 90)
(20, 81)
(20, 94)
(20, 100)
(20, 90)
(20, 70)
(20, 70)
(20, 71)
(20, 83)
(20, 70)
(20, 84)
(20, 86)
(20, 66)
(20, 87)
(20, 70)
(20, 63)
(20, 69)
(20, 94)
(20, 58)
(20, 92)
(20, 83)

need help to solve this error

Upvotes: 0

Views: 119

Answers (1)

Promit Basak
Promit Basak

Reputation: 331

In MLP, the input size of all the samples must be the same. But in your case, the sample sizes are different which is not permitted. So you can do two things:

  1. Either truncate all the samples so that each sample has the size of the smallest sample.
  2. Zero-pad each sample so that each sample has the size of the largest sample.

Upvotes: 0

Related Questions