0
$\begingroup$

I am writing code for SVR. Therefore, I have generated my code as the requirements. But I am stuck on writing codes for the index of for-loop of X_test and y_test. I have to write code as it should be associated with the line in the datasets just next to the X_train and y_train. So their index should be +1 of the ending index of X_train and y_train.

For Example:

  • In the first iteration (i.e. when i=0), we are using the first 1000 rows for training and the next row (i.e. the 1001st row) for testing

  • In the second iteration (i.e. when i=1), we are using the rows from 1 to 1001 for training and the next row (i.e. the 1002nd row) for testing

  • In the third iteration (i.e. when i=2), we are using the rows from 2 to 1002 for training and the next row (i.e. the 1003rd row) for testing and so on.

My full code:

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

y_pred_list = []
y_test_list = []


for i in range(1, 2000):

    X_train, y_train = dataset.iloc[i:1000+i], target.iloc[i:1000+i]
    X_test, y_test = dataset.iloc[i], target.iloc [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)

    y_pred_list.append(y_pred)

    y_test_list.append(y_test)

print(y_test_list, y_pred_list)

I want to update my code on this below:

 X_test, y_test = dataset.iloc[i], target.iloc [i]

So how may I update this index line as an above requirement?

$\endgroup$
4
  • $\begingroup$ Are you trying to do cross-validation? As per the code, it appears you want to fit 2000 SVR models. That seems like overkill. $\endgroup$ Commented Jun 8, 2020 at 23:35
  • $\begingroup$ @Suren Thanks for your kind response actually I want update this line. This line is incorrect. X_test, y_test = dataset.iloc[i:1001+i], target.iloc [i:1001+i]. Please help me to solve this problem $\endgroup$
    – As if
    Commented Jun 9, 2020 at 0:36
  • $\begingroup$ I think it is better if you can explain in words what you intend to do here. It is not clear to me. As i range upto 2000, the iloc exceed the size of the data. $\endgroup$ Commented Jun 9, 2020 at 5:44
  • $\begingroup$ For example in the first iteration (i.e. when i=0), I want to use the first 1000 rows for training and the next row (i.e. the 1001st row) for testing #In the second iteration (i.e. when i=1), I want to use rows from 1 to 1001 for training and the next row (i.e. the 1002nd row) for testing #in the third iteration (i.e. when i=2), I want to use the rows from 2 to 1002 for training and the next row (i.e. the 1003rd row) for testing and so on. $\endgroup$
    – As if
    Commented Jun 9, 2020 at 21:27

1 Answer 1

0
$\begingroup$

I've changed the range values in the loop and indexing for training and test data. (Also, it seems you are using the dataset instead of data by mistake.)

i:(999 + i) contains thought rows locations of which will be incremented by one as the loop progresses.

for i in range(0, 999):

    X_train, y_train = data.iloc[i:(999 + i)], target.iloc[i:(999 + i)]
    X_test, y_test = data.iloc[i + 1000], target.iloc [i + 1000]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.