You are just supposed to provide the predict method with the same 2D array, but with one value that you want to process (or more). In short, you can just replace

[0.58,0.76]

With

[[0.58,0.76]]

And it should work.

EDIT: This answer became popular so I thought I'd add a little more explanation about ML. The short version: we can only use predict on data that is of the same dimensionality as the training data (X) was.

In the example in question, we give the computer a bunch of rows in X (with 2 values each) and we show it the correct responses in y. When we want to predict using new values, our program expects the same - a bunch of rows. Even if we want to do it to just one row (with two values), that row has to be part of another array.

Answer from Ofer Sadan on Stack Overflow
Top answer
1 of 11
221

You are just supposed to provide the predict method with the same 2D array, but with one value that you want to process (or more). In short, you can just replace

[0.58,0.76]

With

[[0.58,0.76]]

And it should work.

EDIT: This answer became popular so I thought I'd add a little more explanation about ML. The short version: we can only use predict on data that is of the same dimensionality as the training data (X) was.

In the example in question, we give the computer a bunch of rows in X (with 2 values each) and we show it the correct responses in y. When we want to predict using new values, our program expects the same - a bunch of rows. Even if we want to do it to just one row (with two values), that row has to be part of another array.

2 of 11
24

The problem is occurring when you run prediction on the array [0.58,0.76]. Fix the problem by reshaping it before you call predict():

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style

style.use("ggplot")
from sklearn import svm

x = [1, 5, 1.5, 8, 1, 9]
y = [2, 8, 1.8, 8, 0.6, 11]

plt.scatter(x,y)
plt.show()

X = np.array([[1,2],
             [5,8],
             [1.5,1.8],
             [8,8],
             [1,0.6],
             [9,11]])

y = [0,1,0,1,0,1]

clf = svm.SVC(kernel='linear', C = 1.0)
clf.fit(X,y)

test = np.array([0.58, 0.76])
print test       # Produces: [ 0.58  0.76]
print test.shape # Produces: (2,) meaning 2 rows, 1 col

test = test.reshape(1, -1)
print test       # Produces: [[ 0.58  0.76]]
print test.shape # Produces (1, 2) meaning 1 row, 2 cols

print(clf.predict(test)) # Produces [0], as expected
🌐
GitHub
github.com › scikit-learn-contrib › MAPIE › issues › 413
Expected 2D array, got 1D array instead. GradientBostingRegressor · Issue #413 · scikit-learn-contrib/MAPIE
February 11, 2024 - ValueError Traceback (most recent call last) <ipython-input-1-2ca3f38dea46> in <cell line: 31>() 29 alpha = 0.1 30 mapie = MapieQuantileRegressor(estimator=Model, cv="split", alpha=alpha) ---> 31 mapie.fit(X_train, y_train, X_calib=X_calib, y_calib=y_calib) 32 y_pred, y_pis = mapie.predict(X_test) 33 5 frames /usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator, input_name) 900 # If input is 1D raise error 901 if array.ndim == 1: --> 902 raise ValueError( 903 "Expected 2D array, got 1D array instead:\narray={}.\n" 904 "Reshape your data either using array.reshape(-1, 1) if " ValueError: Expected 2D array, got 1D array instead: array=[ 0.5914918 -1.9605857 1.3109057 ...
Author   Karoloso
Discussions

