预期的 2D 阵列,得到 1D 阵列,而不是使用 SciKit 进行图像分类

Expected 2D array, got 1D array instead using SciKit for image classification

提问人:Ginger 提问时间:10/2/2023 最后编辑:Matt HallGinger 更新时间:10/3/2023 访问量:60

问:

我正在尝试使用 .我的数据库包含 656 张图像以及来自 csv 文件的标签。下面是我用来导入图像 + 标签以及调用/创建模型的代码。scikit-learn

from numpy.core.multiarray import ndarray
from os.path import join
from glob import glob
import pandas as pd

IMG_DIR = "/content/drive/MyDrive/allddi"
img_paths = list(sorted(glob(join(IMG_DIR, "*.png"))))
def read_img(path):
    img = IMG_DIR + "/" + path
    return img

imgs = [read_img(p) for p in img_paths]
df = pd.read_csv("/content/drive/MyDrive/ddi.images/ddi_metadata.csv", sep=",")
labels = list(sorted(df.sort_values(by="DDI_file")["malignant"].tolist()))

len(labels) == len(img_paths)

用于创建模型的代码

from os import spread
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics

X = imgs
Y = np.array(labels)
model = GaussianNB()
X_train, X_test, Y_train, Y_test = train_test_split(X,Y)
model.fit(X_train, Y_train)

我不断得到的错误是.我尝试了各种重塑(-1,1),反之亦然,似乎没有什么能让我克服这个错误。我也尝试过简单地创建 Y 变量,但这也不起作用。expected a 2D array, got a 1D arrayY = labels

Python 数组 pandas numpy scikit-learn

评论

2赞 rr_goyal 10/2/2023
你不是一个图像,而只是一个.您可以使用 opencv 将图像作为数组读取,然后将此图像数组传递给您的模型。actually readinglist of the image paths

答: 暂无答案