-1

I have the data frame i.e. df. Which has more than 2000 rows and 15 columns including expense column. I have completed all of the processes for SVR. Now I want to make the training set to include the rows until row number 1000.

The test set will have one single row, row number 1001.

For preparing the training and testing the model I need to take the expense column as the target value and all other columns will be taken as the features.

But I know the method for splitting it into training(50%) and testing set(50%). Which I included below:

from sklearn.svm import SVR
import pandas
import sklearn
csv = pandas.read_csv('data.csv')
train, test = sklearn.cross_validation.train_test_split(csv, train_size = 0.5)

If I prepare the training and testing set as above mention, how may I write code?

0

1 Answer 1

1

Is this what you want?

csv = pandas.read_csv('data.csv')
train, test = csv.loc[:1000], csv.loc[1001]

To then split the train test sets further into X and Y, simply do:

train_X, train_Y = train.drop(columns=['expense']), test['expense']
test_X, test_Y = train.drop(columns=['expense']), test['expense']

If instead you want to use the column 'use' as a response, the following should work:

train, test = dataset.loc[:1000], dataset.loc[1001]

train_X, train_y = train.drop(columns=['use']), train['use']
test_X, test_y = test.drop(columns=['use']), test['use']

SupportVectorRefModel = SVR()
SupportVectorRefModel.fit(train_X, train_y)
SupportVectorRefModel.predict(test_X)
12
  • from sklearn.model_selection import train_test_split from sklearn.svm import SVR #X = dataset.loc[:1000] #y = dataset.loc[1001]
    – user13463024
    May 15, 2020 at 19:58
  • I updated my answer such that it also includes splitting X and Y. May 15, 2020 at 20:01
  • I know replaced df.loc[...] with of csv.loc[...] to my code. Let me know if it works this way. May 15, 2020 at 20:26
  • If this is your full code, then obviously test is not defined since you never defined it. Why don't you do it as I suggested? I am quite sure it works. May 15, 2020 at 20:52
  • train_X, train_Y = train.drop(columns=['expense']), test['expense'] test_X, test_Y = train.drop(columns=['expense']), test['expense'] This line seems that the expense column is dropped from entire dataset. But I need to drop them from the variables which are obtained in the previous lines. So it needs to use the variables "X_train, y_train, X_test, y_test" in lines 247 and 248 appropriately. Could you give instructions for these purposes?
    – user13463024
    May 16, 2020 at 20:36

Your Answer

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