0
$\begingroup$

I know little on how random forest works. Usually in classification I could fit the train data into the random forest classifier and ask to predict the test data.

Currently I am working on titanic data that is provided to me. This is a top rows of the data set and there are 1300(approx) rows.

survived pclass sex age sibsp parch fare embarked 0 1 1 female 29 0 0 211.3375 S 1 1 1 male 0.9167 1 2 151.55 S 2 0 1 female 2 1 2 151.55 S 3 0 1 male 30 1 2 151.55 S 4 0 1 female 25 1 2 151.55 S 5 1 1 male 48 0 0 26.55 S 6 1 1 female 63 1 0 77.9583 S 7 0 1 male 39 0 0 0 S 8 1 1 female 53 2 0 51.4792 S 9 0 1 male 71 0 0 49.5042 C 10 0 1 male 47 1 0 227.525 C 11 1 1 female 18 1 0 227.525 C 12 1 1 female 24 0 0 69.3 C 13 1 1 female 26 0 0 78.85 S

There is no test data given. So I want random forest to predict the survival on entire data set and compare it with actual value (more like checking the accuracy score).

So what I have done is divide my complete dataset into test and train. Train consists all the columns except survived and test consists survived column.

dfFeatures = df['survived']
dfTarget = dfCopy.drop('survived', 1)

Note: df is the entire dataset.

Here is the code that checks the score of randomforest

rfClf = RandomForestClassifier(n_estimators=100, max_features=10)
rfClf = rfClf.fit(dfFeatures, dfTarget)
scoreForRf = rfClf.score(dfFeatures, dfTarget)

I get the score output with something like this

The accuracy score for random forest is :  0.983193277311

I am finding it little difficult to understand what is happening behind the code in above given code.

Does, it predict survival for all the tuples based upon other features (dfFeatures) and compare it with test data(dfTarget) and give the prediction score or does it randomly create train and test data based upon the train data provided and compare accuracy for test data it generated behind?

To be more precise, while calculating the accuracy score does it predict the survival for entire data set or just random partial data set?

$\endgroup$
1
$\begingroup$

To be more precise, while calculating the accuracy score does it predict the survival for entire data set or just random partial data set?

Entire data set.

So what I have done is divide my complete dataset into test and train. Train consists all the columns except survived and test consists survived column.

dfWithTestFeature = df['survived']

dfWithTrainFeatures = dfCopy.drop('survived', 1)

This is not a train/test split. What you're calling 'train' dataset is actually features and 'test' - target. Train/test split is "horizontal", not "vertical". scikit-learn provides train_test_split function to do this:

y = df['survived']    
X = df.drop('survived', 1)

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, t_test = train_test_split(X, y, test_size=0.3)

# ... preprocessing ...

# Training:
rfClf.fit(X_train, y_train)

# Testing:  
rfClf.score(X_test, y_test)

In this case model was trained using train subset (random 70% of the entire dataset) and then evaluated on the test subset (remaining 30%). More details can be found in scikit-learn user guide.

$\endgroup$
  • $\begingroup$ Yes I do know there is train test split. But what if there is not test set? For me I want prediction on entire data set and I want to see how accurately a classifier classifies the data set. In this case random forest $\endgroup$ – Cybercop Jan 7 '17 at 17:59
  • $\begingroup$ This is not an answer and the question is not very clear. For any type of classification method you need a test set to evaluate performance of the classifier in an unbiased way. $\endgroup$ – Michael Chernick Jan 7 '17 at 18:08
  • $\begingroup$ @Cybercop "But what if there is not test set?" Then train/test split will provide you one. "I want prediction on entire data set and I want to see how accurately a classifier classifies the data set" Your code does exactly this. Note, however, that it's likely that this classifier won't perform nearly as good on the samples not used during the training. $\endgroup$ – Alex Filatov Jan 7 '17 at 18:18
  • $\begingroup$ @Alex so what you are saying is. My code actually predicts the survival on entire data set and gives score based on prediction of entire data set? $\endgroup$ – Cybercop Jan 7 '17 at 18:24
  • $\begingroup$ @MichaelChernick I agree that the question is a little confusing. I think the confusion is caused by incorrectly used terminology (train/test split vs. features and targets). I tried to answer the question asked in the last paragraph and then correct the author. $\endgroup$ – Alex Filatov Jan 7 '17 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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