簡體   English   中英

如何為 SVC 提供 plot 訓練數據和訓練目標

[英]How to plot training data and training target for SVC

在加載圖像並提取其關鍵點之后,我試圖通過繪制它來可視化我的數據。 但是,當我嘗試分散它時,在繪圖之前,我得到錯誤我的 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)

Its worth noting that inside each class, there is a folder for John Doe, basically it goes Class 1/John Doe, Class 2/John Doe, Class 3/John Doe, Class 4/John Doe, Class 5/John Doe, with每個圖像中的任何圖像。

我像這樣加載圖像: image_dataset = load_image_files("C:\Dataset")

然后我嘗試簡單地分散和繪圖:

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

得到錯誤說 x 和 y 應該是相同的大小。

我想知道如何將我的數據(x 值)調整為我的類值(y 值)

所以這里 plt.scatter() 需要在 XY 2D 平面上繪制的每個點的 X 和 Y 值。 每個點代表數據集中的一個樣本。 所以 plt.scatter() 采用一維 arrays 或 X 和 Y 值的列表。

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

在這里,您的 image_dataset.data 實際上是扁平圖像像素值的列表。 這是一個 2D 數據,將其用於 plot 散點圖沒有任何意義。 另外,您正試圖將其作為 X 軸坐標傳遞。 我不明白你想用它來完成什么。

如果您嘗試 plot 圖像和 state 它們的類,您應該嘗試使用 plt.plot(..) 繪制子圖。 您可以在此處找到文檔。

編輯#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。 然后我們根據像素數據的長度創建標簽。 所以我們 label 來自第一張圖像的每個像素為 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM