如何使用pandas数据帧预测值?

2024-04-19 23:36:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用best行来预测数据帧中的值。我该怎么做?有没有一个函数,例如,我可以输入一年,并得到预期寿命值?在

Year    Life Expectancy
1930    59.7
1940    62.9
1950    70.2
1965    67.7

我如何计算1948年的价值?在


Tags: 数据函数yearbest价值life寿命expectancy
2条回答

因为我有一点时间,为了好玩,一个基于@ALollz注释的完整示例,使用numpy.polyfit().polyval()。在

% 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()

给予:

enter image description here

^{pr2}$

这给了我们想要的结果:

enter image description here

我们可以预测一年:

# 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

希望这是正确的用法,我学到了一些东西来回答这个问题:)

您可以使用:

import seaborn as sns    
sns.lmplot(data['Year'],data['Life Expectancy'],data)

根据线性回归,这将为你的给定数据拟合一条直线,你也可以计算出任何其他的值,比如1948年的数据等等

有关文档,请参阅: https://seaborn.pydata.org/generated/seaborn.lmplot.html

相关问题 更多 >