Tag Archives: ValueError

ValueError: Found array with dim 4. Estimator expected and ValueError: Expected 2D array, got 1D array i

NumPy array is reduced or increased in dimension in Python 3
Resolve to report errors such as:
1.ValueError: Found array with dim 4. Estimator expected
2.ValueError: Expected 2D array, got 1D array instead:
Error 1 valueerror: Found array with dim 4. Estimator expected – solution: use the
np. Concatenate

Function model: Concatenate ((A1, A2…)) , axis=0)

• Passed Parameters (A1, A2, A3…) Must be a multiple of the array a tuple or list
also need to specify the stitching direction, the default axis = 0, that is an array of 0 axis (X/or line) object for joining together to get a combination of longitudinal array, (opposite the axis = 1); Note: In general, Axis = 0 is an operation on the array along this axis, and the direction of operation is another axis, namely Axis =1.

import numpy as np
a = np.array([[1,2],[2,3]])
b = np .array([[4,5],[3,4]])
print(np.concatenate((a, b), axis=0))

print(np.concatenate((a), axis=0))

Output :(This will reduce the dimensionality of the array (strip out a set of brackets []))

[[1 2]
 [2 3]
 [4 5]
 [3 4]]
 
[1 2 2 3]

Refer to the link: https://blog.csdn.net/brucewong0516/article/details/79158758
Error 2 function fit when ValueError: Expected a 2 d array, got home 1 d array: – solution:
here I will function when an error code fragment interception, the specific function of the data is not intercept method
1: use the brackets [] :
the original code:

import numpy as np  
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()  
knn.fit(x,y)                 
x_new = [50000,8,1.2]
y_pred = knn.predict(x_new)

An error:

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

NP.array. Reshape

x_new = np.array([50000,8,1.2]).reshape(1,-1)

Reshape (1,-1) : reshape(1,-1);

x_new = np.array([[50000,8,1.2]])

In the Python 3 version of Sklearn, all data should be two-dimensional matrices, that is, np.array() should contain at least two pairs of brackets [].

ValueError: need more than 1 value to unpack

class Person():

def __init__(self, Newname, Newage, Newtype):

self. type = Newtype,self.name = Newname,self. age = Newage,

This is the wrong way to write, not on the same line!

#     def __init__(self, Newname,Newage,Newtype):

#         self.name = Newname,

#         self. age = Newage,

#         self. type = Newtype,

from django. http import  HttpResponse
from django. template import Template ,Context
import datetime
from HelloDjango.Model. Person import *

def Hello(request , offset ):

tempStr = “{{person.name}} {{person. age}} {{person. type}}”
tempAnswer = {“person” : Person(“hjc”, “11”, “man”)}
#
#     tempStr = “{{person.name}} {{person. age}} {{person. type}}”
#     tempAnswer = {“person” : Person(‘hjc’ , ’11’ ,’man’)}

t = Template(tempStr)
c = Context(tempAnswer)
return HttpResponse(t.render(c))