machine learning model forecast and behavior of independent variables

I am trying to forecast a regression to time=20. I have to predict the variable called target and the behavior of the remaining independent variables with respect to time has been determined through the function called 'get_function'. The following code works as it should, however, I would like to ask if there is a way to change this line new_data=[[i,eq_x[i],eq_y[i]]] without explicitly writing eq_x eq_y etc... for every single independent variable ?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import xgboost as xgb
import datetime
import seaborn as sns
from sklearn.metrics import r2_score

data=[[1, 1,2 ,5],
        [2, 5,5,6],
        [3, 4,6,6]
        ,[5, 6,5,6],
        [7,9,9,7],
        [8, 7,9,4]
        ,[9, 2,3,8],
        [2, 5,1,9],
        [2,2,10,9]
        ,[3, 8,2,8],
        [6, 5,4,10],
        [6, 8,5 ,10]]

df = pd.DataFrame(data, columns=['time','x','y','target'])

df2=df.groupby(['time']).sum()


#df.insert(0, "time", list(range(8)))
print(df.head(7))
print(df2['target'])
plt.scatter(df['time'],df['y'])
plt.show()
y = (df.target)
X=df.drop(['target'], axis = 1)
x_reg=xgb.XGBRegressor( n_estimators= 1000, max_depth=7, eta= 0.1, colsample_bytree= 0.8, subsample= 0.6)
m_reg=x_reg.fit(X,y)

def get_equation(x,y):
    degree = 2
    coefs, res, _, _, _ = np.polyfit(x,y,degree, full = True)
    ffit = np.poly1d(coefs)
    print (ffit)
    return ffit
eq_x=(get_equation(df['time'],df['x']))
eq_y=(get_equation(df['time'],df['y']))


for i in range(20):
        new_data=[[i,eq_x[i],eq_y[i]]]
        print(new_data)
        new_df=pd.DataFrame(new_data, columns=X.columns)
        pred=m_reg.predict(new_df)
        print(pred)


Read more here: https://stackoverflow.com/questions/68483436/machine-learning-model-forecast-and-behavior-of-independent-variables

Content Attribution

This content was originally published by etckml at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: