Get startedGet started for free

Train model and do prediction using Decision Tree

Let’s make first Decision Tree model. Similar to Logistic regression, we first select the input features, train our model and finally perform prediction on test data set.

Ok! time for you to build your first Decision Tree model! The pre processed trainmodified and testmodifed data are available in your workspace.

This exercise is part of the course

Introduction to Python & Machine Learning (with Analytics Vidhya Hackathons)

View Course

Exercise instructions

  • Store input variable in list "predictors"
  • Create a object of DecisionTreeClassifier
  • Do prediction for test data set
  • Export test prediction to csv file

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

#train_modified and test_modified already loaded in the workspace
#Import module for Decision tree
import sklearn.tree

# Select three predictors Credit_History, Education and Gender
predictors =[____,_____,_____]

# Converting predictors and outcome to numpy array
x_train = train_modified[predictors].values
y_train = train_modified['Loan_Status'].values

# Model Building
model = sklearn._____.DecisionTreeClassifier()
model.fit(x_train, y_train)

# Converting predictors and outcome to numpy array
x_test = test_modified[predictors].values

#Predict Output
predicted= model._____(x_test)

#Reverse encoding for predicted outcome
predicted = number.inverse_transform(predicted)

#Store it to test dataset
test_modified['Loan_Status']=predicted

#Output file to make submission
test_modified.______("Submission1.csv",columns=['Loan_ID','Loan_Status'])

Edit and Run Code