pandas 如何执行线性近似并从python中的数据数组中获得线性方程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26109749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:31:25  来源:igfitidea点击:

how to perform a linear approximation and get linear equation from an array of data in python

pythonnumpymatplotlibpandasscipy

提问by Dhruv Patel

I want to know how is it possible to perform a linear approximation and get the linear equation from the array of data in python.

我想知道如何执行线性近似并从 python 中的数据数组中获得线性方程。

i.e. It would be something like

即它会是这样的

linapprox((0,0),(1,1),(2,1.9),(3,3.1))
> y = x

回答by atomh33ls

polyfitwould work

polyfit会工作

x=np.arange(0,4)
y=np.array([0,1,1.9,3.1])
np.polyfit(x,y,1)

Gives

array([ 1.02, -0.03])

The two values are m and c where y = mx + c

这两个值是 m 和 c,其中 y = mx + c

You could round this:

你可以解决这个问题:

np.round(np.polyfit(x,y,1))

to give what you wanted:

给你想要的:

array([ 1.,  0.])