Skip to content

Commit 273335c

Browse files
committed
added core project structure
0 parents  commit 273335c

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
__pycache__/
2+
3+
.taipy/
4+
5+
.vscode/
6+
.idea/
7+
8+
user_data/
9+
10+
*.toml

algos.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import numpy as np
2+
3+
from sklearn.neighbors import KNeighborsClassifier
4+
from sklearn.svm import SVC
5+
from sklearn.linear_model import LogisticRegression
6+
from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier
7+
from sklearn.ensemble import RandomForestClassifier
8+
from sklearn.pipeline import make_pipeline
9+
from sklearn.preprocessing import StandardScaler
10+
from sklearn.tree import DecisionTreeClassifier
11+
from sklearn.naive_bayes import GaussianNB
12+
13+
import plotly.express as px
14+
15+
16+
def fit(X, y, model_name):
17+
18+
if model_name == "RandomForestClassifier":
19+
model = RandomForestClassifier()
20+
elif model_name == "SVC":
21+
model = SVC()
22+
elif model_name == "KNeighborsClassifier":
23+
model = KNeighborsClassifier()
24+
elif model_name == "LogisticRegression":
25+
model = LogisticRegression()
26+
elif model_name == "AdaBoostClassifier":
27+
model = AdaBoostClassifier()
28+
elif model_name == "GradientBoostingClassifier":
29+
model = GradientBoostingClassifier()
30+
elif model_name == "GaussianNB":
31+
model = GaussianNB()
32+
elif model_name == "DecisionTreeClassifier":
33+
model = DecisionTreeClassifier()
34+
35+
return make_pipeline(
36+
StandardScaler(),
37+
model,
38+
).fit(X, y)
39+
40+
41+
def plot(X, y, model):
42+
x1, x2 = X[:, 0], X[:, 1]
43+
44+
# Create a mesh grid
45+
x1_range = np.arange(x1.min()-1, x1.max()+1, 0.01)
46+
x2_range = np.arange(x2.min()-1, x2.max()+1, 0.01)
47+
xx1, xx2 = np.meshgrid(x1_range, x2_range)
48+
49+
# Predict on the mesh grid
50+
y_pred_mesh = model.predict(np.c_[xx1.ravel(), xx2.ravel()]).reshape(xx1.shape)
51+
52+
# Plot the decision boundaries
53+
fig = px.scatter(x=x1, y=x2, color=y)
54+
fig.update_traces(marker={"size": 15})
55+
# Add decision boundaries
56+
fig.add_contour(x=x1_range, y=x2_range, z=y_pred_mesh, showscale=False, opacity=0.2)
57+
58+
return fig

config.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from algos import fit, plot
2+
from taipy import Config
3+
4+
5+
def configure():
6+
X = Config.configure_data_node("X")
7+
y = Config.configure_data_node("y")
8+
model_name = Config.configure_data_node("model_name", default_data="MLPClassifier")
9+
10+
model = Config.configure_data_node("model")
11+
fit_task = Config.configure_task(
12+
id="fit", function=fit, input=[X, y, model_name], output=model, skippable=True
13+
)
14+
15+
fig = Config.configure_data_node("fig")
16+
plot_task = Config.configure_task(
17+
id="plot", function=plot, input=[X, y, model], output=fig, skippable=True
18+
)
19+
20+
scenario = Config.configure_scenario(id="scenario", task_configs=[fit_task, plot_task])
21+
Config.export("scenario.toml")
22+
return scenario

interface.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from taipy.gui import Markdown
2+
3+
selected_scenario = None
4+
figure = None
5+
6+
def on_change(state, var_name):
7+
if var_name == "selected_scenario":
8+
state.figure = state.selected_scenario.fig.read()
9+
10+
root_page = """
11+
<|container|
12+
13+
# Decision region plots from Sklearn models
14+
*Dataset used: make_moon from sklearn.datasets*
15+
16+
<br/>
17+
18+
### Select a model:
19+
20+
<layout_scenario|layout|columns=1 2|
21+
22+
<|{selected_scenario}|scenario_selector|show_add_button=False|>
23+
24+
<scenario|part|render={selected_scenario}|
25+
26+
<|chart|figure={figure}|>
27+
28+
|scenario>
29+
30+
|layout_scenario>
31+
32+
|>
33+
"""
34+
35+
interface = Markdown(root_page)

main.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import taipy as tp
2+
3+
from time import time
4+
from config import configure
5+
from interface import interface
6+
from taipy import Core, Gui
7+
from sklearn.datasets import make_moons
8+
9+
if __name__ == "__main__":
10+
core = Core()
11+
my_scenario = configure()
12+
core.run()
13+
14+
start = time()
15+
16+
dataset = make_moons(noise=0.3, random_state=42)
17+
for model_name in ["RandomForestClassifier", "SVC",
18+
"KNeighborsClassifier", "LogisticRegression",
19+
"AdaBoostClassifier", "GradientBoostingClassifier",
20+
"DecisionTreeClassifier", "GaussianNB"]:
21+
scenario = tp.create_scenario(my_scenario, name=model_name)
22+
23+
scenario.X.write(dataset[0])
24+
scenario.y.write(dataset[1])
25+
scenario.model_name.write(model_name)
26+
27+
tp.submit(scenario)
28+
29+
print(f"Total time {time()-start}")
30+
31+
# Instantiate, configure and run the GUI
32+
gui = Gui(pages={"/": interface})
33+
34+
gui.run(dark_mode=False, port=3559, title="Taipy Scikit Demo App")

0 commit comments

Comments
 (0)