yashkavaiya commited on
Commit
4e0eaa3
1 Parent(s): 24ae2da

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import matplotlib.pyplot as plt
3
+ from sklearn import datasets
4
+ from sklearn.tree import DecisionTreeClassifier, plot_tree
5
+ from io import BytesIO
6
+ from PIL import Image
7
+
8
+ # Load available datasets
9
+ dataset_names = ["iris", "wine", "breast_cancer", "digits"]
10
+ datasets_dict = {
11
+ "iris": datasets.load_iris(),
12
+ "wine": datasets.load_wine(),
13
+ "breast_cancer": datasets.load_breast_cancer(),
14
+ "digits": datasets.load_digits(),
15
+ }
16
+
17
+ # Define the function to visualize the decision tree
18
+ def visualize_decision_tree(dataset_name, max_depth, min_samples_split, min_samples_leaf, max_features, criterion, splitter, max_leaf_nodes, random_state):
19
+ dataset = datasets_dict[dataset_name]
20
+ X, y = dataset.data, dataset.target
21
+ clf = DecisionTreeClassifier(max_depth=max_depth, min_samples_split=min_samples_split, min_samples_leaf=min_samples_leaf, max_features=max_features, criterion=criterion, splitter=splitter, max_leaf_nodes=max_leaf_nodes, random_state=random_state)
22
+ clf.fit(X, y)
23
+ fig, ax = plt.subplots(figsize=(10, 8))
24
+ plot_tree(clf, feature_names=dataset.feature_names, class_names=dataset.target_names, filled=True, ax=ax)
25
+ buf = BytesIO()
26
+ fig.savefig(buf, format='png')
27
+ buf.seek(0)
28
+ image_data = buf.getvalue()
29
+ image = Image.open(BytesIO(image_data))
30
+ return image
31
+
32
+ # Define the hyperparameters and their ranges
33
+ max_depth_range = [None, 2, 3, 4, 5, 6, 7, 8, 9, 10]
34
+ min_samples_split_range = [2, 3, 4, 5, 6, 7, 8, 9, 10]
35
+ min_samples_leaf_range = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
36
+ max_features_range = [None, 'sqrt', 'log2', 0.1, 0.2, 0.3, 0.4, 0.5]
37
+ criterion_range = ['gini', 'entropy']
38
+ splitter_range = ['best', 'random']
39
+ max_leaf_nodes_range = [None, 2, 3, 4, 5, 6, 7, 8, 9, 10]
40
+ random_state_range = [None, 42, 100, 200, 300, 400, 500, 600]
41
+
42
+ # Create the Gradio interface
43
+ dataset_dropdown = gr.components.Dropdown(choices=dataset_names, label="Dataset", value="iris")
44
+ max_depth_dropdown = gr.components.Dropdown(choices=max_depth_range, label="Max Depth", value=None)
45
+ min_samples_split_dropdown = gr.components.Dropdown(choices=min_samples_split_range, label="Min Samples Split", value=2)
46
+ min_samples_leaf_dropdown = gr.components.Dropdown(choices=min_samples_leaf_range, label="Min Samples Leaf", value=1)
47
+ max_features_dropdown = gr.components.Dropdown(choices=max_features_range, label="Max Features", value=None)
48
+ criterion_dropdown = gr.components.Dropdown(choices=criterion_range, label="Criterion", value="gini")
49
+ splitter_dropdown = gr.components.Dropdown(choices=splitter_range, label="Splitter", value="best")
50
+ max_leaf_nodes_dropdown = gr.components.Dropdown(choices=max_leaf_nodes_range, label="Max Leaf Nodes", value=None)
51
+ random_state_dropdown = gr.components.Dropdown(choices=random_state_range, label="Random State", value=None)
52
+
53
+ iface = gr.Interface(
54
+ fn=visualize_decision_tree,
55
+ inputs=[dataset_dropdown, max_depth_dropdown, min_samples_split_dropdown, min_samples_leaf_dropdown, max_features_dropdown, criterion_dropdown, splitter_dropdown, max_leaf_nodes_dropdown, random_state_dropdown],
56
+ outputs=gr.Image(type="pil"),
57
+ title="Decision Tree Visualization",
58
+ description="Visualize a decision tree classifier on various datasets by adjusting hyperparameters."
59
+ )
60
+
61
+ iface.launch()