Question

In: Computer Science

Run the following code and answer the following questions: (a) How do accuracy change with changing...

Run the following code and answer the following questions:

(a) How do accuracy change with changing the tree maximum depth? [Your answer]

(b) What are the ways to reduce overfitting in a decision tree? [Your answer]

from sklearn import datasets
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.tree import plot_tree


iris = datasets.load_iris()
X = iris.data[:, [2, 3]]
y = iris.target


X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=1, stratify=y)

tree = DecisionTreeClassifier(criterion='entropy',
max_depth=10,
random_state=1)
tree.fit(X_train, y_train)

y_pred = tree.predict(X_test)
test_accuracy = metrics.accuracy_score(y_test, y_pred)
print("Test accuracy of decision tree classifier on Iris dataset: "+str(test_accuracy))

plt.figure(figsize=(10, 7))

plot_tree(tree,
filled=True,
rounded=True,
class_names=['Setosa',
'Versicolor',
'Virginica'],
feature_names=['petal length',
'petal width'])

plt.show()

Solutions

Expert Solution

How do accuracy change with changing the tree maximum depth?

ans-> Max depth of tree simply states that if we increase the depth of tree , the maximum we allow tree to grow the more complex our model will become. Higher the max_depth highly complex model will be there.

Hence for the training data,our accuracy increases as model complexity increases, but for the testing set our model might get overfits,and accuracy of the l will be lesser. So it is recommended to use random search cross-validation and grid-search cv to find the best value of parameter max_depth in order to get high accuracy.

* max_depth => very high then model get overfits

* max_depth => very low then model get underfits

Q. What are the ways to reduce overfitting in a decision tree?

Solution->

one way to reduce overfitting is to uses pruning. It means changing the various important parameter like max_depth and min_samples_split and min_samples_leaf and observe the accuracy of model according .

min_samples_split: min number of sample required to do split at each node, taking it too high causes overfitting and taking too low causes underfitting.

max_depth : HIgher is the max_depth higher will be number of sample split, so it may cause overfitting so choose it accordingly , you can use random search Cv(cross validation ).

I have corrected your below code and now its working fine and showing the accuracy as well and showing the decision tree as well.


Related Solutions

Compile and run the following code then answer the following questions: a.) Run this command from...
Compile and run the following code then answer the following questions: a.) Run this command from the shell prompt: ./a.out ls -F Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1); b.) Run this command from the shell prompt:...
Question 3. In the following code, answer these questions: Analyze the code and how it works?...
Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how? #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int changed = 0; char buff[8]; while (changed == 0){ gets(buff); if (changed !=0){ break;} else{     printf("Enter again: ");     continue; } }      printf("the 'changed' variable is modified\n %d", changed); } END of the questions :::::::::::::::::::::::::: :::::::::::::::::::::::::: Submission...
In at least 100 words answer the following questions on how Facebook is changing the internet:...
In at least 100 words answer the following questions on how Facebook is changing the internet: How do you think that Facebook should balance its responsibilities to its shareholders - to maximize profits - with its responsibilities to its other stakeholders (e.g., its users, and the citizens of the countries in which it operates)? For instance, Facebook makes more money when controversial content is posted, but what if that content is "fake news"?  Does Facebook have any responsibility for ensuring that...
Use sufficient detail and depth to answer the following questions on how Facebook is changing the...
Use sufficient detail and depth to answer the following questions on how Facebook is changing the internet: How do you think that Facebook should balance its responsibilities to its shareholders - to maximize profits - with its responsibilities to its other stakeholders (e.g., its users, and the citizens of the countries in which it operates)? For instance, Facebook makes more money when controversial content is posted, but what if that content is "fake news"? Does Facebook have any responsibility for...
Answer the following questions 4.1  People do not expect the price level to change. Then the Reserve...
Answer the following questions 4.1  People do not expect the price level to change. Then the Reserve Bank unexpectedly cuts the interest rate so that the quantity of money unexpectedly increases. What is the effect on the price level and real GDP? Be sure to explain what happens to the aggregate demand curve and short-run aggregate supply curve. [3 marks] 4.2  Explain how the events in part (a) could lead to a demand-pull inflation spiral. [2 marks] 4.3  Monetary and fiscal policy are...
Answer the following questions comparing monopolistic competition to perfect competition. a. In the long run, how...
Answer the following questions comparing monopolistic competition to perfect competition. a. In the long run, how does the profit earned by a firm operating in a monopolistically competitive market compare to the profit the same firm would earn if it were instead operating in a perfectly competitive market? Please explain. b. In the long run, how does the average cost of production for a firm operating in a monopolistically competitive market compare to the average cost of production for the...
Answer the following questions regarding project management: -How difficult it is to change a culture where...
Answer the following questions regarding project management: -How difficult it is to change a culture where project management is perceived as of low status and something to get out of? How would you approach such a task? -What is the problem with the mentality of admiring heroic rescues of projects in trouble? -What are the similarities and differences between the skills sought for project managers among business communication system's leadership continuity plan -If striving to meet schedule or budget isn't...
How do we change source code on java programming? Thanks
How do we change source code on java programming? Thanks
Answer the following questions comparing monopolistic competition to perfect competition. I. In the long run, how...
Answer the following questions comparing monopolistic competition to perfect competition. I. In the long run, how does the profit earned by a firm operating in a monopolistically competitive market compare to the profit the same firm would earn if it were instead operating in a perfectly competitive market?Please explain.b. In the long run, how does the average cost of production for a firm operating in a monopolistically competitive market compare to the average cost of production for the same firm...
  Answer the questions related to the following code:             Class B             {      &nbs
  Answer the questions related to the following code:             Class B             {                         Public:                                     Void b1();                         Protected:                                     Void b2();             };             Class A : public B             {                         Public:                                     Void  a1();                         Protected: Void  a2();             };             Class C: public A               {                         Public:                                     Void c1();             }; Void main () { B temp1; A temp2; C temp3; }    Name all member functions of all classes visible through temp1 in the main function? Name all member functions of all classes visible through temp2 in the main function? Name all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT