我在尝试拟合 sklearn 模型时遇到错误。类型错误:只有 size-1 数组可以转换为 Python 标量


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_fileplants.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_filebase.py", line 596, in _validate_data
X, y = check_X_y(X, y, **check_params)
File "C:path_to_filevalidation.py", line 1074, in check_X_y
X = check_array(
File "C:path_to_filevalidation.py", line 856, in check_array
array = np.asarray(array, order=order, dtype=dtype)
ValueError: setting an array element with a sequence.

我对这个错误的含义感到困惑,不知道该怎么做。非常感谢任何帮助!

问题似乎出在X_train的形状上。X变换前的大小为:(number_of_samples, height, width)。MLPClassifier's拟合函数期望如下形状:(number_of_samples, number_of_features)。因此,您需要将2D图像重塑为1D向量(特征),例如,通过连接行,使输入X为:(number_of_samples, height x width)。

然而,在您的示例中,您使用reshape(-1, 1)转换X_train,导致以下形状:(number_of_samples x height x width)

说明性示例:假设您有两张大小为3x3的图像。目标是转换它们,使每个"图像"的大小为1x9而不是3x3。例如,您可以使用numpy的重塑方法:

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

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

我希望这对你有帮助。

最新更新