将Sklearn与NumPy和图像一起使用并获得此错误';使用序列设置数组元素';

将Sklearn与NumPy和图像一起使用并获得此错误';使用序列设置数组元素';,numpy,scikit-learn,python-imaging-library,sklearn-pandas,Numpy,Scikit Learn,Python Imaging Library,Sklearn Pandas,我正在尝试创建一个简单的图像分类工具 我希望下面的代码能够对图像进行分类。当它是非图像NumPy数组时,它可以正常工作 #https://e2eml.school/images_to_numbers.html import numpy as np from sklearn.utils import Bunch from PIL import Image monkey = [1] dog = [2] example_animals = Bunch(data = np.array([monke

我正在尝试创建一个简单的图像分类工具

我希望下面的代码能够对图像进行分类。当它是非图像NumPy数组时,它可以正常工作

#https://e2eml.school/images_to_numbers.html
import numpy as np
from sklearn.utils import Bunch
from PIL import Image
 
monkey = [1]
dog = [2]

example_animals = Bunch(data = np.array([monkey,dog]),target = np.array(['monkey','dog']))
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=2) #with KMeans you get to pre specify the number of Clusters
KModel = kmeans.fit(example_animals.data) #fit a model using the training data , in this case original example animal data passed through
import pandas as pd
crosstab = pd.crosstab(example_animals.target,KModel.labels_)
print(crosstab)
我在
下面我将图像转换为NumPy数组的代码不起作用。 当运行时,它会出现以下错误

**'使用序列设置数组元素'**

#https://e2eml.school/images_to_numbers.html
import numpy as np
from sklearn.utils import Bunch
from PIL import Image
    
monkey = np.asarray(Image.open("monkey.jpg"))
dog = np.asarray(Image.open("dog.jpeg"))

example_animals = Bunch(data = np.array([monkey,dog]),target = np.array(['monkey','dog']))
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=2) #with KMeans you get to pre specify the number of Clusters
KModel = kmeans.fit(example_animals.data) #fit a model using the training data , in this case original example animal data passed through
import pandas as pd
crosstab = pd.crosstab(example_animals.target,KModel.labels_)
print(crosstab)

我希望了解如何修复错误“使用序列设置数组元素”,以便图像与sklearn处理兼容。

我找到了错误解决方案使用序列设置数组元素 Kmeans要求用于比较的数据数组大小必须相同。 这意味着如果导入图片,图片需要调整大小,转换成numpy数组(一种与Kmeans兼容的格式),最后生成一维数组

#https://e2eml.school/images_to_numbers.html
#https://machinelearningmastery.com/how-to-load-and-manipulate-images-for-deep-learning-in-python-with-pil-pillow/

import numpy as np
from matplotlib import pyplot as plt
from sklearn.utils import Bunch
from PIL import Image
from sklearn.cluster import KMeans
import pandas as pd

monkey = Image.open("monkey.jpg")
dog = Image.open("dog.jpeg")

#resize pictures
monkey1 = monkey.resize((180,220))
dog1 = dog.resize((180,220))

#make pictures into numpy array
monkey2 = np.asarray(monkey1)
dog2 = np.asarray(dog1)

#https://www.quora.com/How-do-I-convert-image-data-from-2D-array-to-1D-using-python
#make numpy array into 1 dimensional array
monkey3 = monkey2.reshape(-1) 
dog3 = dog2.reshape(-1) 

example_animals = Bunch(data = np.array([monkey3,dog3]),target = np.array(['monkey','dog']))
kmeans = KMeans(n_clusters=2) #with KMeans you get to pre specify the number of Clusters
KModel = kmeans.fit(example_animals.data) #fit a model using the training data , in this case original example food data passed through
crosstab = pd.crosstab(example_animals.target,KModel.labels_)
print(crosstab)

您需要确保图像“monkey.jpg”和“dog.jpeg”的像素数相同。否则,您必须调整图像的大小,使其具有相同的大小。此外,束对象的数据需要具有形状(n_样本,n_特征)(您可以查看文档)

您需要知道您使用的是未服务的学习模型(Kmeans)。因此,模型的输出不是直接的“猴子”或“狗”