Scipy ValueError: object too deep for desired array with optimize.leastsq










0














I am trying to fit my 3D data with linear 3D function Z = ax+by+c. I import the data with pandas:



dataframe = pd.read_csv('3d_data.csv',names=['x','y','z'],header=0)

print(dataframe)

x y z
0 52.830740 7.812507 0.000000
1 44.647931 61.031381 8.827942
2 38.725318 0.707952 52.857968
3 0.000000 31.026271 17.743218
4 57.137854 51.291656 61.546131
5 46.341341 3.394429 26.462564
6 3.440893 46.333864 70.440650


I have done some digging and found that the best way to fit 3D data it is to use optimize from scipy with the model equation and residual function:



def model_calc(parameter, x, y):
a, b, c = parameter
return a*x + b*y + c

def residual(parameter, data, x, y):
res =
for _x in x:
for _y in y:
res.append(data-model_calc(parameter,x,y))
return res


I fit the data with:



params0 = [0.1, -0.2,1.]
result = scipy.optimize.leastsq(residual,params0,(dataframe['z'],dataframe['x'],dataframe['y']))
fittedParams = result[0]


But the result is a ValueError:



ValueError: object too deep for desired array [...]
minpack.error: Result from function call is not a proper array of floats.


I was trying to minimize the residual function to give only single value or single np.array but it didn't help. I don't know where is the problem and if maybe the search space for parameters it is not too complex. I would be very grateful for some hints!










