error in python program. “expected 2D array but got 1D array instead”












0















I am trying to predict the price as well as plot to visualize the data. But there is an error that I am not able to figure it out.



dates=
prices=

def getdata(filename):
with open(filename,'r') as csvfile:
csvFilereader=csv.reader(csvfile)
next(csvFilereader)
for row in csvFilereader:

dates.append(int(row[0].split('-')[0]))
prices.append(float(row[1]))
return
def predicted_price(dates, prices, x):

dates=np.reshape(dates,len(dates),1)


svr_linear= SVR(kernel='linear', C=1e3)
svr_poly= SVR(kernel='poly', C=1e3, degree=2)
svr_rbf= SVR(kernel='rbf', C=1e3, gamma=0.1)

svr_linear.fit(dates,prices)
svr_poly.fit(dates,prices)
svr_rbf.fit(dates,prices)

plt.scatter(dates,prices, color='black', label='Data')
plt.plot(dates, svr.rbf.predict(dates), color='red', label='RBF Model')
plt.plot(dates, svr.poly.predict(dates), color='blue', label='Poly Model')
plt.plot(dates, svr.linear.predict(dates), color='green', label='Linera Model')

plt.xlabel('Dates')
plt.ylabel('Prices')
plt.title('Regression')

plt.legend()
plt.show()

return svr_rbf.predict(x)[0], svr_linerar.predict(x)[0], svr_poly(x)[0]


getdata('D:\android\trans1.csv')


predicted_prices=predicted_price(dates,prices,10)
print(predicted_prices)


Here is the error that I am getting:



Expected 2D array, got 1D array instead:
array=[19102018. 19102018. 19102018. ... 22102018. 20102018. 23102018.].
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.


Changing predicted_price:



(dates,prices,10)


to



([dates,prices,10])


Gives this error:



predicted_price() missing 2 required positional arguments: 'prices' and 'x'


Here is the image of data:



date and price data










share|improve this question

























  • In case you can make a minimal complete example including some data, you might want to ask for feedback on codereview.stackexchange.com

    – Martin Thoma
    Nov 23 '18 at 8:40
















0















I am trying to predict the price as well as plot to visualize the data. But there is an error that I am not able to figure it out.



dates=
prices=

def getdata(filename):
with open(filename,'r') as csvfile:
csvFilereader=csv.reader(csvfile)
next(csvFilereader)
for row in csvFilereader:

dates.append(int(row[0].split('-')[0]))
prices.append(float(row[1]))
return
def predicted_price(dates, prices, x):

dates=np.reshape(dates,len(dates),1)


svr_linear= SVR(kernel='linear', C=1e3)
svr_poly= SVR(kernel='poly', C=1e3, degree=2)
svr_rbf= SVR(kernel='rbf', C=1e3, gamma=0.1)

svr_linear.fit(dates,prices)
svr_poly.fit(dates,prices)
svr_rbf.fit(dates,prices)

plt.scatter(dates,prices, color='black', label='Data')
plt.plot(dates, svr.rbf.predict(dates), color='red', label='RBF Model')
plt.plot(dates, svr.poly.predict(dates), color='blue', label='Poly Model')
plt.plot(dates, svr.linear.predict(dates), color='green', label='Linera Model')

plt.xlabel('Dates')
plt.ylabel('Prices')
plt.title('Regression')

plt.legend()
plt.show()

return svr_rbf.predict(x)[0], svr_linerar.predict(x)[0], svr_poly(x)[0]


getdata('D:\android\trans1.csv')


predicted_prices=predicted_price(dates,prices,10)
print(predicted_prices)


Here is the error that I am getting:



Expected 2D array, got 1D array instead:
array=[19102018. 19102018. 19102018. ... 22102018. 20102018. 23102018.].
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.


Changing predicted_price:



(dates,prices,10)


to



([dates,prices,10])


Gives this error:



predicted_price() missing 2 required positional arguments: 'prices' and 'x'


Here is the image of data:



date and price data










share|improve this question

























  • In case you can make a minimal complete example including some data, you might want to ask for feedback on codereview.stackexchange.com

    – Martin Thoma
    Nov 23 '18 at 8:40














0












0








0








I am trying to predict the price as well as plot to visualize the data. But there is an error that I am not able to figure it out.



dates=
prices=

def getdata(filename):
with open(filename,'r') as csvfile:
csvFilereader=csv.reader(csvfile)
next(csvFilereader)
for row in csvFilereader:

dates.append(int(row[0].split('-')[0]))
prices.append(float(row[1]))
return
def predicted_price(dates, prices, x):

dates=np.reshape(dates,len(dates),1)


