python - 如何使用 for 循环中的一组数据从外推(线性回归?)中获得下一个预测值?
问题描述
对不起,但标题不够清楚,因为我不知道如何用几个词来描述它。
正如您在图像中看到的那样,当 x=7 时,我使用 interp1d 以图形方式“预测”y 的值。我要做的是在 x+1 (8) 时预测 y 的另一个值,以此类推,只要 X 的大小增长到使用 for 循环达到数据集的最后一个值(比如说 100) ?. 喜欢
[1 2 3 4 5 6]
[ 4470.76 25465.72 25465.72 25465.72 21480.59 20024.53]
[1 2 3 4 5 6 7]
[ 4470.76 25465.72 25465.72 25465.72 21480.59 20024.53 15487.45]
[1 2 3 4 5 6 7 8]
[ 4470.76 25465.72 25465.72 25465.72 21480.59 20024.53 15487.45 25654.14]
[1 2 3 4 5 6 7 8 9]
[ 4470.76 25465.72 25465.72 25465.72 21480.59 20024.53 15487.45 25654.14 54874.22]
...
请问有什么想法吗?编辑: csv_file
import pandas as pd
import numpy as np
import os
import scipy.stats as sp
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set(rc={'figure.figsize': (18, 5)})
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
# Load dataset
df = pd.read_csv('data.csv', sep=";", index_col = 'date')
df = df[['pow']]
# Reset index
df = df.reset_index()
df = df[['date', 'pow(+)']]
df.head(10)
X = np.array(pd.to_datetime(df['date'].index.values+1, format='%Y-%m-%d'), dtype=int)#.reshape((-1, 1))
X = X[:6]
y = np.array(df['pow(+)'], dtype=float)#.reshape(-1, 1)
y = y[:6]
print (X)
print (y)
f = interp1d(X, y, fill_value = "extrapolate")
#start, stop , nber of samples to generate, If True, stop is the last sample
X_new = np.linspace(0, 7, num=8, endpoint=True)
plt.plot(X, y, 'o', X_new, f(X_new), '-')
plt.legend(['data', 'linear'], loc='best')
plt.show()
#print('\n')
#print("X shape:", X.shape)
#print("y shape:", y.shape)
解决方案
这不是一项简单的任务,您需要:
- 找到适合您数据的单一或混合功能
- 在此示例中,使用线拟合查找数据的趋势
- 为复杂函数的每个参数设置适当的界限
- 根据拟合参数预测新值
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
import pandas as pd
df = pd.read_csv('df.csv')
x_data = np.array(pd.to_datetime(df['date'].index.values+1, format='%Y-%m-%d'), dtype=int)
y_data = np.array(df['pow'], dtype=float)
# normalise data
y_data = (y_data - np.min(y_data))/ np.max(y_data)
# find data trend
def line_function(x, a, b):
return a*x + b
# fit function
parameters_line, covariance_line = curve_fit(line_function, x_data, y_data, method='lm')
# define fitting function
def fit_function(x, A, t, fi, c, d):
return A*np.sin(x*t + fi)**2 + c*x + d
# set bounds for each parameter
param_bounds = ([0, 0, 0, -1, 0], [2, (2*np.pi/600), 10, parameters_line[0], 10])
# fit function
parameters_fit, covariance_fit = curve_fit(fit_function, x_data, y_data,bounds=param_bounds , method='trf')
A, t, fi, c, d = [value for value in parameters_fit]
# predict new value
x_predict = 900
y_predict = fit_function(x_predict, A, t, fi, c, d)
# plot data
x_fit_data = np.linspace(-100, 1000, 1000)
y_fit_data = fit_function(x_fit_data, A, t, fi, c, d)
plt.plot(x_data, y_data, '.')
plt.plot(x_fit_data, y_fit_data, '-')
y_line_fit_data = line_function(x_fit_data, parameters_line[0], parameters_line[1])
plt.plot(x_fit_data, y_line_fit_data, '--')
plt.plot(x_predict, y_predict, 'o')
plt.show()
输出:
推荐阅读
- reactjs - 我们如何在反应原生文本上制作动画效果?
- security - 如何最好地处理服务器上的 Stripe API 密钥
- python - 如何使“日志”功能起作用,以便只有 log("The log message") 可以工作?
- python - 将 HTML 表单结果发布到 api 并在烧瓶中获得响应
- python - Pandas:如何选择(过滤)子组中的子组?
- c# - 在 web api .net core 3.1 中传递 int 而不是字符串时出现 400 badrequest
- react-native - 我可以免费获得坐标的地点详细信息吗?地理编码/反向地理编码 Api 不是免费的吗?
- neo4j - Neo4j 查询以获取没有父级的文档
- python - 无法打开 lib '/opt/netsuite/odbcclient/lib64/ivoa27.so 无法找到 odbc 驱动程序 (Netsuite)
- python - 如何避免使用此代码收到 HTTP 错误 429:太多请求?