numpy 尝试拟合sklearn模型时出错,TypeError:只有大小为1的数组可以转换为Python标量

6tr1vspr  于 2022-11-24  发布在  Python
关注(0)|答案(1)|浏览(155)
from PIL import Image
import glob
import numpy as np
import matplotlib as plt
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split

X = []
y = []

classes = [r"Anthracnose", r"Leaf Crinkcle", r"Powdery Mildew", r"Yellow Mosaic", r"Healthy"]

for i in range(5):
    for filename in glob.glob(r"C:\-\-\-\-\-\-\\" + classes[i] + r"/*.jpg"):
        image = Image.open(filename)
        matrix_temp = np.array(image)
        X.append(matrix_temp)
        y.append(i)

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2)

X_train = np.array(X_train, dtype=object).reshape(-1,1)
y_train = np.array(y_train).reshape(-1,1)
X_test = np.array(X_test, dtype=object).reshape(-1,1)
y_test = np.array(y_test).reshape(-1,1)

model = MLPClassifier()
model.fit(X_train, y_train)

错误:

TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\path_to_file\plants.py", line 32, in <module>
    model.fit(X_train, y_train)
  File "C:\path_to_file\_multilayer_perceptron.py", line 762, in fit
    return self._fit(X, y, incremental=False)
  File "C:\path_to_file\_multilayer_perceptron.py", line 394, in _fit
    X, y = self._validate_input(X, y, incremental, reset=first_pass)
  File "C:\path_to_file\_multilayer_perceptron.py", line 1109, in _validate_input
    X, y = self._validate_data(
  File "C:\path_to_file\base.py", line 596, in _validate_data
    X, y = check_X_y(X, y, **check_params)
  File "C:\path_to_file\validation.py", line 1074, in check_X_y
    X = check_array(
  File "C:\path_to_file\validation.py", line 856, in check_array
    array = np.asarray(array, order=order, dtype=dtype)
ValueError: setting an array element with a sequence.

我对这个错误的含义感到困惑,不知道该尝试什么。希望得到帮助!

sqxo8psd

sqxo8psd1#

看起来问题出在X_train的形状上。X在变换之前的大小为:(number_of_samples,height,width)。MLPClassifier's拟合函数需要以下形状:(number_of_samples,number_of_features)。因此,需要通过例如连接行来将2D图像重新整形为1D向量(特征),使得输入X为:(* 样本数,高度x宽度 )。
但是,在您的示例中,您使用reshape(-1, 1)转换X_train,这将导致以下形状:(
样本数x高度x宽度 *)
示例:假设您有两个大小为3x3的图像,我们的目标是将它们转换为1x9而不是3x3。例如,您可以使用numpy的reshape方法:

import numpy as np
a = np.zeros((2, 3, 3))  # Shape is (2,3,3). It's like 2 images with size 3x3 each
a_reshaped = a.reshape((2, 9)). # Reshape the two images
print(a_reshaped)
a_reshaped_2 = a.reshape((2, -1)). # Same result as above
print(a_reshaped_2)
a_reshaped_wrong = a.reshape(-1, 1). # What you do
print(a_reshaped_wrong.shape())  # Check the size and see what's different

最后,请注意,每个图像必须具有相同的大小。使用不同的高度和宽度也会导致错误。
希望能帮上忙。

相关问题