Why am I getting a data conversion warning?

I am a relative newbie in this area, so I would appreciate your help. I play with the mnist dataset. I took the code http://g.sweyla.com/blog/2012/mnist-numpy/ , but changed the "images" to 2 sizes so that each image is a sign of the function. Then I ran PCA on the data and then SVM and checked the score. Everything seems to be working fine, but I get the following warning, and I'm not sure why.

"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()." 

I have tried several things but cannot get rid of this warning. Any suggestions? Here is the complete code (ignore the missing padding, it looks like they messed up the code a bit here):

 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 

Thanks for the help!

+5
source share
1 answer

I think scikit-learn expects y to be a 1-D array. Your variable labels is 2-D - labels.shape is (N, 1). The warning tells you to use labels.ravel() , which will turn labels into a one-dimensional array with form (N).
Reform will also work: labels=labels.reshape((N,))
Think about it, so the call compresses: labels=labels.squeeze()

I assume that here it turns out that in numpy 1-D the array is different from a two-dimensional array with one of its sizes equal to 1.

+4
source

Source: https://habr.com/ru/post/1238530/


All Articles