How to get rid of 'value error expected 2d array got 1d array instead' in spyder while using simple linear...
I'm new to machine learning and I'm trying out simple linear regression for the first time on my pc. I am getting an error saying value error expected 2d array but got 1d array instead.
I have no clue on what to do. Any tips in fixing this code will be helpful.
import numpy as np
import matplotlib as plt
import pandas as pd
#preparing dataset
dataset = pd.read_csv("Salary_Data.csv")
X = dataset.iloc[:,0].values
Y = dataset.iloc[:,1].values
#Missing values
#Encoding
#splitting Dataset
from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test = train_test_split(X,Y, test_size = 1/3, random_state = 0)
#Format Scaling
#Simple Linear regressing
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train,Y_train)
The error I got is:
regressor.fit(X_train,Y_train)
Traceback (most recent call last):
File "<ipython-input-10-4d17c24ccad2>", line 1, in <module>
regressor.fit(X_train,Y_train)
File "C:UsershomeAnacondalibsite-packagessklearnlinear_modelbase.py", line 458, in fit
y_numeric=True, multi_output=True)
File "C:UsershomeAnacondalibsite-packagessklearnutilsvalidation.py", line 756, in check_X_y
estimator=estimator)
File "C:UsershomeAnacondalibsite-packagessklearnutilsvalidation.py", line 552, in check_array
"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[ 2.9 5.1 3.2 4.5 8.2 6.8 1.3 10.5 3. 2.2 5.9 6. 3.7 3.2
9. 2. 1.1 7.1 4.9 4. ].
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.
python machine-learning scikit-learn
add a comment |
I'm new to machine learning and I'm trying out simple linear regression for the first time on my pc. I am getting an error saying value error expected 2d array but got 1d array instead.
I have no clue on what to do. Any tips in fixing this code will be helpful.
import numpy as np
import matplotlib as plt
import pandas as pd
#preparing dataset
dataset = pd.read_csv("Salary_Data.csv")
X = dataset.iloc[:,0].values
Y = dataset.iloc[:,1].values
#Missing values
#Encoding
#splitting Dataset
from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test = train_test_split(X,Y, test_size = 1/3, random_state = 0)
#Format Scaling
#Simple Linear regressing
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train,Y_train)
The error I got is:
regressor.fit(X_train,Y_train)
Traceback (most recent call last):
File "<ipython-input-10-4d17c24ccad2>", line 1, in <module>
regressor.fit(X_train,Y_train)
File "C:UsershomeAnacondalibsite-packagessklearnlinear_modelbase.py", line 458, in fit
y_numeric=True, multi_output=True)
File "C:UsershomeAnacondalibsite-packagessklearnutilsvalidation.py", line 756, in check_X_y
estimator=estimator)
File "C:UsershomeAnacondalibsite-packagessklearnutilsvalidation.py", line 552, in check_array
"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[ 2.9 5.1 3.2 4.5 8.2 6.8 1.3 10.5 3. 2.2 5.9 6. 3.7 3.2
9. 2. 1.1 7.1 4.9 4. ].
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.
python machine-learning scikit-learn
add a comment |
I'm new to machine learning and I'm trying out simple linear regression for the first time on my pc. I am getting an error saying value error expected 2d array but got 1d array instead.
I have no clue on what to do. Any tips in fixing this code will be helpful.
import numpy as np
import matplotlib as plt
import pandas as pd
#preparing dataset
dataset = pd.read_csv("Salary_Data.csv")
X = dataset.iloc[:,0].values
Y = dataset.iloc[:,1].values
#Missing values
#Encoding
#splitting Dataset
from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test = train_test_split(X,Y, test_size = 1/3, random_state = 0)
#Format Scaling
#Simple Linear regressing
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train,Y_train)
The error I got is:
regressor.fit(X_train,Y_train)
Traceback (most recent call last):
File "<ipython-input-10-4d17c24ccad2>", line 1, in <module>
regressor.fit(X_train,Y_train)
File "C:UsershomeAnacondalibsite-packagessklearnlinear_modelbase.py", line 458, in fit
y_numeric=True, multi_output=True)
File "C:UsershomeAnacondalibsite-packagessklearnutilsvalidation.py", line 756, in check_X_y
estimator=estimator)
File "C:UsershomeAnacondalibsite-packagessklearnutilsvalidation.py", line 552, in check_array
"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[ 2.9 5.1 3.2 4.5 8.2 6.8 1.3 10.5 3. 2.2 5.9 6. 3.7 3.2
9. 2. 1.1 7.1 4.9 4. ].
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.
python machine-learning scikit-learn
I'm new to machine learning and I'm trying out simple linear regression for the first time on my pc. I am getting an error saying value error expected 2d array but got 1d array instead.
I have no clue on what to do. Any tips in fixing this code will be helpful.
import numpy as np
import matplotlib as plt
import pandas as pd
#preparing dataset
dataset = pd.read_csv("Salary_Data.csv")
X = dataset.iloc[:,0].values
Y = dataset.iloc[:,1].values
#Missing values
#Encoding
#splitting Dataset
from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test = train_test_split(X,Y, test_size = 1/3, random_state = 0)
#Format Scaling
#Simple Linear regressing
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train,Y_train)
The error I got is:
regressor.fit(X_train,Y_train)
Traceback (most recent call last):
File "<ipython-input-10-4d17c24ccad2>", line 1, in <module>
regressor.fit(X_train,Y_train)
File "C:UsershomeAnacondalibsite-packagessklearnlinear_modelbase.py", line 458, in fit
y_numeric=True, multi_output=True)
File "C:UsershomeAnacondalibsite-packagessklearnutilsvalidation.py", line 756, in check_X_y
estimator=estimator)
File "C:UsershomeAnacondalibsite-packagessklearnutilsvalidation.py", line 552, in check_array
"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[ 2.9 5.1 3.2 4.5 8.2 6.8 1.3 10.5 3. 2.2 5.9 6. 3.7 3.2
9. 2. 1.1 7.1 4.9 4. ].
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.
python machine-learning scikit-learn
python machine-learning scikit-learn
edited Jan 4 at 8:12
Vivek Kumar
16.4k42155
16.4k42155
asked Jan 2 at 4:45
RAJU CRAJU C
12
12
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Per the sklearn docs LinearRegression::fit() expects that the shape of X is (n_samples, n_features) and the shape of Y is (n_samples, n_targets). In the case in question I believe this would correspond to (20L, 1) and (20L, ) (i.e. one 'row vector' of length 20 and one 'column' vector of length 20). The call
X = dataset.iloc[:,0].values
Produces an object shaped (20L, ). So this needs to be reshaped to (20L, 1) which is fairly trivial (and actually what the somewhat cryptic suggestion "use array.reshape(-1, 1)" is trying to tell you),
X = dataset.iloc[:,0].values.reshape(-1, 1)
as @Dan has noted, an appropriate alternative is
X = dataset.iloc[:,[0]].values
Both options will produce an object of shape (20L, 1) which is then compatible with the Y object of shape (20L, ). Note that the shapes of X_train and Y_train shadow those of X and Y, respectively.
1
You can also just do this:X = dataset.iloc[:,[0]].valuesto get that correct shape
– Dan
Jan 2 at 14:39
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001299%2fhow-to-get-rid-of-value-error-expected-2d-array-got-1d-array-instead-in-spyder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Per the sklearn docs LinearRegression::fit() expects that the shape of X is (n_samples, n_features) and the shape of Y is (n_samples, n_targets). In the case in question I believe this would correspond to (20L, 1) and (20L, ) (i.e. one 'row vector' of length 20 and one 'column' vector of length 20). The call
X = dataset.iloc[:,0].values
Produces an object shaped (20L, ). So this needs to be reshaped to (20L, 1) which is fairly trivial (and actually what the somewhat cryptic suggestion "use array.reshape(-1, 1)" is trying to tell you),
X = dataset.iloc[:,0].values.reshape(-1, 1)
as @Dan has noted, an appropriate alternative is
X = dataset.iloc[:,[0]].values
Both options will produce an object of shape (20L, 1) which is then compatible with the Y object of shape (20L, ). Note that the shapes of X_train and Y_train shadow those of X and Y, respectively.
1
You can also just do this:X = dataset.iloc[:,[0]].valuesto get that correct shape
– Dan
Jan 2 at 14:39
add a comment |
Per the sklearn docs LinearRegression::fit() expects that the shape of X is (n_samples, n_features) and the shape of Y is (n_samples, n_targets). In the case in question I believe this would correspond to (20L, 1) and (20L, ) (i.e. one 'row vector' of length 20 and one 'column' vector of length 20). The call
X = dataset.iloc[:,0].values
Produces an object shaped (20L, ). So this needs to be reshaped to (20L, 1) which is fairly trivial (and actually what the somewhat cryptic suggestion "use array.reshape(-1, 1)" is trying to tell you),
X = dataset.iloc[:,0].values.reshape(-1, 1)
as @Dan has noted, an appropriate alternative is
X = dataset.iloc[:,[0]].values
Both options will produce an object of shape (20L, 1) which is then compatible with the Y object of shape (20L, ). Note that the shapes of X_train and Y_train shadow those of X and Y, respectively.
1
You can also just do this:X = dataset.iloc[:,[0]].valuesto get that correct shape
– Dan
Jan 2 at 14:39
add a comment |
Per the sklearn docs LinearRegression::fit() expects that the shape of X is (n_samples, n_features) and the shape of Y is (n_samples, n_targets). In the case in question I believe this would correspond to (20L, 1) and (20L, ) (i.e. one 'row vector' of length 20 and one 'column' vector of length 20). The call
X = dataset.iloc[:,0].values
Produces an object shaped (20L, ). So this needs to be reshaped to (20L, 1) which is fairly trivial (and actually what the somewhat cryptic suggestion "use array.reshape(-1, 1)" is trying to tell you),
X = dataset.iloc[:,0].values.reshape(-1, 1)
as @Dan has noted, an appropriate alternative is
X = dataset.iloc[:,[0]].values
Both options will produce an object of shape (20L, 1) which is then compatible with the Y object of shape (20L, ). Note that the shapes of X_train and Y_train shadow those of X and Y, respectively.
Per the sklearn docs LinearRegression::fit() expects that the shape of X is (n_samples, n_features) and the shape of Y is (n_samples, n_targets). In the case in question I believe this would correspond to (20L, 1) and (20L, ) (i.e. one 'row vector' of length 20 and one 'column' vector of length 20). The call
X = dataset.iloc[:,0].values
Produces an object shaped (20L, ). So this needs to be reshaped to (20L, 1) which is fairly trivial (and actually what the somewhat cryptic suggestion "use array.reshape(-1, 1)" is trying to tell you),
X = dataset.iloc[:,0].values.reshape(-1, 1)
as @Dan has noted, an appropriate alternative is
X = dataset.iloc[:,[0]].values
Both options will produce an object of shape (20L, 1) which is then compatible with the Y object of shape (20L, ). Note that the shapes of X_train and Y_train shadow those of X and Y, respectively.
edited Jan 2 at 17:39
answered Jan 2 at 5:54
William MillerWilliam Miller
1,405217
1,405217
1
You can also just do this:X = dataset.iloc[:,[0]].valuesto get that correct shape
– Dan
Jan 2 at 14:39
add a comment |
1
You can also just do this:X = dataset.iloc[:,[0]].valuesto get that correct shape
– Dan
Jan 2 at 14:39
1
1
You can also just do this:
X = dataset.iloc[:,[0]].values to get that correct shape– Dan
Jan 2 at 14:39
You can also just do this:
X = dataset.iloc[:,[0]].values to get that correct shape– Dan
Jan 2 at 14:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001299%2fhow-to-get-rid-of-value-error-expected-2d-array-got-1d-array-instead-in-spyder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown