# Valueerror: Expected 2d Array, Got 1d Array Instead

Fariba Laiq Aug 24, 2022

As you know, every programming language encounters many errors, some at runtime, others at compile time. Python sometimes encounters array errors when using the numpy library.

The `ValueError: Expected 2D array, got 1D array instead` error occurs when you pass a 1-dimensional array instead of a 2-dimensional array in numpy.

## Numpy Array in Python

Numpy is an open-source library that deals with arrays and mathematical operations. In Python, you have lists that provide us with the purpose of arrays, but the numpy creators claim that they prove `50x` faster arrays than lists.

It is one of the core purposes of using the numpy array.

## Create a Numpy Array in Python

The syntax of the numpy arrays is straightforward. You must import the numpy library to your program and use as accordingly.

``````#Python 3.x
import numpy as np
# creating a numpy array
array1 = np.array([2,4,6])
print(array1)
``````

Output:

``````#Python 3.x
[2 4 6]
``````

## Cause of `ValueError Expected 2D array, got 1D array instead` in Python

This error occurs when you pass a 1-dimensional array in a function. However, the function requires a 2-dimensional array, so instead of giving a 2D array, you passed an Array with a Single Dimension.

It mostly happens using a machine learning algorithm in the `predict()` method.

Now let’s take a look at this scenario.

``````#Python 3.x
import numpy as np
from sklearn import svm
X = np.array([[2,1],
[4,5],
[2.6,3.5],
[6,6],
[0.8,1],
[7,10]])
y = [1,0,1,0,1,0]
classifier = svm.SVC(kernel="linear", C = 1.0)
classifier.fit(X,y)
print(classifier.predict([0.7,1.10]))
``````

Output:

``````#Python 3.x
ValueError: Expected 2D array, got 1D array instead:
array=[0.7 1.1].
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.
``````

## Fix `ValueError: Expected 2D Array, got 1D Array instead` in Python

### Use Double Square Brackets With the Data

Below we have resolved the error in the previous example. The simplest way to fix the error is to convert a dimensional array to a 2-dimensional array.

You can enclose `[0.7,1.10]` in another square bracket to convert it to a 2D array when passing it to the `predict()` method.

Example code:

``````#Python 3.x
import numpy as np
from sklearn import svm
X = np.array([[2,1],
[4,5],
[2.6,3.5],
[6,6],
[0.8,1],
[7,10]])
y = [1,0,1,0,1,0]
classifier = svm.SVC(kernel="linear", C = 1.0)
classifier.fit(X,y)
print(classifier.predict([[0.7,1.10]]))
``````

Output:

``````#Python 3.x
[1]
``````

### Reshape the Array Using `reshape()`

Another way to convert a 1D array to 2D is to reshape the array using the `reshape()` method. You can reshape an array in Python using the `reshape()` method.

The number of elements in each dimension determines the shape of an array. You can add or remove array dimensions using reshaping.

In the following code, you can see the dimensions of the numpy array before and after using the `reshape()` method.

Example code:

``````#Python 3.x
import numpy as np
from sklearn import svm
X = np.array([[2,1],
[4,5],
[2.6,3.5],
[6,6],
[0.8,1],
[7,10]])
y = [1,0,1,0,1,0]
classifier = svm.SVC(kernel="linear", C = 1.0)
classifier.fit(X,y)
test=np.array([0.7,1.10])
print("Dimension before:", test.ndim)
test=test.reshape(1, -1)
print("Dimension now:", test.ndim)
print("Classifier Result:", classifier.predict(test))
``````

Output:

``````#Python 3.x
Dimension before: 1
Dimension now: 2
Classifier Result: [1]
``````
Author: Fariba Laiq

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.