Как у меня было немного времени, для прикола полный пример, основанный на комментарии @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()
Предоставление:

# Fit coefficents.
coef = np.polyfit(data['year'], data['life_exp'], 1)
# Generate predictions for entire series.
data['predicted'] = pd.Series(np.polyval(coef, data['year']))
data[['life_exp', 'predicted']].plot()
Что дает нам желаемый результат:

И мы можем предсказать один год:
# 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
Надеюсь, это правильное использование, и я узнал кое-что, отвечая на это