% matplotlib inline
import pandas as pd
import numpy as np
# Generate some test data with a trend.
data = pd.DataFrame(
{
'year': list(range(1900, 2000)),
'life_exp': np.linspace(50, 80, 100) * ((np.random.randn(100, ) * 0.1) + 1)
}
)
data[['life_exp']].plot()
给予:
^{pr2}$
这给了我们想要的结果:
我们可以预测一年:
# Passing in a single year.
x = 1981
print('Predicted life expectancy for {}: {:.2f} years'.format(x, np.polyval(coef, x)))
给出:Predicted life expectancy for 1981: 72.40 years
因为我有一点时间,为了好玩,一个基于@ALollz注释的完整示例,使用
numpy.polyfit()
和.polyval()
。在给予:
^{pr2}$
这给了我们想要的结果:
我们可以预测一年:
给出:
Predicted life expectancy for 1981: 72.40 years
希望这是正确的用法,我学到了一些东西来回答这个问题:)
您可以使用:
根据线性回归,这将为你的给定数据拟合一条直线,你也可以计算出任何其他的值,比如1948年的数据等等
有关文档,请参阅: https://seaborn.pydata.org/generated/seaborn.lmplot.html
相关问题 更多 >
编程相关推荐