获取趋势线的方程(二次多项式)

huangapple go评论73阅读模式
英文:

To get trend-line's equation (polynomial, order 2)

问题

以下是您要翻译的内容:

# 一个简单的数据框,我想用它的趋势线(二次多项式,阶数为2)绘制它。然而,我显然得到了错误的方程式:

y = 1.4x**2 + 6.6x + 0.9

它应该是

y = 0.22x2 - 1.45x + 11.867  # "2" 在x后表示平方

我该如何得到正确的方程

import matplotlib.pyplot as plot
from scipy import stats
import numpy as np

data = [["2020-03-03", 9.727273],
["2020-03-04", 9.800000],
["2020-03-05", 9.727273],
["2020-03-06", 10.818182],
["2020-03-07", 9.500000],
["2020-03-08", 10.909091],
["2020-03-09", 15.000000],
["2020-03-10", 14.333333],
["2020-03-11", 15.333333],
["2020-03-12", 16.000000],
["2020-03-13", 21.000000],
["2020-03-14", 28.833333]]

fig, ax = plot.subplots()

dates = [x[0] for x in data]
usage = [x[1] for x in data]

bestfit = stats.linregress(range(len(usage)), usage)

equation = str(round(bestfit[0], 1)) + "x**2 + " + str(round(bestfit[1], 1)) + "x + " + str(round(bestfit[2], 1))

ax.plot(range(len(usage)), usage)
ax.plot(range(len(usage)), np.poly1d(np.polyfit(range(len(usage)), usage, 2))(range(len(usage)), '--', label=equation)

plot.show()

print(equation)

请注意,上述代码中的"是HTML编码的引号,应该被替换为正常的引号(")。

英文:

A simple dataframe that I want to plot it with its trend-line (polynomial, order 2). However I got the equation obviously wrong:

y = 1.4x**2 + 6.6x + 0.9

It shall be:

y = 0.22x2 - 1.45x + 11.867  # the "2" after x is square

How can I get the correct equation?

import matplotlib.pyplot as plot
from scipy import stats
import numpy as np

data = [["2020-03-03",9.727273],
["2020-03-04",9.800000],
["2020-03-05",9.727273],
["2020-03-06",10.818182],
["2020-03-07",9.500000],
["2020-03-08",10.909091],
["2020-03-09",15.000000],
["2020-03-10",14.333333],
["2020-03-11",15.333333],
["2020-03-12",16.000000],
["2020-03-13",21.000000],
["2020-03-14",28.833333]]

fig, ax = plot.subplots()

dates = [x[0] for x in data]
usage = [x[1] for x in data]

bestfit = stats.linregress(range(len(usage)),usage)

equation = str(round(bestfit[0],1)) + "x**2 + " + str(round(bestfit[1],1)) + "x + " + str(round(bestfit[2],1)) 

ax.plot(range(len(usage)), usage)
ax.plot(range(len(usage)), np.poly1d(np.polyfit(range(len(usage)), usage, 2))(range(len(usage))), '--',label=equation)

plot.show()

print (equation)

获取趋势线的方程(二次多项式)

答案1

得分: 2

以下是您要翻译的内容:

"You should define your question better, and I'll explain.

You are trying to fit polynom of second degree (quadratic polynomial function), using series of dates as input, and series of value as output.
The problem, is that you have to define what is 'zero'- your reference point for the date values.
The way you handle that in your code, which is reasonable- but you need to validate that it fits the problem you are trying to solve, is to just look at the 'index' of the date, starting from 0.

When I replace the way you calculate 'bestfit' with the same function you used for generating the graph, I receive similar results to the results you wanted:

Polynomial Equation: 0.22x^2 + -1.02x + 10.63

Two ways that can help you understand the different results I got, from the ones you wanted:

  1. The optional parameter rcond that can be added to the calculation (numpy.polyfit documation)
  2. maybe the numbers you used as y values were rounded, had more decimal points in the original data you used for calculation.

Here is the updated code:


import matplotlib.pyplot as plot
from scipy import stats
import numpy as np

data = [["2020-03-03",9.727273],
["2020-03-04",9.800000],
["2020-03-05",9.727273],
["2020-03-06",10.818182],
["2020-03-07",9.500000],
["2020-03-08",10.909091],
["2020-03-09",15.000000],
["2020-03-10",14.333333],
["2020-03-11",15.333333],
["2020-03-12",16.000000],
["2020-03-13",21.000000],
["2020-03-14",28.833333]]

fig, ax = plot.subplots()

dates = [x[0] for x in data]
usage = [x1 for x in data]

bestfit = np.polyfit(range(len(usage)), usage, 2)

equation = str(round(bestfit[0],2)) + "x**2 + " + str(round(bestfit1,2)) + "x + " + str(round(bestfit2,2))

ax.plot(range(len(usage)), usage)
ax.plot(range(len(usage)), np.poly1d(np.polyfit(range(len(usage)), usage, 2))(range(len(usage)), '--',label=equation)

plot.show()

print (equation)

[![enter image description here][2]][2]

英文:

You should define your question better, and I'll explain.

You are trying to fit polynom of second degree (quadratic polynomial function), using series of dates as input, and series of value as output.
The problem, is that you have to define what is "zero"- your reference point for the date values.
The way you handle that in your code, which is reasonable- but you need to validate that it fits the problem you are trying to solve, is to just look at the 'index' of the date, starting from 0.

When I replace the way you calculate 'bestfit' with the same function you used for generating the graph, I receive similar results to the results you wanted:

Polynomial Equation: 0.22x^2 + -1.02x + 10.63

Two ways that can help you understand the different results I got, from the ones you wanted:

  1. The optional parameter rcond that can be added to the calculation (numpy.polyfit documation)
  2. maybe the numbers you used as y values were rounded, had more decimal points in the original data you used for calculation.

Here is the updated code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

import matplotlib.pyplot as plot
from scipy import stats
import numpy as np

data = [[&quot;2020-03-03&quot;,9.727273],
[&quot;2020-03-04&quot;,9.800000],
[&quot;2020-03-05&quot;,9.727273],
[&quot;2020-03-06&quot;,10.818182],
[&quot;2020-03-07&quot;,9.500000],
[&quot;2020-03-08&quot;,10.909091],
[&quot;2020-03-09&quot;,15.000000],
[&quot;2020-03-10&quot;,14.333333],
[&quot;2020-03-11&quot;,15.333333],
[&quot;2020-03-12&quot;,16.000000],
[&quot;2020-03-13&quot;,21.000000],
[&quot;2020-03-14&quot;,28.833333]]

fig, ax = plot.subplots()

dates = [x[0] for x in data]
usage = [x[1] for x in data]

bestfit = np.polyfit(range(len(usage)), usage, 2)

equation = str(round(bestfit[0],2)) + &quot;x**2 + &quot; + str(round(bestfit[1],2)) + &quot;x + &quot; + str(round(bestfit[2],2)) 

ax.plot(range(len(usage)), usage)
ax.plot(range(len(usage)), np.poly1d(np.polyfit(range(len(usage)), usage, 2))(range(len(usage))), &#39;--&#39;,label=equation)

plot.show()

print (equation)

<!-- end snippet -->获取趋势线的方程(二次多项式)

huangapple
  • 本文由 发表于 2023年3月7日 16:36:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定