在sklearn.kneighborsclassifier中,我有错误:发现dim 4的数组。估算器预期< = 2

发布于 2025-02-06 18:14:02 字数 683 浏览 3 评论 0 原文

我对为图像进行分类的程序有问题。我将图像存储在

arr = np.concatenate((X1, Y1))

具有大小的阵列中:(80,128,128,3) 我还有另一个:

arr2 = np.concatenate((X2, Y2))

有大小:(20、128、128、3) 我将使数组进入培训数据。并创建目标:

a= np.full((1, 40), 1)
b= np.full((1, 40), 2)
arr3 = np.concatenate((a, b))

并将其设置为KNN算法

knn=KNeighborsClassifier(n_neighbors=3) #define K=3
knn.fit(arr,arr3)
res = knn.predict(arr2)
print(res)

,我没有得到结果,并且存在错误:找到带有dim 4的数组。估算器预期< = 2。

我还尝试重新设计ARR和ARR2:

arr5 = arr.reshape(-1,1)
arr6 = arr2.reshape(-1,1)

但是也遇到了错误:发现的输入变量与样本数量不一致,

我需要您的意见来解决问题。

I have a problem with the program I created to classify images. I store my image in an array

arr = np.concatenate((X1, Y1))

where has size : (80, 128, 128, 3)
and i have another one :

arr2 = np.concatenate((X2, Y2))

where has size : (20, 128, 128, 3)
I will make array into training data. and create target :

a= np.full((1, 40), 1)
b= np.full((1, 40), 2)
arr3 = np.concatenate((a, b))

and set into knn algorithm

knn=KNeighborsClassifier(n_neighbors=3) #define K=3
knn.fit(arr,arr3)
res = knn.predict(arr2)
print(res)

I don't get results and there is an error: Found array with dim 4. Estimator expected <= 2.

I've also tried to reshape arr and arr2 :

arr5 = arr.reshape(-1,1)
arr6 = arr2.reshape(-1,1)

but also getting errors: Found input variables with inconsistent numbers of samples

I need your opinion about this to fix the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑凤梨 2025-02-13 18:14:02

您可以阅读 sklearn.neighbors.kneighborsclassifier 和 .fit(x,y)应该具有以下形状:

fit(x,y):适合培训数据集的K-Nearest邻居分类器。

参数:
如果metric ='预计'
培训数据。
y {类似零件的稀疏矩阵}(n_samples,)或(n_samples,n_outputs)
目标值。

因此,您需要重塑 arr或x_train喜欢(80,128*128*3) and arr2或x_test like(20,128*128*3) and> y或arr3喜欢(80)

(对于此重塑,我们可以使用`numpy.reshape(-1)如下。)

from sklearn.neighbors import KNeighborsClassifier
import numpy as np

arr  = np.random.rand(80,128,128,3)
arr2 = np.random.rand(20,128,128,3)

a= np.full((1, 40), 1)
b= np.full((1, 40), 2)
arr3 = np.concatenate((a, b))

knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(arr.reshape(80,-1), arr3.reshape(-1))

res = knn.predict(arr2.reshape(20,-1))
print(res)

输出:

[2 1 1 2 2 1 1 1 2 1 2 1 1 2 2 2 2 1 1 1]

You can read here, For using sklearn.neighbors.KNeighborsClassifier and .fit(X,y) should have shape like :

fit(X, y): Fit the k-nearest neighbors classifier from the training dataset.

Parameters :
X{array-like, sparse matrix} of shape (n_samples, n_features) or (n_samples, n_samples) if metric=’precomputed’
Training data.
y{array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_outputs)
Target values.

For this reason, you need to reshape arr or x_train like (80, 128*128*3) and arr2 or x_test like (20, 128*128*3) and y or arr3 like (80):

(for this reshaping we can use `numpy.reshape(-1) like below.)

from sklearn.neighbors import KNeighborsClassifier
import numpy as np

arr  = np.random.rand(80,128,128,3)
arr2 = np.random.rand(20,128,128,3)

a= np.full((1, 40), 1)
b= np.full((1, 40), 2)
arr3 = np.concatenate((a, b))

knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(arr.reshape(80,-1), arr3.reshape(-1))

res = knn.predict(arr2.reshape(20,-1))
print(res)

Output:

[2 1 1 2 2 1 1 1 2 1 2 1 1 2 2 2 2 1 1 1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文