python - 为什么我会收到数据转换警告?

标签 python scikit-learn warnings

我是这方面的新手,非常感谢您的帮助。 我正在玩弄 mnist 数据集。我从 http://g.sweyla.com/blog/2012/mnist-numpy/ 中获取了代码但将“图像”更改为二维,以便每个图像都是一个特征向量。然后我对数据运行 PCA,然后运行 ​​SVM 并检查分数。一切似乎都工作正常,但我收到以下警告,我不确定为什么。

"DataConversionWarning: A column-vector y was passed when a 1d array was expected.\
Please change the shape of y to (n_samples, ), for example using ravel()."

我已经尝试了几件事,但似乎无法摆脱这个警告。有什么建议么?这是完整的代码(忽略缺失的缩进,看起来他们在复制这里的代码时搞砸了):

import os, struct
from array import array as pyarray
from numpy import append, array, int8, uint8, zeros, arange
from sklearn import svm, decomposition
#from pylab import *
#from matplotlib import pyplot as plt

def load_mnist(dataset="training", digits=arange(10), path="."):
"""
Loads MNIST files into 3D numpy arrays

Adapted from: http://abel.ee.ucla.edu/cvxopt/_downloads/mnist.py
"""

    if dataset == "training":
        fname_img = os.path.join(path, 'train-images.idx3-ubyte')
        fname_lbl = os.path.join(path, 'train-labels.idx1-ubyte')
    elif dataset == "testing":
        fname_img = os.path.join(path, 't10k-images.idx3-ubyte')
        fname_lbl = os.path.join(path, 't10k-labels.idx1-ubyte')
    else:
        raise ValueError("dataset must be 'testing' or 'training'")

    flbl = open(fname_lbl, 'rb')
    magic_nr, size = struct.unpack(">II", flbl.read(8))
    lbl = pyarray("b", flbl.read())
    flbl.close()

    fimg = open(fname_img, 'rb')
    magic_nr, size, rows, cols = struct.unpack(">IIII", fimg.read(16))
    img = pyarray("B", fimg.read())
    fimg.close()

    ind = [ k for k in range(size) if lbl[k] in digits ]
    N = len(ind)

    images = zeros((N, rows*cols), dtype=uint8)
    labels = zeros((N, 1), dtype=int8)
    for i in range(len(ind)):
        images[i] = array(img[ ind[i]*rows*cols : (ind[i]+1)*rows*cols ])
        labels[i] = lbl[ind[i]]

    return images, labels

if __name__ == "__main__":
    images, labels = load_mnist('training', arange(10),"path...")
    pca = decomposition.PCA()
    pca.fit(images)
    pca.n_components = 200
    images_reduced = pca.fit_transform(images)
    lin_classifier = svm.LinearSVC()
    lin_classifier.fit(images_reduced, labels)
    images2, labels2 = load_mnist('testing', arange(10),"path...")
    images2_reduced = pca.transform(images2)
    score = lin_classifier.score(images2_reduced,labels2)
    print score

感谢您的帮助!

最佳答案

我认为 scikit-learn 期望 y 是一维数组。您的 labels 变量是二维的 - labels.shape 是 (N, 1)。该警告告诉您使用 labels.ravel(),这会将 labels 转换为一维数组,形状为 (N,)。
reshape 也将起作用:labels=labels.reshape((N,))
想想看,调用挤压也是如此:labels=labels.squeeze()

我想这里的陷阱是在 numpy 中,一维数组不同于其中一个维度等于 1 的二维数组。

关于python - 为什么我会收到数据转换警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34337093/

相关文章:

python - scikit-learn 中每个数据拆分的交叉验证指标

python - 错误: object of type 'numpy.float64' has no len()

python - django中的调度用于什么?

python - 如何使用 python 将下一个文本存储在 Telegram Bot 中

python - Eclipse IDE Pydev 导入错误

python-3.x - TypeError : iteration over a 0-d array, 使用 numpy

python - 在内置变量之后命名实例变量是一种不好的做法吗

javascript - react - 警告 : An update was scheduled from inside an update function

: is FrameLayout really useless? ArrayAdapter ListView的Android布局优化

php - 不应静态调用非静态方法 DB::connect()