【问题标题】:ValueError: Found array with dim 3. Estimator expected <= 2ValueError:找到暗淡为 3 的数组。估计器预期 <= 2
【发布时间】:2016-11-28 07:45:33
【问题描述】:

我正在尝试为识别问题生成自己的训练数据。我有两个文件夹s0s1,包含的文件夹是dataimageslables 是其中labels 包含文件夹名称的两个列表。

|—- data
|    |—- s0
|    |    |—- 1.pgm
|    |    |—- 2.pgm
|    |    |—- 3.pgm
|    |    |—- 4.pgm
|    |    |—- ...
|    |—- s1
|    |    |—- 1.pgm
|    |    |—- 2.pgm
|    |    |—- 3.pgm
|    |    |—- 4.pgm
|    |    |—- ...

以下是代码,它在classifier.fit(images, lables) 行显示错误

 Traceback (most recent call last):
File "mint.py", line 34, in <module>
 classifier.fit(images, lables)
 File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py",  line 150, in fit
  X = check_array(X, accept_sparse='csr', dtype=np.float64, order='C')
 File "/usr/local/lib/python2.7/dist-   packages/sklearn/utils/validation.py", line 396, in check_array
  % (array.ndim, estimator_name))

ValueError:找到暗淡为 3 的数组。估计器预期

import os,sys
import cv2
import numpy as np
from sklearn.svm import SVC
fn_dir ='/home/aquib/Desktop/Natural/data'

# Create a list of images and a list of corresponding names
(images, lables, names, id) = ([], [], {}, 0)
for (subdirs, dirs, files) in os.walk(fn_dir):
    for subdir in dirs:
      names[id] = subdir
      mypath = os.path.join(fn_dir, subdir)
      for item in os.listdir(mypath):
        if '.png' in item:  
        label=id
        image = cv2.imread(os.path.join(mypath, item),0)
        r_image = np.resize(image,(30,30))
        if image is not None:
            images.append(r_image)
            lables.append(int(label))
      id += 1  
#Create a Numpy array from the two lists above
(images, lables) = [np.array(lis) for lis in [images, lables]]
classifier = SVC(verbose=0, kernel='poly', degree=3)
classifier.fit(images, lables)

我真的不明白如何在二维中纠正它。 我正在尝试以下代码,但错误是相同的: images = np.array(images) im_sq = np.squeeze(images).shape images = images.reshape(images.shape[:2])

【问题讨论】:

  • images.append(cv2.imread((path, 0)) 我数了 3 个开括号和 2 个闭括号。
  • 是的,我成功了。但新的错误是TypeError: expected string or Unicode object, tuple found

标签: python-2.7 image-processing machine-learning


【解决方案1】:

您的代码最后一行images.append(cv2.imread((path, 0)) 存在语法错误。括号没有正确关闭。所以应该是这样的 images.append(cv2.imread((path, 0))) 。此外,发布错误的回溯总是一件好事,这样任何人都可以轻松回答。

【讨论】:

  • 是的,谢谢...但回溯显示在下一行...这就是为什么我每个人都在寻找同一行...。我这样做了,但我得到了其他@ 987654323@
猜你喜欢
  • 2019-06-01
  • 2021-04-16
  • 2019-07-19
  • 2018-01-16
  • 2020-07-10
  • 2022-01-16
  • 2016-04-30
  • 1970-01-01
  • 2016-04-24
相关资源
最近更新 更多