ValueError: Expected 2D array, got 1D array instead: array=[2 4 7 9].

0 votes

Hi Guys,
I am trying to create one model using LinearRegression module, but it shows me the below error.

$ mind=LinearRegression()
$ mind.fit(x,y)
ValueError                                Traceback (most recent call last)
<ipython-input-24-f07030a0937c> in <module>
----> 1 mind.fit(x,y)
~\anaconda3\lib\site-packages\sklearn\linear_model\_base.py in fit(self, X, y, sample_weight)
    490         n_jobs_ = self.n_jobs
    491         X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
--> 492                          y_numeric=True, multi_output=True)
    493
    494         if sample_weight is not None:
~\anaconda3\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    753                     ensure_min_features=ensure_min_features,
    754                     warn_on_dtype=warn_on_dtype,
--> 755                     estimator=estimator)
    756     if multi_output:
    757         y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,
~\anaconda3\lib\site-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, warn_on_dtype, estimator)
    554                     "Reshape your data either using array.reshape(-1, 1) if "
    555                     "your data has a single feature or array.reshape(1, -1) "
--> 556                     "if it contains a single sample.".format(array))
    557
    558         # in the future np.flexible dtypes will be handled like object dtypes
ValueError: Expected 2D array, got 1D array instead:
array=[2 4 7 9].
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.

What I am doing wrong?

Apr 9 in Machine Learning by akhtar
• 10,580 points
604 views

1 answer to this question.

0 votes

Hi@akhtar,

According to your code, you are trying to create one model using mind.fit() function. This function contains two parameter. But it takes the first parameter as a 2D array.

So, make sure your x is a 2D array or you can reshape your using the below given command.

$ X = x.values.reshape(a,b)

Now try once it will work.

answered Apr 9 by MD
• 23,050 points

Related Questions In Machine Learning

0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 947 views
0 votes
0 answers
0 votes
1 answer

How to convert 1D array into 2D array using pandas?

Hi@akhtar, You can follow the below given codes to ...READ MORE

answered May 8 in Machine Learning by MD
• 23,050 points
86 views