Python ValueError:找到样本数不一致的输入变量:[30583777]
代码如下:Python ValueError:找到样本数不一致的输入变量:[30583777],python,python-3.x,image-processing,scikit-learn,Python,Python 3.x,Image Processing,Scikit Learn,代码如下: import numpy as np import os import cv2 import matplotlib.pyplot as plt from os import listdir from pathlib import Path jpeg_images = list(Path(r'D:/ncfm/train').glob('**/*.jpg')) np.array([np.array(cv2.imread(str(file))).flatten() for file i
import numpy as np
import os
import cv2
import matplotlib.pyplot as plt
from os import listdir
from pathlib import Path
jpeg_images = list(Path(r'D:/ncfm/train').glob('**/*.jpg'))
np.array([np.array(cv2.imread(str(file))).flatten() for file in
jpeg_images])
folder = ['ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER', 'SHARK', 'YFT','test
images']
Path = r'D:\ncfm\train'
for i in range(9):
listing = os.listdir(Path+'/'+folder[i])
folder[i] = np.array([np.array(cv2.imread(Path+'/'+folder[i]+'/'+file)).flatten()for file in listing])
L.append(len(listing))
接下来,我尝试将其连接起来
M = np.concatenate((folders[1], folders[2], folders[3], folders[4],
folders[5], folders[6], folders[7], folders[8]))
接下来我做了标签
label = np.ones((3777), dtype=int)
label[0:1720]=1
label[1720:1920]=2
label[1920:2038]=3
label[2038:2104]=4
label[2104:2568]=5
label[2568:2868]=6
label[2868:3044]=7
label[3044:3777]=8
from sklearn.utils import shuffle
data,Label = shuffle(M, label, random_state = 2)
错误来了
ValueError Traceback (most recent call last)
<ipython-input-148-f7cec68b48c6> in <module>
1 from sklearn.utils import shuffle
2
----> 3 data,Label = shuffle(M, label, random_state = 2)
~\Anaconda3\lib\site-packages\sklearn\utils\__init__.py in
shuffle(*arrays, **options)
447 """
448 options['replace'] = False
--> 449 return resample(*arrays, **options)
450
451
~\Anaconda3\lib\site-packages\sklearn\utils\__init__.py in
resample(*arrays, **options)
330 n_samples))
331
--> 332 check_consistent_length(*arrays)
333
334 if stratify is None:
~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in
check_consistent_length(*arrays)
203 if len(uniques) > 1:
204 raise ValueError("Found input variables with inconsistent
numbers of"
--> 205 " samples: %r" % [int(l) for l in
lengths])
206
207
ValueError: Found input variables with inconsistent numbers of samples:
[3058, 3777]
ValueError回溯(最近一次调用)
在里面
1从sklearn.utils导入shuffle
2.
---->3数据,标签=shuffle(M,标签,随机状态=2)
~\Anaconda3\lib\site packages\sklearn\utils\\uuuuuu init\uuuuuuuuuu.py in
洗牌(*数组,**选项)
447 """
448选项['replace']=False
-->449返回重采样(*数组,**选项)
450
451
~\Anaconda3\lib\site packages\sklearn\utils\\uuuuuu init\uuuuuuuuuu.py in
重新采样(*数组,**选项)
330 n_(样本)
331
-->332检查长度是否一致(*数组)
333
334如果“分层”为“无”:
中的~\Anaconda3\lib\site packages\sklearn\utils\validation.py
检查长度是否一致(*数组)
203如果len(uniques)>1:
204 raise VALUERROR(“发现输入变量不一致
“的数目”
-->205“样本:%r”%[int(l)表示l英寸
长度])
206
207
ValueError:找到样本数不一致的输入变量:
[3058, 3777]
起初,我得到的长度是[83777]。在将RGB转换为灰度并调整大小后,我得到的长度是[30583777]。我想同时洗牌矩阵M中的行和标签的行。
shuffle
以一致的方式排列相同长度的数组。这里M
的长度为8,label
的长度为3777。您想发生什么?仍然不太清楚。我建议您尝试问另一个问题。这里的问题是,怎么回事r、 shuffle
以一致的方式排列相同长度的数组。此处M
的长度为8,label
的长度为3777。您希望发生什么?仍然不太清楚。我建议您尝试问另一个问题。但是,此处的问题已在上面的注释中解释。