Answer to Question #322918 in Python for Shilpa

Question #322918

Decision Tree Assignment

1. Load the dataset using pandas

2. Extract data fromOutcome column is a variable named Y

3. Extract data from every column except Outcome column in a variable named X

4. Divide the dataset into two parts for training and testing in 70% and 30% proportion

5. Create and train Decision Tree Model on training set

6. Make predictions based on the testing set using the trained model

7. Check the performance by calculating the confusion matrix and accuracy score of the model


1
Expert's answer
2022-04-04T02:40:38-0400
# Import statements 
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split 
from sklearn.metrics import accuracy_score, confusion_matrix
import pandas as pd


# Read the data.
data = pd.read_csv('data.csv')
# Assign the features to the variable X, and the labels to the variable Y. 
X = data[:,:-1]
Y = data['Outcome']
#splitting thedate
X_train,X_test,Y_train,Y_test = train_test_split(X,Y, test_size=0.3)

model = DecisionTreeClassifier()

#Fit the model.
model.fit(X_train,Y_train)
#Make predictions. Store them in the variable y_pred.
y_pred = model.predict(X_test)

#Calculate the accuracy and assign it to the variable acc.
acc = accuracy_score(Y_test,y_pred)
cm = confusion_matrix(y_pred,Y_test)

print(cm)
print(acc)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS