saherPervaiz commited on
Commit
5cb3e7f
·
verified ·
1 Parent(s): 063744e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -15
app.py CHANGED
@@ -1,19 +1,13 @@
1
  import streamlit as st
2
  import pandas as pd
3
- from utils.data_cleaning import preprocess_data, remove_outliers_iqr, cap_extreme_values, convert_string_to_numeric
4
- from utils.visualizations import (
5
- plot_correlation_heatmap,
6
- plot_histogram,
7
- plot_box_plot,
8
- plot_pair_plot,
9
- plot_scatter_plot,
10
- plot_bar_plot,
11
- )
12
- from utils.model_training import train_all_models
13
- import matplotlib.pyplot as plt
14
  import seaborn as sns
 
15
  import io
16
 
 
 
 
 
17
  # New Function: Combined Histogram and Bar Plot Comparison
18
  def combined_histogram_barplot(df):
19
  """
@@ -42,6 +36,71 @@ def combined_histogram_barplot(df):
42
  plt.tight_layout()
43
  return fig
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  # Streamlit App Title
46
  st.title("Data Analysis, Model Training, and Visualization")
47
 
@@ -82,12 +141,10 @@ if uploaded_file is not None:
82
  st.subheader("Correlation Heatmap")
83
  st.write("Visualizing correlations between numeric features...")
84
  heatmap_fig = plot_correlation_heatmap(df_cleaned)
85
- st.pyplot(heatmap_fig)
86
 
87
  # Save and download heatmap as PNG
88
- heatmap_buffer = io.BytesIO()
89
- heatmap_fig.savefig(heatmap_buffer, format="png") # Save the figure to the buffer
90
- heatmap_buffer.seek(0) # Reset buffer position to the beginning
91
 
92
  st.download_button(
93
  label="Download Correlation Heatmap (PNG)",
 
1
  import streamlit as st
2
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
3
  import seaborn as sns
4
+ import matplotlib.pyplot as plt
5
  import io
6
 
7
+ # Import custom functions from your utils
8
+ from utils.data_cleaning import preprocess_data, remove_outliers_iqr, cap_extreme_values, convert_string_to_numeric
9
+ from utils.model_training import train_all_models
10
+
11
  # New Function: Combined Histogram and Bar Plot Comparison
12
  def combined_histogram_barplot(df):
13
  """
 
36
  plt.tight_layout()
37
  return fig
38
 
39
+ # Plotting Functions
40
+ def plot_correlation_heatmap(df):
41
+ """
42
+ Plot a correlation heatmap for the numeric columns in the dataframe.
43
+ """
44
+ corr = df.corr()
45
+ fig = plt.figure(figsize=(10, 8)) # Create a new figure object
46
+ heatmap = sns.heatmap(corr, annot=True, cmap="coolwarm", fmt=".2f", linewidths=0.5)
47
+ plt.title("Correlation Heatmap")
48
+ return fig # Return the figure object
49
+
50
+ def save_figure_as_png(fig):
51
+ """
52
+ Save the given figure as a PNG file to a BytesIO buffer.
53
+ """
54
+ buffer = io.BytesIO()
55
+ fig.savefig(buffer, format="png") # Save the figure to the buffer
56
+ buffer.seek(0) # Reset the buffer's position to the beginning
57
+ return buffer
58
+
59
+ def plot_histogram(df, column):
60
+ """
61
+ Plot a histogram for a specific column in the dataframe.
62
+ """
63
+ plt.figure(figsize=(8, 6))
64
+ sns.histplot(df[column], kde=True, bins=30, color="skyblue")
65
+ plt.title(f"Histogram of {column}")
66
+ plt.xlabel(column)
67
+ plt.ylabel("Frequency")
68
+ return plt.gcf()
69
+
70
+ def plot_box_plot(df, column):
71
+ """
72
+ Plot a box plot for a specific column in the dataframe.
73
+ """
74
+ plt.figure(figsize=(8, 6))
75
+ sns.boxplot(x=df[column])
76
+ plt.title(f"Box Plot of {column}")
77
+ return plt.gcf()
78
+
79
+ def plot_pair_plot(df):
80
+ """
81
+ Plot a pair plot for numeric columns in the dataframe.
82
+ """
83
+ numeric_columns = df.select_dtypes(include=['float64', 'int64']).columns
84
+ return sns.pairplot(df[numeric_columns])
85
+
86
+ def plot_scatter_plot(df, x_col, y_col):
87
+ """
88
+ Plot a scatter plot between two numeric columns.
89
+ """
90
+ plt.figure(figsize=(8, 6))
91
+ sns.scatterplot(x=df[x_col], y=df[y_col], color="green")
92
+ plt.title(f"Scatter Plot between {x_col} and {y_col}")
93
+ return plt.gcf()
94
+
95
+ def plot_bar_plot(df, column):
96
+ """
97
+ Plot a bar plot for a categorical column.
98
+ """
99
+ plt.figure(figsize=(8, 6))
100
+ sns.countplot(x=df[column])
101
+ plt.title(f"Bar Plot of {column}")
102
+ return plt.gcf()
103
+
104
  # Streamlit App Title
105
  st.title("Data Analysis, Model Training, and Visualization")
106
 
 
141
  st.subheader("Correlation Heatmap")
142
  st.write("Visualizing correlations between numeric features...")
143
  heatmap_fig = plot_correlation_heatmap(df_cleaned)
144
+ st.pyplot(heatmap_fig) # Display the heatmap using Streamlit
145
 
146
  # Save and download heatmap as PNG
147
+ heatmap_buffer = save_figure_as_png(heatmap_fig) # Save the figure to buffer
 
 
148
 
149
  st.download_button(
150
  label="Download Correlation Heatmap (PNG)",