Python Forum
prediction using linear regression (extrapolation?) in a loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: prediction using linear regression (extrapolation?) in a loop (/thread-24231.html)



prediction using linear regression (extrapolation?) in a loop - karlito - Feb-05-2020

Hey!
sorry but the title is not clear enough because I didn't know how to describe it with few words.

As you can see in the image I have used interp1d to graphically "predict" the value of y when x=7.
What I'm trying to do is to predict another value of y when x+1 (8) and so on any time the size of X grows up till the last value of the dataset is reached(let's say 100) using a for loop?. like


[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]
...
Any ideas, please?

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)
graphic