簡體   English   中英

如何修復numpy數組的錯誤維度

[英]how to fix the incorrect dimension of numpy array

我正在使用監督機器學習解決二值圖像分類問題。 我使用了 svm 分類器算法。 首先,我為變量 X 中的標准化彩色圖像創建了一個 numpy 數組,其形狀為 (17500,32,32,3)。 然后在數據拆分之后,X_train 具有形狀 (14000,32,32,3) 和維度 4,y_train 具有形狀 (14000,2) 和維度 2。

clf.fit(X_train,y_train)

運行此代碼后,我收到一個值錯誤:發現 4 維估計器數組的維數 <= 2。

提前致謝!

如果您使用 scikit-learn SVM 分類算法,它需要形狀(n_samples, n_features)二維數組作為SVM 擬合函數的訓練數據集。

您傳入的數據集是一個 4D 數組,因此您需要將數組重新整形為 2D 數組。

例子:

from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

# To apply a classifier, we need to flatten the image, to
# turn the data in a (samples, feature) matrix, 
# assuming data is numpy array of shape (17500, 32, 32, 3), convert to shape (17500, 3072).
n_samples = len(data)
data_reshape = data.reshape((n_samples, -1))

# Split data into train and test subsets
X_train, X_test, y_train, y_test = train_test_split(data_reshape, labels, 
                                                    test_size=0.2)
clf.fit(X_train,y_train)

該技術稱為降維 將數據從高維空間映射到低維空間。 最常用的技術是主成分分析(PCA) 您可以通過以下鏈接了解它們:

暫無
暫無

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

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