【问题标题】:scikit fitting data errorscikit拟合数据错误
【发布时间】:2017-06-17 18:25:13
【问题描述】:

我正在尝试实现一种学习算法来预测图像的目标值是1 还是0。首先,我的目标值是这样设置的……

real = [1] * len(images)
fake = [0] * len(fake_images)

total_target = real + fake
total_target = numpy.array(total_target)

>>> [1 1 1 ... 0 0 0 0]

接下来,我将图像列表转换为 numpy 数组 numpy 数组。所以我将每个图像存储为numpy 数组...

training_set = []
for image in total_images:
    im = image.convert("L")
    dataset = numpy.asarray(im)
    training_set.append(dataset)
training_set = numpy.array(training_set)

所以training_set 保存图像。 training_set 的顺序对应total_target 的顺序,所以training_set 中的第一个图像对应total_target 中的第一个值,在上面的示例中为1

接下来我将训练集展平...

n_samples = len(training_set)
data = training_set.reshape((n_samples, -1))

现在我将它传递给以下...

classifier = svm.SVC(gamma=0.001)
classifier.fit(data[:n_samples-1], total_target[:n_samples-1])

我没有包括最后一张图片及其各自的值,因为这是我要预测的值...

expected = total_target[-1]
predicted = classifier.predict(data[-1])

当我运行所有这些时,我收到以下错误...

DeprecationWarning:将一维数组作为数据传递在 0.17 中已弃用,并将在 0.19 中引发 ValueError。如果您的数据具有单个特征,则使用 X.reshape(-1, 1) 或 X.reshape(1, -1) 如果它包含单个样本,则使用 X.reshape(1, -1) 重塑您的数据。 弃用警告)

好的,所以看起来我的total_target 格式错误,所以我添加以下内容...

total_target = numpy.array(total_target).reshape(-1, 1)

我运行它,现在我收到以下错误

DataConversionWarning:当需要一维数组时,传递了列向量 y。请将 y 的形状更改为 (n_samples, ),例如使用 ravel()。 y_ = column_or_1d(y, warn=True)

C:\Users\Eric\Anaconda2\lib\site-packages\sklearn\utils\validation.py:386: DeprecationWarning: Passing 1d arrays as data 在 0.17 中被弃用,并且会在 0.19 中引发 ValueError。如果您的数据具有单个特征,则使用 X.reshape(-1, 1) 或 X.reshape(1, -1) 如果它包含单个样本,则使用 X.reshape(1, -1) 重塑您的数据。 弃用警告)

我尝试在total_target 上使用ravel(),但这只是让我回到之前的错误。我认为我的格式错误我对numpy 数组很陌生。

【问题讨论】:

  • OK, so by the error it looks like my total_target is in the wrong format, - 不,scikit-learn 抱怨 data[-1] 是平面向量而不是二维数组。 total_target 应该是一个平面向量,它是,不需要改变它。

标签: python numpy machine-learning scikit-learn svm


【解决方案1】:

Numpy 的 atleast_2d 让代码工作。

让我们首先生成一些模拟数据,即 800 行 1200 列的 5 个真实和 5 个假 8 位图像:

In [111]: import numpy as np

In [112]: real, fake = 5, 5

In [113]: rows, cols = 800, 1200

In [114]: bits = 8

In [115]: target = np.hstack([np.ones(real), np.zeros(fake)])

In [116]: np.random.seed(2017)

In [117]: images = np.random.randint(2**bits, size=(real + fake, rows, cols))

In [118]: data = images.reshape(images.shape[0], -1)

In [119]: data
Out[119]: 
array([[ 59,   9, 198, ..., 189, 201,  38],
       [150, 251, 145, ...,  95, 214, 175],
       [156, 212, 220, ..., 179,  63,  48],
       ..., 
       [ 25,  94, 108, ..., 159, 144, 216],
       [179, 103, 217, ...,  92, 219,  34],
       [198, 209, 177, ...,   6,   4, 144]])

In [120]: data.shape
Out[120]: (10L, 960000L)

然后我们使用除最后一张以外的所有图像训练分类器:

In [121]: from sklearn import svm

In [122]: classifier = svm.SVC(gamma=0.001)

In [123]: classifier.fit(data[:-1], target[:-1])
Out[123]: 
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma=0.001, kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

如果您现在尝试通过classifier.predict(data[-1]) 对最后一张图像进行分类,sklearn 会抱怨。为了让 sklearn 满意,您只需要确保测试数据是二维的,如下所示:

In [124]: classifier.predict(np.atleast_2d(data[-1]))
Out[124]: array([ 1.])

【讨论】:

    猜你喜欢
    • 2018-05-26
    • 2012-07-27
    • 2020-10-05
    • 2015-10-25
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 2013-12-19
    相关资源
    最近更新 更多