share|improve this question


























    0














    I am trying to fit my 3D data with linear 3D function Z = ax+by+c. I import the data with pandas:



    dataframe = pd.read_csv('3d_data.csv',names=['x','y','z'],header=0)

    print(dataframe)

    x y z
    0 52.830740 7.812507 0.000000
    1 44.647931 61.031381 8.827942
    2 38.725318 0.707952 52.857968
    3 0.000000 31.026271 17.743218
    4 57.137854 51.291656 61.546131
    5 46.341341 3.394429 26.462564
    6 3.440893 46.333864 70.440650


    I have done some digging and found that the best way to fit 3D data it is to use optimize from scipy with the model equation and residual function:



    def model_calc(parameter, x, y):
    a, b, c = parameter
    return a*x + b*y + c

    def residual(parameter, data, x, y):
    res =
    for _x in x:
    for _y in y:
    res.append(data-model_calc(parameter,x,y))
    return res


    I fit the data with:



    params0 = [0.1, -0.2,1.]
    result = scipy.optimize.leastsq(residual,params0,(dataframe['z'],dataframe['x'],dataframe['y']))
    fittedParams = result[0]


    But the result is a ValueError:



    ValueError: object too deep for desired array [...]
    minpack.error: Result from function call is not a proper array of floats.


    I was trying to minimize the residual function to give only single value or single np.array but it didn't help. I don't know where is the problem and if maybe the search space for parameters it is not too complex. I would be very grateful for some hints!










    share|improve this question
























      0












      0








      0







      I am trying to fit my 3D data with linear 3D function Z = ax+by+c. I import the data with pandas:



      dataframe = pd.read_csv('3d_data.csv',names=['x','y','z'],header=0)

      print(dataframe)

      x y z
      0 52.830740 7.812507 0.000000
      1 44.647931 61.031381 8.827942
      2 38.725318 0.707952 52.857968
      3 0.000000 31.026271 17.743218
      4 57.137854 51.291656 61.546131
      5 46.341341 3.394429 26.462564
      6 3.440893 46.333864 70.440650


      I have done some digging and found that the best way to fit 3D data it is to use optimize from scipy with the model equation and residual function:



      def model_calc(parameter, x, y):
      a, b, c = parameter
      return a*x + b*y + c

      def residual(parameter, data, x, y):
      res =
      for _x in x:
      for _y in y:
      res.append(data-model_calc(parameter,x,y))
      return res


      I fit the data with:



      params0 = [0.1, -0.2,1.]
      result = scipy.optimize.leastsq(residual,params0,(dataframe['z'],dataframe['x'],dataframe['y']))
      fittedParams = result[0]


      But the result is a ValueError:



      ValueError: object too deep for desired array [...]
      minpack.error: Result from function call is not a proper array of floats.


      I was trying to minimize the residual function to give only single value or single np.array but it didn't help. I don't know where is the problem and if maybe the search space for parameters it is not too complex. I would be very grateful for some hints!










      share|improve this question













      I am trying to fit my 3D data with linear 3D function Z = ax+by+c. I import the data with pandas:



      dataframe = pd.read_csv('3d_data.csv',names=['x','y','z'],header=0)

      print(dataframe)

      x y z
      0 52.830740 7.812507 0.000000
      1 44.647931 61.031381 8.827942
      2 38.725318 0.707952 52.857968
      3 0.000000 31.026271 17.743218
      4 57.137854 51.291656 61.546131
      5 46.341341 3.394429 26.462564
      6 3.440893 46.333864 70.440650


      I have done some digging and found that the best way to fit 3D data it is to use optimize from scipy with the model equation and residual function:



      def model_calc(parameter, x, y):
      a, b, c = parameter
      return a*x + b*y + c

      def residual(parameter, data, x, y):
      res =
      for _x in x:
      for _y in y:
      res.append(data-model_calc(parameter,x,y))
      return res


      I fit the data with:



      params0 = [0.1, -0.2,1.]
      result = scipy.optimize.leastsq(residual,params0,(dataframe['z'],dataframe['x'],dataframe['y']))
      fittedParams = result[0]


      But the result is a ValueError:



      ValueError: object too deep for desired array [...]
      minpack.error: Result from function call is not a proper array of floats.


      I was trying to minimize the residual function to give only single value or single np.array but it didn't help. I don't know where is the problem and if maybe the search space for parameters it is not too complex. I would be very grateful for some hints!







      python numpy scipy data-fitting function-fitting






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 14:16









      Dawid

      147110




      147110






















          1 Answer
          1






          active

          oldest

          votes


















          1














          If you are fitting parameters to a function, you can use curve_fit. Here's an implementation:



          from scipy.optimize import curve_fit

          def model_calc(X, a, b, c):
          x, y = X
          return a*x + b*y + c

          p0 = [0.1, -0.2, 1.]
          popt, pcov = curve_fit(model_calc, (dataframe.x, dataframe.y), dataframe.z, p0) #popt is the fit, pcov is the covariance matrix (see the docs)


          Note that your sintax must be if the form f(X, a, b, c), where X can be a 2D vector (See this post).



          (Another approach)



          If you know your fit is going to be linear, you can use numpy.linalg.lstsq. See here. Example solution:



          import numpy as np
          from numpy.linalg import lstsq
          A = np.vstack((dataframe.x, dataframe.y, np.ones_like(dataframe.y))).T
          B = dataframe.z
          a, b, c = lstsq(A, B)[0]





          share|improve this answer




















          • Your answer works perfectly! Thank you so much!
            – Dawid
            Nov 13 at 8:42










          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%2f53264042%2fscipy-valueerror-object-too-deep-for-desired-array-with-optimize-leastsq%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









          1














          If you are fitting parameters to a function, you can use curve_fit. Here's an implementation:



          from scipy.optimize import curve_fit

          def model_calc(X, a, b, c):
          x, y = X
          return a*x + b*y + c

          p0 = [0.1, -0.2, 1.]
          popt, pcov = curve_fit(model_calc, (dataframe.x, dataframe.y), dataframe.z, p0) #popt is the fit, pcov is the covariance matrix (see the docs)


          Note that your sintax must be if the form f(X, a, b, c), where X can be a 2D vector (See this post).



          (Another approach)



          If you know your fit is going to be linear, you can use numpy.linalg.lstsq. See here. Example solution:



          import numpy as np
          from numpy.linalg import lstsq
          A = np.vstack((dataframe.x, dataframe.y, np.ones_like(dataframe.y))).T
          B = dataframe.z
          a, b, c = lstsq(A, B)[0]





          share|improve this answer




















          • Your answer works perfectly! Thank you so much!
            – Dawid
            Nov 13 at 8:42















          1














          If you are fitting parameters to a function, you can use curve_fit. Here's an implementation:



          from scipy.optimize import curve_fit

          def model_calc(X, a, b, c):
          x, y = X
          return a*x + b*y + c

          p0 = [0.1, -0.2, 1.]
          popt, pcov = curve_fit(model_calc, (dataframe.x, dataframe.y), dataframe.z, p0) #popt is the fit, pcov is the covariance matrix (see the docs)


          Note that your sintax must be if the form f(X, a, b, c), where X can be a 2D vector (See this post).



          (Another approach)



          If you know your fit is going to be linear, you can use numpy.linalg.lstsq. See here. Example solution:



          import numpy as np
          from numpy.linalg import lstsq
          A = np.vstack((dataframe.x, dataframe.y, np.ones_like(dataframe.y))).T
          B = dataframe.z
          a, b, c = lstsq(A, B)[0]





          share|improve this answer




















          • Your answer works perfectly! Thank you so much!
            – Dawid
            Nov 13 at 8:42













          1












          1








          1






          If you are fitting parameters to a function, you can use curve_fit. Here's an implementation:



          from scipy.optimize import curve_fit

          def model_calc(X, a, b, c):
          x, y = X
          return a*x + b*y + c

          p0 = [0.1, -0.2, 1.]
          popt, pcov = curve_fit(model_calc, (dataframe.x, dataframe.y), dataframe.z, p0) #popt is the fit, pcov is the covariance matrix (see the docs)


          Note that your sintax must be if the form f(X, a, b, c), where X can be a 2D vector (See this post).



          (Another approach)



          If you know your fit is going to be linear, you can use numpy.linalg.lstsq. See here. Example solution:



          import numpy as np
          from numpy.linalg import lstsq
          A = np.vstack((dataframe.x, dataframe.y, np.ones_like(dataframe.y))).T
          B = dataframe.z
          a, b, c = lstsq(A, B)[0]





          share|improve this answer












          If you are fitting parameters to a function, you can use curve_fit. Here's an implementation:



          from scipy.optimize import curve_fit

          def model_calc(X, a, b, c):
          x, y = X
          return a*x + b*y + c

          p0 = [0.1, -0.2, 1.]
          popt, pcov = curve_fit(model_calc, (dataframe.x, dataframe.y), dataframe.z, p0) #popt is the fit, pcov is the covariance matrix (see the docs)


          Note that your sintax must be if the form f(X, a, b, c), where X can be a 2D vector (See this post).



          (Another approach)



          If you know your fit is going to be linear, you can use numpy.linalg.lstsq. See here. Example solution:



          import numpy as np
          from numpy.linalg import lstsq
          A = np.vstack((dataframe.x, dataframe.y, np.ones_like(dataframe.y))).T
          B = dataframe.z
          a, b, c = lstsq(A, B)[0]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 16:41









          Mstaino

          73939




          73939











          • Your answer works perfectly! Thank you so much!
            – Dawid
            Nov 13 at 8:42
















          • Your answer works perfectly! Thank you so much!
            – Dawid
            Nov 13 at 8:42















          Your answer works perfectly! Thank you so much!
          – Dawid
          Nov 13 at 8:42




          Your answer works perfectly! Thank you so much!
          – Dawid
          Nov 13 at 8:42

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53264042%2fscipy-valueerror-object-too-deep-for-desired-array-with-optimize-leastsq%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto