Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split data frame into x and y

Tags:

python

numpy

I am splitting the data into training data and testing data like so:

train, test = train_test_split(dataFrame(), test_size=0.2)

Which works wonders, my training data frame looks like this:

     PassengerId  Survived  SibSp  Parch
77            78         0      0      0
748          749         0      1      0
444          445         1      0      0
361          362         0      1      0
576          577         1      0      0
27            28         0      3      2
232          233         0      0      0
424          425         0      1      1
785          786         0      0      0
…            …           …      …      … 

I am now attempting to get the X and Y columns, X being my SibSp column and Y being my Parch column. After following many tutorials on Regression and training my AI, every person "split" the columns into x and y like so:

x = train[:, 0:2]

However, after many variations and googling, I cannot solve this error this line is giving me nor understand it:

TypeError: unhashable type: 'slice'

How can I split the SibSp column into an array of x and the Parch column into an array of y within my training data frame?

like image 499
Danny_P Avatar asked Dec 31 '18 20:12

Danny_P


People also ask

How do you split a data frame in two variables?

You can also do the following: split(x = df, f = ~ var1 + var2...) This way, you can also achieve the same split dataframe by many variables without using a list in the f parameter.


1 Answers

The correct way to slice is x = train.iloc[:, 0:2].

like image 186
Xiaoyu Lu Avatar answered Oct 18 '22 21:10

Xiaoyu Lu