1

I did polynomial regression on some data. The graph was plotted. But how do I get the function "f(x)" of the plotted graph? I like to integrate the function!

 z = np.polyfit(x, y1, 6)  
 p = np.poly1d(z) 

 xp = np.linspace(0, 1, 100)
 line = plt.plot(x, y1, '.', xp, p(xp), '-', label=' G_MEAN_1 ')
 plt.ylim(0, 1)
 plt.legend()
 plt.show()

So how can I get the function out of here - for integration?

ShapeOfMatter
  • 991
  • 6
  • 25
Max Mark
  • 71
  • 2
  • 9

2 Answers2

0

As

    p = np.poly1d(z) 

returns a function, you can use "p" for integration. So to get the integral form 0.3 to 0.6, you can go via:

integrate.quad(p, 0.3, 0.6)
Max Mark
  • 71
  • 2
  • 9
0

In a good sense your variable p already is that function. Have a look at the usage examples in the docs.

If you want a string representation to the effect of "x^2 + 4x - 5", it looks like you could just cast it to string. The handling of the exponents in the print() examples looks a little weird to me though.

ShapeOfMatter
  • 991
  • 6
  • 25