ValueError: Expected 2D array, got 1D array instead - Example Notebook
Hi, i just tried tsfresh on a fresh Anaconda Installation on Mac OSX and used the timeseries_forecasting_google_stock.ipyn example Notebook. It fails at row 15 with the error --------------------------------------------------------------... More on github.com
🌐 github.com
5
November 15, 2017
machine learning - ValueError: y should be a 1d array, got an array of shape (1045, 5) instead - Data Science Stack Exchange
I have just started Python and working on training models. The task that I have been assigned is to train a dataset named "austin_Weather" Original Dataset y attribute After having done s... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
December 25, 2020
2D array value error
Hie guys am having this error in my code Expected 2D array, got 1D array instead:array=[0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0].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. More on discuss.python.org
🌐 discuss.python.org
0
0
September 10, 2021
machine learning - Expected 2D array, got scalar array instead - Data Science Stack Exchange
Can anyone help me with this error. I did the following code but it does not work and I am getting the following error: ValueError: Expected 2D array, got scalar array instead: array=6.5. Reshape ... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
June 1, 2019
🌐
Medium
medium.com › @shubhshreeaishwarya › valueerror-expected-2d-array-got-1d-array-instead-fixed-95c7c0756b83
ValueError: Expected 2D array, got 1D array instead [Fixed] | by Shubhshree aishwarya | Medium
October 12, 2023 - ValueError: Expected 2D array, got 1D array instead [Fixed] occurs when we pass a 1-dimensional array to a function that expects a 2-dimensional array. To solve the error, reshape the numpy.reshape() …
🌐
Codemia
codemia.io › knowledge-hub › path › error_in_python_script_expected_2d_array_got_1d_array_instead
Error in Python script Expected 2D array, got 1D ...
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Aionlinecourse
aionlinecourse.com › blog › error-in-python-script-expected-2d-array-got-1d-array-instead
Error in Python script "Expected 2D array, got 1D array instead:"? | Aionlinecourse
The "ValueError: Expected 2D array, got 1D array instead" error occurs when a function in your Python script expects a two-dimensional array but receives a one-dimensional array instead.
🌐
GitHub
github.com › blue-yonder › tsfresh › issues › 351
ValueError: Expected 2D array, got 1D array instead - Example Notebook · Issue #351 · blue-yonder/tsfresh
November 15, 2017 - The error can be easily fixed by changing the predict call and enclose the passed array in brackets.
Author   seb-koch
Find elsewhere
🌐
Python.org
discuss.python.org › python help
2D array value error - Python Help - Discussions on Python.org
September 10, 2021 - Hie guys am having this error in my code Expected 2D array, got 1D array instead:array=[0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0].Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains ...
🌐
Quora
quora.com › How-do-I-solve-ValueError-Expected-2D-array-got-1D-array-instead-error-in-Python
How to solve ValueError: Expected 2D array, got 1D array instead error in Python - Quora
Answer (1 of 2): These simply means that the function in which you are passing your array requires a 2D array, but you are passing a single-dimensional array to it Suppose you have an array X = [1,2,3,4,5] You need to pass it as a 2D array to ...
🌐
Kaggle
kaggle.com › questions-and-answers › 218705
ValueError: Expected 2D array, got 1D array instead:
Checking your browser before accessing www.kaggle.com · Click here if you are not automatically redirected after 5 seconds
🌐
Medium
medium.com › geekculture › sklearn-expects-data-to-be-in-shape-64fbcaf80a8c
Sklearn expects Data to be In Shape | by Mahindra Venkat Lukka | Geek Culture | Medium
November 10, 2021 - ValueError: Expected 2D array, got 1D array instead: array=[2 7 8 4 1 6]. 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.
🌐
Stack Exchange
datascience.stackexchange.com › questions › 63554 › valueerror-expected-2d-array-got-1d-array-instead
python - ValueError: Expected 2D array, got 1D array instead - Data Science Stack Exchange
ValueError: Expected 2D array, got scalar array instead: array=<built-in method reshape of numpy.ndarray object at 0x000000000C28EAD0>. 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.
🌐
Kaggle
kaggle.com › questions-and-answers › 110679
valueerror expected 2d array got 1d array instead sklearn ...
Checking your browser before accessing www.kaggle.com · Click here if you are not automatically redirected after 5 seconds
🌐
GitHub
github.com › stanford-oval › storm › issues › 112
ValueError: Expected 2D array, got 1D array instead: · Issue #112 · stanford-oval/storm
July 30, 2024 - ValueError: Expected 2D array, got 1D array instead: array=[]. 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.
Author   Stubborn-z
🌐
YouTube
youtube.com › how to fix your computer
PYTHON : Error in Python script "Expected 2D array, got 1D array instead:"? - YouTube
PYTHON : Error in Python script "Expected 2D array, got 1D array instead:"? [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] PYT...
Published   December 8, 2021
Views   2K
🌐
Google Developer forums
googlecloudcommunity.com › google cloud › build with ai › custom ml & mlops
error when trying to run sample request in python - #2 by lsolatorio - Custom ML & MLOps - Google Developer forums
December 9, 2023 - I understand that you are having challenges deploying your custom trained model. The error message suggests that there has been a discrepancy with your array input, receiving only 1D instead of a 2D input.