How to Solve DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueErr…

Background: to write a few lines of script in Spyder, the purpose is to apply sklearn’s own SVM (support vector machine) algorithm to classify its own digits (handwritten digits) data set, including the training stage and prediction stage. The feature data of handwritten numeral data and the penultimate sample point of label data target are taken as test sample points, and the rest are taken as training samples

Problem Description: depredationwarning: passing 1D arrays as data is predicted in 0.17 and will raise valueerror in 0.19. Reshape your data either using x.reshape (- 1,1) if your data has a single feature or x.reshape (1, – 1) if it contains a single sample. Depredationwarning)

the problem code is as follows:

correction method:

The first one: the 17th line is changed to a = CLF. Predict ([digits. Data [- 1]]), that is to say, there is more than one ‘[]’ (square brackets) on the sample data to be tested

The second method is to reshape the sample data in 17 rows

summary:

We can take a look at where the initial error is, and print out the input data in CLF. Predict (*) in three pieces of code with the following code:

The results are as follows: the data contents of the three are consistent, they all contain 64 numbers, but their forms are different

It can be seen from the following that the first type of test data is a one-dimensional numpy.ndarray data type with size of 64, the second type is a list (list, container), which contains a two-dimensional numpy.ndarray data with size of 1×64, and the third type is a two-dimensional numpy.ndarray data type with size of 1×64. Therefore, we can conclude that the data passed to CLF. Predict() should be a two-dimensional array, because obviously each sample has 64 features, which should be passed into CLF. Predict() as a whole. The first test data can only be passed in by a number of 0. (the first element), which obviously can not predict the result (the last sentence is personal understanding, which may not be correct)

 

Similar Posts: