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.










share|improve this question




















  • 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















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.










share|improve this question




















  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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

















active

oldest

votes











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',
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
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings