prithivMLmods commited on
Commit
a5366ac
1 Parent(s): 154ed1d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ import plotly.graph_objects as go
5
+ from sklearn.model_selection import train_test_split
6
+ from sklearn.preprocessing import StandardScaler
7
+ from sklearn.neural_network import MLPRegressor
8
+
9
+ def analyze_data(csv_file):
10
+ # Read the CSV file
11
+ df = pd.read_csv(csv_file.name)
12
+
13
+ if len(df.columns) < 3:
14
+ return "Error: CSV file must have at least 3 columns for analysis."
15
+
16
+ # Use the first three columns for x, y, z
17
+ x, y, z = df.iloc[:, 0], df.iloc[:, 1], df.iloc[:, 2]
18
+
19
+ # Create the original data plot
20
+ original_fig = go.Figure(data=[go.Scatter3d(
21
+ x=x, y=y, z=z, mode='markers',
22
+ marker=dict(size=5, color=z, colorscale='Viridis', opacity=0.8)
23
+ )])
24
+ original_fig.update_layout(title="Original Data: 3,000 points", scene=dict(
25
+ xaxis_title=df.columns[0], yaxis_title=df.columns[1], zaxis_title=df.columns[2]))
26
+
27
+ # Prepare data for modeling
28
+ X = df.iloc[:, :2]
29
+ y = df.iloc[:, 2]
30
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
31
+
32
+ scaler = StandardScaler()
33
+ X_train_scaled = scaler.fit_transform(X_train)
34
+ X_test_scaled = scaler.transform(X_test)
35
+
36
+ # Create a grid for predictions
37
+ xx, yy = np.meshgrid(np.linspace(X.iloc[:, 0].min(), X.iloc[:, 0].max(), 100),
38
+ np.linspace(X.iloc[:, 1].min(), X.iloc[:, 1].max(), 100))
39
+ grid = np.c_[xx.ravel(), yy.ravel()]
40
+ grid_scaled = scaler.transform(grid)
41
+
42
+ # Train different models and create plots
43
+ models = [
44
+ ("Good model", MLPRegressor(hidden_layer_sizes=(10, 5), max_iter=1000, random_state=42)),
45
+ ("Underfitting", MLPRegressor(hidden_layer_sizes=(2,), max_iter=10, random_state=42)),
46
+ ("Overfitting", MLPRegressor(hidden_layer_sizes=(100, 100), max_iter=1000, random_state=42)),
47
+ ("4-layer NN", MLPRegressor(hidden_layer_sizes=(50, 30, 20), max_iter=100000, random_state=42))
48
+ ]
49
+
50
+ figs = []
51
+ for name, model in models:
52
+ model.fit(X_train_scaled, y_train)
53
+ z_pred = model.predict(grid_scaled).reshape(xx.shape)
54
+
55
+ fig = go.Figure(data=[go.Surface(x=xx, y=yy, z=z_pred, colorscale='Viridis')])
56
+ fig.update_layout(title=f"{name}", scene=dict(
57
+ xaxis_title=df.columns[0], yaxis_title=df.columns[1], zaxis_title=df.columns[2]))
58
+ figs.append(fig)
59
+
60
+ return original_fig, figs[0], figs[1], figs[2], figs[3]
61
+
62
+ # Create Gradio interface
63
+ iface = gr.Interface(
64
+ fn=analyze_data,
65
+ inputs=gr.File(label="Upload CSV file"),
66
+ outputs=[
67
+ gr.Plot(label="Original Data"),
68
+ gr.Plot(label="1. Good model"),
69
+ gr.Plot(label="2. Underfitting"),
70
+ gr.Plot(label="3. Overfitting"),
71
+ gr.Plot(label="4. 4-layer Neural Network")
72
+ ],
73
+ title="Advanced 3D Graph Analyzer",
74
+ description="Upload a CSV file with at least 3 columns to create various 3D graph visualizations and model predictions."
75
+ )
76
+
77
+ # Launch the app
78
+ iface.launch()