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 [].
Read More:
- Python IndexError: too many indices for array: array is 1-dimensional, but 2 were i..
- [Solved] ValueError: Error when checking input: expected conv2d_input to have 4 dimensions
- Pandas Error: ValueError: setting an array element with a sequence.
- Plt.acorr() Function Error: ValueError: object too deep for desired array
- Pytorch ValueError: Expected more than 1 value per channel when training, got input size [1, 768
- numpy.AxisError: axis 1 is out of bounds for array of dimension 1
- [Solved] Python Error: object too deep for desired array.
- [How to Fix] TypeError: Cannot cast array data from dtype(‘float64‘) to dtype(‘<U32‘)….
- Python ValueError: only 2 non-keyword arguments accepted
- [Solved] Pytorch Error: RuntimeError: expected scalar type Double but found Float
- [Solved] Runtime error: expected scalar type Float but found Double
- [Solved] RuntimeError: expected scalar type Long but found Float
- Python Valueerror: cannot index with vector containing Na / Nan values
- Pytorch Error: runtimeerror: expected scalar type double but found float
- [resolution] str.contains() problem] valueerror: cannot index with vector containing Na/Nan values
- [Solved] PyInstaller Error: ValueError: too many values to unpack
- How to Solve RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu
- How to Solve Python Xlwt ValueError: More than 4094 XFs (styles)
- [Solved] ValueError: row index was 65536, not allowed by .xls format
- Python errors: valueerror: if using all scalar values, you must pass an index (four solutions)