ValueError:使用GaussianNB在scikit-learn(sklearn)中使用序列设置数组元素 [英] ValueError: setting an array element with a sequence in scikit-learn (sklearn) using GaussianNB

查看:322
本文介绍了ValueError:使用GaussianNB在scikit-learn(sklearn)中使用序列设置数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作sklearn图像分类器,但无法将数据放入分类器中.

I am trying to make a sklearn image classifier but I am unable to fit the data into a classifier.

x_train = np.array(im_matrix)
y_train = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
clf = GaussianNB()
clf.fit(x_train, y_train)

clf.fit(x_train, y_train)处出现以下错误:

ValueError:设置具有序列的数组元素.

ValueError: setting an array element with a sequence.

im_matrix是一个保存图像矩阵的数组:

im_matrix is an array holding image matrices:

for file in files:
        path = os.path.join(root, file)
        im_matrix.append(mpimg.imread(path))

x_train的

形状是(10,1) y_train的形状是(10,)

shape of x_train is (10, 1) shape of y_train is (10,)

我猜想问题出在x_train的形状怪异:

I am guessing the problem is with the x_train as its weirdly shaped:

array([array([[[227, 255, 233],
        [227, 255, 233],
        [227, 255, 233],
        ...,
        [175, 140, 160],
        [175, 140, 160],
        [175, 140, 160]],

       [[227, 255, 233],
        [227, 255, 233],
        [227, 255, 233],
        ...,
        [174, 139, 159],
        [174, 139, 159],
        [174, 139, 159]],

       [[227, 255, 233],
        [227, 255, 233],
        [227, 255, 233],
        ...,
        [173, 138, 158],
        [173, 138, 158],
        [173, 138, 158]],

       ...,

       [[199, 222, 253],
        [121, 142, 169],
        [ 13,  34,  55],
        ...,
        [ 31,  40,  49],
        [ 31,  40,  49],
        [ 32,  41,  50]],

       [[187, 206, 246],
        [ 80,  98, 134],
        [  0,  13,  41],
        ...,
        [ 36,  44,  63],
        [ 35,  43,  62],
        [ 35,  43,  62]],

       [[187, 206, 246],
        [ 80,  98, 134],
        [  0,  13,  41],
        ...,
        [ 36,  44,  63],
        [ 35,  43,  62],
        [ 35,  43,  62]]], dtype=uint8),

在这里已被多次询问,但我找不到解决方案.任何帮助将不胜感激

This has been asked here several times, but I could not find a solution. Any help would be appreciated

推荐答案

大多数(如果不是全部)scikit-learn函数期望将形状为(n_samples, n_features)2D array作为输入X.

Most (if not all) scikit-learn functions expect as input X, a 2D array with shape (n_samples, n_features).

请参阅文档: http://scikit-learn.org/stable/modules/generation/sklearn.naive_bayes.GaussianNB.html#sklearn.naive_bayes.GaussianNB.fit

根据X,y拟合高斯朴素贝叶斯

Fit Gaussian Naive Bayes according to X, y

参数:X:类似数组,形状(n_samples,n_features)

Parameters: X : array-like, shape (n_samples, n_features)

训练向量,其中n_samples是样本数, n_features是要素的数量.

Training vectors, where n_samples is the number of samples and n_features is the number of features.

要解决您的问题,请使用图像的矢量表示形式,然后将每个矢量作为一行放置在矩阵中.

To solve your problem, use a vector representation of the images and then put each vector as a row in your x_train matrix.

最后,使用此X来表示GaussianNB .

Finally, use this X for the fitting of the GaussianNB.

如何矢量化图像?

使用类似这样的内容:

import numpy as np
from PIL import Image

img = Image.open('orig.png').convert('RGBA')
arr = np.array(img)


# make a 1-dimensional view of arr
flat_arr = arr.ravel()

这篇关于ValueError:使用GaussianNB在scikit-learn(sklearn)中使用序列设置数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