svr_linear= SVR(kernel='linear', C=1e3)
svr_poly= SVR(kernel='poly', C=1e3, degree=2)
svr_rbf= SVR(kernel='rbf', C=1e3, gamma=0.1)

svr_linear.fit(dates,prices)
svr_poly.fit(dates,prices)
svr_rbf.fit(dates,prices)

plt.scatter(dates,prices, color='black', label='Data')
plt.plot(dates, svr.rbf.predict(dates), color='red', label='RBF Model')
plt.plot(dates, svr.poly.predict(dates), color='blue', label='Poly Model')
plt.plot(dates, svr.linear.predict(dates), color='green', label='Linera Model')

plt.xlabel('Dates')
plt.ylabel('Prices')
plt.title('Regression')

plt.legend()
plt.show()

return svr_rbf.predict(x)[0], svr_linerar.predict(x)[0], svr_poly(x)[0]


getdata('D:\android\trans1.csv')


predicted_prices=predicted_price(dates,prices,10)
print(predicted_prices)


Here is the error that I am getting:



Expected 2D array, got 1D array instead:
array=[19102018. 19102018. 19102018. ... 22102018. 20102018. 23102018.].
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.


Changing predicted_price:



(dates,prices,10)


to



([dates,prices,10])


Gives this error:



predicted_price() missing 2 required positional arguments: 'prices' and 'x'


Here is the image of data:



date and price data










share|improve this question
















I am trying to predict the price as well as plot to visualize the data. But there is an error that I am not able to figure it out.



dates=
prices=

def getdata(filename):
with open(filename,'r') as csvfile:
csvFilereader=csv.reader(csvfile)
next(csvFilereader)
for row in csvFilereader:

dates.append(int(row[0].split('-')[0]))
prices.append(float(row[1]))
return
def predicted_price(dates, prices, x):

dates=np.reshape(dates,len(dates),1)


svr_linear= SVR(kernel='linear', C=1e3)
svr_poly= SVR(kernel='poly', C=1e3, degree=2)
svr_rbf= SVR(kernel='rbf', C=1e3, gamma=0.1)

svr_linear.fit(dates,prices)
svr_poly.fit(dates,prices)
svr_rbf.fit(dates,prices)

plt.scatter(dates,prices, color='black', label='Data')
plt.plot(dates, svr.rbf.predict(dates), color='red', label='RBF Model')
plt.plot(dates, svr.poly.predict(dates), color='blue', label='Poly Model')
plt.plot(dates, svr.linear.predict(dates), color='green', label='Linera Model')

plt.xlabel('Dates')
plt.ylabel('Prices')
plt.title('Regression')

plt.legend()
plt.show()

return svr_rbf.predict(x)[0], svr_linerar.predict(x)[0], svr_poly(x)[0]


getdata('D:\android\trans1.csv')


predicted_prices=predicted_price(dates,prices,10)
print(predicted_prices)


Here is the error that I am getting:



Expected 2D array, got 1D array instead:
array=[19102018. 19102018. 19102018. ... 22102018. 20102018. 23102018.].
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.


Changing predicted_price:



(dates,prices,10)


to



([dates,prices,10])


Gives this error:



predicted_price() missing 2 required positional arguments: 'prices' and 'x'


Here is the image of data:



date and price data







python scikit-learn






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 6:27









Martin Thoma

44.4k61317540




44.4k61317540










asked Nov 23 '18 at 5:16









varun chauhanvarun chauhan

3910




3910













  • In case you can make a minimal complete example including some data, you might want to ask for feedback on codereview.stackexchange.com

    – Martin Thoma
    Nov 23 '18 at 8:40



















  • In case you can make a minimal complete example including some data, you might want to ask for feedback on codereview.stackexchange.com

    – Martin Thoma
    Nov 23 '18 at 8:40

















In case you can make a minimal complete example including some data, you might want to ask for feedback on codereview.stackexchange.com

– Martin Thoma
Nov 23 '18 at 8:40





In case you can make a minimal complete example including some data, you might want to ask for feedback on codereview.stackexchange.com

– Martin Thoma
Nov 23 '18 at 8:40












1 Answer
1






active

oldest

votes


















0














This code has at least 3 issues:





  • getdata does not return anything. It only works because dates and prices are global. Move both of them in getdata and return dates, prices


  • SVR is not imported (sklearn I guess)

  • What the error message tells you: dates = dates.reshape(-1, 1)


Sub-question about parameters




Changing predicted_price:



(dates,prices,10)
to



([dates,prices,10])
Gives this error:



predicted_price() missing 2 required positional arguments: 'prices' and 'x'




When you write [dates,prices,10] you construct a single list. This single list is what you pass to the function. But the function expects 3 parameters, not one. Hence call it like predicted_price(dates,prices,10).



