【问题标题】:How to plot training data and training target for SVC如何为 SVC 绘制训练数据和训练目标
【发布时间】:2020-09-10 13:46:38
【问题描述】:

在加载图像并提取其关键点之后,我正在尝试通过绘制数据来可视化我的数据。但是,当我尝试分散它时,在绘图之前,我得到错误我的 x 和 y 值应该是相同的大小。

这是我从加载我自己的数据集得到的:

数据:

[[0.06887255 0.06887255 0.06887255 ... 0.08921569 0.08921569 0.08921569]
 [0.06666667 0.06666667 0.06666667 ... 0.08823529 0.08823529 0.08823529]
 [0.07598039 0.07598039 0.07598039 ... 0.08848039 0.08848039 0.08848039]
 ...
 [0.01568627 0.01568627 0.01568627 ... 0.1129902  0.1129902  0.1129902 ]
 [0.01666667 0.01666667 0.01666667 ... 0.1129902  0.1129902  0.1129902 ]
 [0.01666667 0.01666667 0.01666667 ... 0.1129902  0.1129902  0.1129902 ]]

目标:[0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4]

复制代码

 import matplotlib.pyplot as plt
 import numpy as np
 import skimage
 import cv2
 from skimage.io import imread
 from skimage.transform import resize

DATADIR = "C:/Dataset"
CATEGORIES = ["class 1", "class 2", "class 3", "class 4", "class 5"]
def load_image_files(fullpath, dimension=(35, 35)):
    descr = "A image classification dataset"
    flat_data = []
    target = []
    images = []
    dimension=(64, 64)
    for category in CATEGORIES:
        path = os.path.join(DATADIR, category)
        for person in os.listdir(path):
            personfolder = os.path.join(path, person)
            for imgname in os.listdir(personfolder):
                class_num = CATEGORIES.index(category)
                fullpath = os.path.join(personfolder, imgname)
                imageList = skimage.io.imread(fullpath)
                orb = cv2.ORB_create(edgeThreshold=1)
                orb = cv2.ORB_create()
                key_points = [cv2.KeyPoint(65, 9, 10), cv2.KeyPoint(66, 40, 10),cv2.KeyPoint(49, 200, 10), cv2.KeyPoint(76, 187, 10), cv2.KeyPoint(139, 188, 10), cv2.KeyPoint(176, 200, 10)]
                kp, des = orb.compute(imageList, key_points)
                kparray = cv2.drawKeypoints(imageList, kp, None, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
                img_resized = resize(kparray, dimension, anti_aliasing=True, mode='reflect')
                img_resized = img_resized.flatten()

                flat_data.append(img_resized)
                images.append(flat_data)
                target.append(class_num)

    flat_data = np.array(flat_data)
    target = np.array(target)
    images = np.array(images)
    return Bunch(data=flat_data,
                     target=target,
                     target_names=CATEGORIES,
                     images=images,
                     DESCR=descr)

值得注意的是,在每个班级里面,都有一个John Doe的文件夹,基本上是Class 1/John Doe,Class 2/John Doe,Class 3/John Doe,Class 4/John Doe,Class 5/John Doe,每个图像中的任何图像。

我正在加载这样的图像:image_dataset = load_image_files("C:\Dataset")

然后我尝试简单地分散和绘图:

plt.scatter(image_dataset.data, image_dataset.target)
plt.show()

错误提示 x 和 y 的大小应该相同。

我想知道如何将我的数据(x 值)调整为我的类值(y 值)

【问题讨论】:

    标签: python numpy opencv matplotlib computer-vision


    【解决方案1】:

    所以这里 plt.scatter() 需要在 XY 2D 平面上绘制的每个点的 X 和 Y 值。每个点代表数据集中的一个样本。所以 plt.scatter() 为 X 和 Y 值采用一维数组或列表。

    EX: 
    X = [1,2,3,4] , Y = [4,2,1,4]
    Dot1 => 1,4
    Dot2 => 2,2
    .
    .
    .
    

    这里的 image_dataset.data 实际上是扁平图像像素值的列表。这是一个二维数据,用它来绘制散点图没有任何意义。另外,您正试图将其作为 X 轴坐标传递。我不明白你想用它来完成什么。

    如果您尝试绘制图像并说明它们的类,您应该尝试使用 plt.plot(..) 绘制子图。您可以找到文档here。

    编辑#1: 你可以试试:

    image_dataset.target = [label for label in image_dataset.target
                for i in range(image_dataset.data.shape[0])]
    image_dataset.data = image_dataset.data.flatten()
    

    这里我们首先将 image.data 从 2D 展平为 1D。然后我们根据像素数据的长度创建标签。因此,我们将第一张图像中的每个像素标记为 1。

    这是一个最小的例子:

    X = np.array([[1,2,3],[1,2,3],[1,2,3],[1,2,3]])
    Y = np.array([1,1,2,2])
    
    Y = [label for label in Y
                for i in range(X.shape[1])]
    X = X.flatten()
    
    len(X)
    >>12
    len(Y)
    >>12
    

    【讨论】:

    • 只有一个问题,为什么要再次展平数据?我在函数内部这样做,我在那部分感到困惑。我的函数基本上返回类别,应该是作为目标的标签,以及数据,以便澄清,我从关键点中提取特征并将其用于分类。我尝试运行它给我的代码:“IndexError:元组索引超出范围”。重要的是要注意我的代码的作用:它在图像上设置 22 个点,将它们的特征提取到 22 维向量,我想在其中加载它们进行分类。
    • 好吧,由于变量的名称,我可能已经在这里用您的代码得出结论。你能告诉我 image_dataset.data 和它的形状到底是什么吗?我假设 image_dataset.data 是一个由多个 1D 数组组成的 2D 数组。
    • 绝对没问题。所以,基本上我得到了一个包含数组的字典,它由 Bunch(data=flat_data, target=target, target_names=CATEGORIES, images=images, DESCR=descr) 返回。 img_resized 或 image.append 的实际形状为 (12288,)。鉴于 img_resized 在字典内。现在,>>实际的 image_dataset.data.shape 是 (25, 12288)
    • 如果 image_dataset.data.shape 为 (25, 12288),则不能将其作为 plt.scatter() 的 X 轴变量传递。 image_dataset.data 需要是 1D 才能使用它来绘制散点图。我对我的代码进行了更改,您可以再试一次吗?
    • 按照你现在给出的代码:ValueError: x and y must be the same size
    猜你喜欢
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 2014-07-22
    • 2014-11-03
    • 2019-05-01
    • 2022-11-17
    • 1970-01-01
    相关资源
    最近更新 更多