I have datasets that have more than 2000 rows and 23 columns including the age column. I have generated X_train for SVR. Now, I need to generate X_train by taking the rows 2-1001 of the dataset. Basically I shift my X_train by one step on the dataset. And the other data i.e. y_train, X_test, y_test should be shifted as well. Once it is completed I need to do it for the rows 3-1002. And I need to follow this procedure until the end of the dataset.
#I need to put the below lines in a for loop
X_train, y_train = data.loc[:1000], target.loc[:1000]
X_test, y_test = data.loc[1001], target.loc[1001]
X_test = np.array(X_test).reshape(1, -1)
print(X_test.shape)
SupportVectorRefModel = SVR()
SupportVectorRefModel.fit(X_train, y_train)
y_pred = SupportVectorRefModel.predict(X_test)
y_pred
#in a for loop and change the indexes in lines
X_train, y_train = data.loc[:1000], target.loc[:1000] and
X_test, y_test = data.loc[1001], target.loc[1001]
that is at every iteration of the loop. Now I am stuck down in this situation. My way of wringing is correct or not? How may I write the code for this condition?
Code from the beginning:
import pandas as pd
import numpy as np
# Make fake dataset
dataset = pd.DataFrame(data= np.random.rand(2000,22))
dataset['age'] = np.random.randint(2, size=2000)
# Separate the target from the other features
target = dataset['age']
data = dataset.drop('age', axis = 1)
X_train, y_train = data.loc[:1000], target.loc[:1000]
X_test, y_test = data.loc[1001], target.loc[1001]
X_test = np.array(X_test).reshape(1, -1)
print(X_test.shape)
SupportVectorRefModel = SVR()
SupportVectorRefModel.fit(X_train, y_train)
y_pred = SupportVectorRefModel.predict(X_test)
y_pred
Code for current work:
for i in range(1, 1001):
X_train, y_train = dataset.loc[:1000], target.loc[:1000]
X_test, y_test = dataset.loc[i], target.loc[i]
X_test = np.array(X_test).reshape(1, -1)
print(X_test.shape)
SupportVectorRefModel = SVR()
SupportVectorRefModel.fit(X_train, y_train)
y_pred = SupportVectorRefModel.predict(X_test)
Output: KeyError: 1