Tag Archives: ValueError: bad input shape ()

Tag code error valueerror: bad input shape()

In section 2.9 of classic examples of Python machine learning, you want to practice the quality of automobile feature evaluation yourself, so you need to preprocess the data. The code includes encoding the string mark into the corresponding number, as shown in the following code

input_data = ['vhigh', 'vhigh', '2', '2', 'small', 'low'] 
input_data_encoded = [-1] * len(input_data)
for i,item in enumerate(input_data):
    input_data_encoded[i] = int(label_encoder[i].transform(input_data[i]))

Error reporting:

Traceback (most recent call last):
  File "E:/17770426925/PythonLeaning/Machine-Learning/classifier/classifier.py", line 255, in <module>
    input_data_encoded[i] = int(label_encoder[i].transform(input_data[i]))
  File "D:\ProgramData\Anaconda3\lib\site-packages\sklearn\preprocessing\label.py", line 147, in transform
    y = column_or_1d(y, warn=True)
  File "D:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 562, in column_or_1d
    raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape ()

Therefore, it can be seen that it is label_encoder[i].transform(input_Input in data [i])_Data [i] is not in the correct form and needs to be changed to list, so the code can be improved:

for i, item in enumerate(input_data):
    labels=[]
    labels.append(input_data[i])
    input_data_encoded[i] = int(label_encoder[i].transform(labels))