scikit-learn kernel density not working with 1-D array
up vote
-1
down vote
favorite
I'm trying to compute kernel density estimation using scikit-learn, and it's not working for 1-D array
Suppose this is the array
import numpy as np
from sklearn.neighbors import KernelDensity
df = [11,22,1,5,22,68,10,22,10.5,33,221,45,22,...................]
kde = KernelDensity(kernel='gaussian', bandwidth=0.75).fit(df)
ValueError: Expected 2D array, got 1D array instead:
array=[ 11. 22. 1. 5. 22. 68. 10. 22. 10.5 33. 221. 45.
22. ].
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.
I tried to reshape the array
kde = KernelDensity(kernel='gaussian',bandwidth=0.75).fit(np.array(df).reshape(-1,1))
# or
kde = KernelDensity(kernel='gaussian',bandwidth=0.75).fit(np.array(df).reshape(1,-1))
But it's not working either.
python numpy scikit-learn kernel-density
add a comment |
up vote
-1
down vote
favorite
I'm trying to compute kernel density estimation using scikit-learn, and it's not working for 1-D array
Suppose this is the array
import numpy as np
from sklearn.neighbors import KernelDensity
df = [11,22,1,5,22,68,10,22,10.5,33,221,45,22,...................]
kde = KernelDensity(kernel='gaussian', bandwidth=0.75).fit(df)
ValueError: Expected 2D array, got 1D array instead:
array=[ 11. 22. 1. 5. 22. 68. 10. 22. 10.5 33. 221. 45.
22. ].
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.
I tried to reshape the array
kde = KernelDensity(kernel='gaussian',bandwidth=0.75).fit(np.array(df).reshape(-1,1))
# or
kde = KernelDensity(kernel='gaussian',bandwidth=0.75).fit(np.array(df).reshape(1,-1))
But it's not working either.
python numpy scikit-learn kernel-density
2
When you say, "it's not working," what do you mean? What error are you getting? Both commands execute without error on my setup.
– Evan
Nov 7 at 20:44
Now it's giving me this : Expected 2D array, got scalar array instead
– Amine Messaoudi
Nov 7 at 20:48
if you reshape the array separately, what does it look like? Can you show your output from the reshape commands? When you say "it's giving me this," what is "it"? Python? Numpy? Scikit-lean? Your computer? Can you paste the error message and traceback?
– Evan
Nov 7 at 22:51
The problem is between keyboard and chair.
– user6655984
Nov 8 at 14:38
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm trying to compute kernel density estimation using scikit-learn, and it's not working for 1-D array
Suppose this is the array
import numpy as np
from sklearn.neighbors import KernelDensity
df = [11,22,1,5,22,68,10,22,10.5,33,221,45,22,...................]
kde = KernelDensity(kernel='gaussian', bandwidth=0.75).fit(df)
ValueError: Expected 2D array, got 1D array instead:
array=[ 11. 22. 1. 5. 22. 68. 10. 22. 10.5 33. 221. 45.
22. ].
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.
I tried to reshape the array
kde = KernelDensity(kernel='gaussian',bandwidth=0.75).fit(np.array(df).reshape(-1,1))
# or
kde = KernelDensity(kernel='gaussian',bandwidth=0.75).fit(np.array(df).reshape(1,-1))
But it's not working either.
python numpy scikit-learn kernel-density
I'm trying to compute kernel density estimation using scikit-learn, and it's not working for 1-D array
Suppose this is the array
import numpy as np
from sklearn.neighbors import KernelDensity
df = [11,22,1,5,22,68,10,22,10.5,33,221,45,22,...................]
kde = KernelDensity(kernel='gaussian', bandwidth=0.75).fit(df)
ValueError: Expected 2D array, got 1D array instead:
array=[ 11. 22. 1. 5. 22. 68. 10. 22. 10.5 33. 221. 45.
22. ].
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.
I tried to reshape the array
kde = KernelDensity(kernel='gaussian',bandwidth=0.75).fit(np.array(df).reshape(-1,1))
# or
kde = KernelDensity(kernel='gaussian',bandwidth=0.75).fit(np.array(df).reshape(1,-1))
But it's not working either.
python numpy scikit-learn kernel-density
python numpy scikit-learn kernel-density
edited Nov 7 at 20:38
asked Nov 7 at 20:32
Amine Messaoudi
415314
415314
2
When you say, "it's not working," what do you mean? What error are you getting? Both commands execute without error on my setup.
– Evan
Nov 7 at 20:44
Now it's giving me this : Expected 2D array, got scalar array instead
– Amine Messaoudi
Nov 7 at 20:48
if you reshape the array separately, what does it look like? Can you show your output from the reshape commands? When you say "it's giving me this," what is "it"? Python? Numpy? Scikit-lean? Your computer? Can you paste the error message and traceback?
– Evan
Nov 7 at 22:51
The problem is between keyboard and chair.
– user6655984
Nov 8 at 14:38
add a comment |
2
When you say, "it's not working," what do you mean? What error are you getting? Both commands execute without error on my setup.
– Evan
Nov 7 at 20:44
Now it's giving me this : Expected 2D array, got scalar array instead
– Amine Messaoudi
Nov 7 at 20:48
if you reshape the array separately, what does it look like? Can you show your output from the reshape commands? When you say "it's giving me this," what is "it"? Python? Numpy? Scikit-lean? Your computer? Can you paste the error message and traceback?
– Evan
Nov 7 at 22:51
The problem is between keyboard and chair.
– user6655984
Nov 8 at 14:38
2
2
When you say, "it's not working," what do you mean? What error are you getting? Both commands execute without error on my setup.
– Evan
Nov 7 at 20:44
When you say, "it's not working," what do you mean? What error are you getting? Both commands execute without error on my setup.
– Evan
Nov 7 at 20:44
Now it's giving me this : Expected 2D array, got scalar array instead
– Amine Messaoudi
Nov 7 at 20:48
Now it's giving me this : Expected 2D array, got scalar array instead
– Amine Messaoudi
Nov 7 at 20:48
if you reshape the array separately, what does it look like? Can you show your output from the reshape commands? When you say "it's giving me this," what is "it"? Python? Numpy? Scikit-lean? Your computer? Can you paste the error message and traceback?
– Evan
Nov 7 at 22:51
if you reshape the array separately, what does it look like? Can you show your output from the reshape commands? When you say "it's giving me this," what is "it"? Python? Numpy? Scikit-lean? Your computer? Can you paste the error message and traceback?
– Evan
Nov 7 at 22:51
The problem is between keyboard and chair.
– user6655984
Nov 8 at 14:38
The problem is between keyboard and chair.
– user6655984
Nov 8 at 14:38
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53197378%2fscikit-learn-kernel-density-not-working-with-1-d-array%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
2
When you say, "it's not working," what do you mean? What error are you getting? Both commands execute without error on my setup.
– Evan
Nov 7 at 20:44
Now it's giving me this : Expected 2D array, got scalar array instead
– Amine Messaoudi
Nov 7 at 20:48
if you reshape the array separately, what does it look like? Can you show your output from the reshape commands? When you say "it's giving me this," what is "it"? Python? Numpy? Scikit-lean? Your computer? Can you paste the error message and traceback?
– Evan
Nov 7 at 22:51
The problem is between keyboard and chair.
– user6655984
Nov 8 at 14:38