Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用scikit learn分割阵列后,图像会失真_Python_Numpy_Image Processing - Fatal编程技术网

Python 使用scikit learn分割阵列后,图像会失真

Python 使用scikit learn分割阵列后,图像会失真,python,numpy,image-processing,Python,Numpy,Image Processing,我有一个.mat文件,其中包含60024*21图像数组。因此,得到的尺寸是(24,21,600)。以下代码将在子图中显示图像: X = loadmat('../input/data.mat')['face'] # generate dummy labels for this example y = np.concatenate([np.zeros(300), np.ones(300)]) # X.shape == (24, 21, 600) # y.shape == (600,) fig,

我有一个
.mat
文件,其中包含
600
24*21
图像数组。因此,得到的尺寸是
(24,21,600)
。以下代码将在子图中显示图像:

X = loadmat('../input/data.mat')['face']
# generate dummy labels for this example
y = np.concatenate([np.zeros(300), np.ones(300)])

# X.shape == (24, 21, 600)
# y.shape == (600,)

fig, ax = plt.subplots(4,5)

for i, axis in enumerate(ax.flat):
    axis.imshow(X[:,:,i], cmap='bone')
    axis.set(xticks=[], yticks=[])
那很好。问题是当我尝试使用以下方法将数据拆分为列车和测试数据时:

X_train, X_test, y_train, y_test = train_test_split(
    X, y,
    random_state=42,
    test_size=0.2,
    shuffle=True
)
出现以下错误:

ValueError: Found input variables with inconsistent numbers of samples: [24, 600]
我知道错误在于我的
X
y
的第一维度不匹配。但是,如果我尝试使用
X=X来重塑
X
。重塑(600,24,21)
,使
X.shape==(600,24,21)
,这些形状是固定的,我可以使用
train\u test\u split()成功分割数据。

但是,现在当我尝试使用下面的代码制作子图时:

fig, ax = plt.subplots(4,5)

for i, axis in enumerate(ax.flat):
    axis.imshow(X[i,:,:], cmap='bone')
    axis.set(xticks=[], yticks=[])
这些图像毫无意义,因此被改变了。我无法使用这些图像制作分类器。有人能帮我吗

  • 在最好的情况下,如何拆分数据而不必重塑形状
  • 在重塑图像并成功分割数据后,如何不扭曲图像

  • 问题是采样轴位于位置2而不是0。Sklearn然后认为您有24个形状样本
    (21600)
    。 您希望
    X.shape
    成为
    (600,24,21)
    ,而不是
    (24,21,600)

    因此,您可以使用更改位置:

    X = np.rollaxis(X, 2, 0)
    

    虽然我使用了
    X=X.transpose([2,0,1])
    ,但您的解决方案可能也会起作用。事实上,我的问题是一个重复的问题。