saherPervaiz commited on
Commit
9369add
·
verified ·
1 Parent(s): dd79f94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -54
app.py CHANGED
@@ -1,75 +1,86 @@
1
  import streamlit as st
2
  import pandas as pd
3
  from utils.data_cleaning import handle_missing_values, remove_outliers_iqr, cap_extreme_values
4
- from utils.visualizations import plot_correlation_heatmap, save_plot_as_png
5
  from utils.model_training import train_all_models
6
  import io
7
 
8
  # Streamlit App Title
9
- st.title("Model Training with Metrics and Correlation Heatmap")
10
 
11
- # File uploader with unique keys
12
- uploaded_file = st.file_uploader("Upload a CSV file for data analysis", type=["csv"], key="file_uploader_1")
13
 
14
  if uploaded_file is not None:
15
- # Read the uploaded file into a DataFrame
16
  df = pd.read_csv(uploaded_file)
17
- st.write("Dataset Uploaded Successfully!")
18
  st.dataframe(df)
19
 
20
- # Clean Data: Missing values, outliers, and extreme values
21
- st.subheader("Data Cleaning")
22
- df_cleaned = handle_missing_values(df)
23
- df_cleaned = remove_outliers_iqr(df_cleaned)
24
- df_cleaned = cap_extreme_values(df_cleaned)
 
 
25
 
26
- st.write("Cleaned Dataset:")
27
- st.dataframe(df_cleaned)
28
 
29
- # Add download option for the cleaned dataset
30
- st.subheader("Download Cleaned Dataset")
31
- st.download_button(
32
- label="Download Cleaned Dataset (CSV)",
33
- data=df_cleaned.to_csv(index=False),
34
- file_name="cleaned_dataset.csv",
35
- mime="text/csv"
36
- )
37
 
38
- # Correlation Heatmap
39
- st.subheader("Correlation Heatmap")
40
- corr_plot = plot_correlation_heatmap(df_cleaned)
41
- st.pyplot(corr_plot)
 
42
 
43
- # Save heatmap as PNG
44
- heatmap_buffer = io.BytesIO()
45
- corr_plot.savefig(heatmap_buffer, format='png')
46
- heatmap_buffer.seek(0)
47
 
48
- # Download Button for Heatmap
49
- st.download_button(
50
- label="Download Correlation Heatmap as PNG",
51
- data=heatmap_buffer,
52
- file_name="correlation_heatmap.png",
53
- mime="image/png"
54
- )
55
 
56
- # Target and Feature Selection
57
- st.subheader("Select Target and Features")
58
- target = st.selectbox("Select Target Variable", df_cleaned.columns)
59
- features = [col for col in df_cleaned.columns if col != target]
60
- X = df_cleaned[features]
61
- y = df_cleaned[target]
62
 
63
- # Train and Evaluate Models
64
- st.subheader("Model Training and Evaluation")
65
- model_results = train_all_models(X, y) # Train all models based on data type
66
- st.write("Model Training Results:")
67
- st.dataframe(model_results)
68
 
69
- # Add download option for model results
70
- st.download_button(
71
- label="Download Model Results (CSV)",
72
- data=model_results.to_csv(),
73
- file_name="model_results.csv",
74
- mime="text/csv"
75
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  from utils.data_cleaning import handle_missing_values, remove_outliers_iqr, cap_extreme_values
4
+ from utils.visualizations import plot_correlation_heatmap
5
  from utils.model_training import train_all_models
6
  import io
7
 
8
  # Streamlit App Title
9
+ st.title("Data Analysis, Model Training, and Visualization")
10
 
11
+ # File Uploader
12
+ uploaded_file = st.file_uploader("Upload a CSV file for data analysis", type=["csv"])
13
 
14
  if uploaded_file is not None:
15
+ # Load dataset
16
  df = pd.read_csv(uploaded_file)
17
+ st.write("### Dataset Preview")
18
  st.dataframe(df)
19
 
20
+ try:
21
+ # Data Cleaning
22
+ st.subheader("Data Cleaning")
23
+ st.write("Handling missing values, removing outliers, and capping extreme values...")
24
+ df_cleaned = handle_missing_values(df)
25
+ df_cleaned = remove_outliers_iqr(df_cleaned)
26
+ df_cleaned = cap_extreme_values(df_cleaned)
27
 
28
+ st.write("### Cleaned Dataset")
29
+ st.dataframe(df_cleaned)
30
 
31
+ # Download option for cleaned dataset
32
+ st.download_button(
33
+ label="Download Cleaned Dataset (CSV)",
34
+ data=df_cleaned.to_csv(index=False),
35
+ file_name="cleaned_dataset.csv",
36
+ mime="text/csv"
37
+ )
 
38
 
39
+ # Correlation Heatmap
40
+ st.subheader("Correlation Heatmap")
41
+ st.write("Visualizing correlations between numeric features...")
42
+ heatmap_plot = plot_correlation_heatmap(df_cleaned)
43
+ st.pyplot(heatmap_plot)
44
 
45
+ # Save and download heatmap as PNG
46
+ heatmap_buffer = io.BytesIO()
47
+ heatmap_plot.savefig(heatmap_buffer, format="png")
48
+ heatmap_buffer.seek(0)
49
 
50
+ st.download_button(
51
+ label="Download Correlation Heatmap (PNG)",
52
+ data=heatmap_buffer,
53
+ file_name="correlation_heatmap.png",
54
+ mime="image/png"
55
+ )
 
56
 
57
+ # Select Target and Features
58
+ st.subheader("Feature and Target Selection")
59
+ target = st.selectbox("Select Target Variable", df_cleaned.columns)
60
+ features = [col for col in df_cleaned.columns if col != target]
 
 
61
 
62
+ if not features:
63
+ st.warning("No features available after removing the target variable.")
64
+ else:
65
+ X = df_cleaned[features]
66
+ y = df_cleaned[target]
67
 
68
+ # Train and Evaluate Models
69
+ st.subheader("Model Training and Evaluation")
70
+ st.write("Training models and calculating metrics...")
71
+ model_results = train_all_models(X, y)
72
+
73
+ st.write("### Model Training Results")
74
+ st.dataframe(model_results)
75
+
76
+ # Download option for model results
77
+ st.download_button(
78
+ label="Download Model Results (CSV)",
79
+ data=model_results.to_csv(index=False),
80
+ file_name="model_results.csv",
81
+ mime="text/csv"
82
+ )
83
+ except Exception as e:
84
+ st.error(f"An error occurred: {e}")
85
+ else:
86
+ st.info("Please upload a CSV file to proceed.")