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.
我对这个错误的含义感到困惑,不知道该尝试什么。希望得到帮助!
1条答案
按热度按时间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方法:
最后,请注意,每个图像必须具有相同的大小。使用不同的高度和宽度也会导致错误。
希望能帮上忙。