Another note: The braces (...) belong to the function, not to the data. This is important, because



predicted_price(dates,prices,10)


is different from



predicted_price((dates,prices,10))


The first one is correct, the second one constructs a tuple and passes it to predicted_price.



In case you can make a minimal complete example including some data, you might want to ask for feedback on codereview.stackexchange.com






share|improve this answer

























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


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440971%2ferror-in-python-program-expected-2d-array-but-got-1d-array-instead%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









    0














    This code has at least 3 issues:





    • getdata does not return anything. It only works because dates and prices are global. Move both of them in getdata and return dates, prices


    • SVR is not imported (sklearn I guess)

    • What the error message tells you: dates = dates.reshape(-1, 1)


    Sub-question about parameters




    Changing predicted_price:



    (dates,prices,10)
    to



    ([dates,prices,10])
    Gives this error:



    predicted_price() missing 2 required positional arguments: 'prices' and 'x'




    When you write [dates,prices,10] you construct a single list. This single list is what you pass to the function. But the function expects 3 parameters, not one. Hence call it like predicted_price(dates,prices,10).



    Another note: The braces (...) belong to the function, not to the data. This is important, because



    predicted_price(dates,prices,10)


    is different from



    predicted_price((dates,prices,10))


    The first one is correct, the second one constructs a tuple and passes it to predicted_price.



    In case you can make a minimal complete example including some data, you might want to ask for feedback on codereview.stackexchange.com






    share|improve this answer






























      0














      This code has at least 3 issues:





      • getdata does not return anything. It only works because dates and prices are global. Move both of them in getdata and return dates, prices


      • SVR is not imported (sklearn I guess)

      • What the error message tells you: dates = dates.reshape(-1, 1)


      Sub-question about parameters




      Changing predicted_price:



      (dates,prices,10)
      to



      ([dates,prices,10])
      Gives this error:



      predicted_price() missing 2 required positional arguments: 'prices' and 'x'




      When you write [dates,prices,10] you construct a single list. This single list is what you pass to the function. But the function expects 3 parameters, not one. Hence call it like predicted_price(dates,prices,10).



      Another note: The braces (...) belong to the function, not to the data. This is important, because



      predicted_price(dates,prices,10)


      is different from



      predicted_price((dates,prices,10))


      The first one is correct, the second one constructs a tuple and passes it to predicted_price.



      In case you can make a minimal complete example including some data, you might want to ask for feedback on codereview.stackexchange.com






      share|improve this answer




























        0












        0








        0







        This code has at least 3 issues:





        • getdata does not return anything. It only works because dates and prices are global. Move both of them in getdata and return dates, prices


        • SVR is not imported (sklearn I guess)

        • What the error message tells you: dates = dates.reshape(-1, 1)


        Sub-question about parameters




        Changing predicted_price:



        (dates,prices,10)
        to



        ([dates,prices,10])
        Gives this error:



        predicted_price() missing 2 required positional arguments: 'prices' and 'x'




        When you write [dates,prices,10] you construct a single list. This single list is what you pass to the function. But the function expects 3 parameters, not one. Hence call it like predicted_price(dates,prices,10).



        Another note: The braces (...) belong to the function, not to the data. This is important, because



        predicted_price(dates,prices,10)


        is different from



        predicted_price((dates,prices,10))


        The first one is correct, the second one constructs a tuple and passes it to predicted_price.



        In case you can make a minimal complete example including some data, you might want to ask for feedback on codereview.stackexchange.com






        share|improve this answer















        This code has at least 3 issues:





        • getdata does not return anything. It only works because dates and prices are global. Move both of them in getdata and return dates, prices


        • SVR is not imported (sklearn I guess)

        • What the error message tells you: dates = dates.reshape(-1, 1)


        Sub-question about parameters




        Changing predicted_price:



        (dates,prices,10)
        to



        ([dates,prices,10])
        Gives this error:



        predicted_price() missing 2 required positional arguments: 'prices' and 'x'




        When you write [dates,prices,10] you construct a single list. This single list is what you pass to the function. But the function expects 3 parameters, not one. Hence call it like predicted_price(dates,prices,10).



        Another note: The braces (...) belong to the function, not to the data. This is important, because



        predicted_price(dates,prices,10)


        is different from



        predicted_price((dates,prices,10))


        The first one is correct, the second one constructs a tuple and passes it to predicted_price.



        In case you can make a minimal complete example including some data, you might want to ask for feedback on codereview.stackexchange.com







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 8:38

























        answered Nov 23 '18 at 6:26









        Martin ThomaMartin Thoma

        44.4k61317540




        44.4k61317540
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440971%2ferror-in-python-program-expected-2d-array-but-got-1d-array-instead%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







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud