Bottom Page

Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 prediction using linear regression (extrapolation?) in a loop
#1
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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Can't make Random Forest Prediction work donnertrud 0 319 May-23-2020, 12:26 PM
Last Post: donnertrud
  Prediction of Coal Fire Power Plant Pollutants Emission Dalpi 2 428 May-08-2020, 06:28 PM
Last Post: Dalpi
  Linear regression doubt - Urgent kumarants 6 497 May-05-2020, 04:11 PM
Last Post: kumarants
  Linear Regression on Time Series karlito 5 608 Jan-28-2020, 10:02 AM
Last Post: buran
  Random Forest high R2 Score but poor prediction donnertrud 5 358 Jan-13-2020, 11:23 PM
Last Post: jefsummers
  How to build linear regression by implementing Gradient Descent using only linear alg PythonSpeaker 1 458 Dec-01-2019, 05:35 PM
Last Post: Larz60+
  Linear Regression Python3 code giving weird solutions deepsen 0 355 Nov-01-2019, 12:06 PM
Last Post: deepsen
  What is wrong with this implementation of the cost function for linear regression? JoeB 1 1,483 Dec-23-2017, 10:05 AM
Last Post: buran
  help on exponential smoothing and linear regression hkyeung 1 1,452 Sep-02-2017, 09:31 PM
Last Post: Larz60+
  Extrapolation bananen 1 8,221 Dec-23-2016, 06:42 AM
Last Post: j.crater

Forum Jump:


Users browsing this thread: 1 Guest(s)