shwetashweta05 commited on
Commit
c4a721c
·
verified ·
1 Parent(s): 53f260b

Delete pages

Browse files
pages/Automatic_Machine_Learning_project-1.py DELETED
@@ -1,168 +0,0 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import numpy as np
4
- import matplotlib.pyplot as plt
5
- import seaborn as sns
6
- import optuna
7
- import joblib
8
- from sklearn.model_selection import train_test_split
9
- from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
10
- from sklearn.linear_model import LogisticRegression
11
- from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, roc_curve, auc
12
- from sklearn.preprocessing import LabelEncoder, StandardScaler
13
-
14
- # App Title
15
- st.title("Wine Quality Analysis & Classification using Optuna")
16
-
17
- # Step 1: File Upload
18
- uploaded_file = st.file_uploader("Upload your dataset (CSV format)", type=["csv"])
19
-
20
- if uploaded_file is not None:
21
- # Step 2: Load Data
22
- data = pd.read_csv(uploaded_file, encoding="utf-8")
23
-
24
- # Step 3: Display Dataset
25
- st.write("Dataset Preview")
26
- st.dataframe(data.head())
27
-
28
- # Step 4: Dataset Information
29
- st.write("Dataset Info")
30
- st.write(data.dtypes)
31
-
32
- # Step 5: Summary Statistics
33
- st.write("Summary Statistics")
34
- st.write(data.describe())
35
-
36
- # Step 6: Missing Values Check
37
- st.write("Missing Values")
38
- st.write(data.isnull().sum())
39
-
40
- # Step 7: Duplicate Check
41
- st.write(f"Duplicate Rows: {data.duplicated().sum()}")
42
-
43
- # Step 8: Outlier Detection
44
- st.write("Outliers Detection using IQR")
45
- numerical_columns = data.select_dtypes(include=["float64", "int64"]).columns.tolist()
46
-
47
- outlier_counts = {}
48
- for column in numerical_columns:
49
- Q1 = data[column].quantile(0.25)
50
- Q3 = data[column].quantile(0.75)
51
- IQR = Q3 - Q1
52
- lower_bound = Q1 - 1.5 * IQR
53
- upper_bound = Q3 + 1.5 * IQR
54
- outlier_counts[column] = ((data[column] < lower_bound) | (data[column] > upper_bound)).sum()
55
-
56
- st.write(pd.DataFrame(outlier_counts.items(), columns=["Column", "Outliers Count"]))
57
-
58
- # Step 9: Data Visualization
59
- st.write("Data Distribution")
60
- selected_column = st.selectbox("Select a numerical column", numerical_columns)
61
- fig, ax = plt.subplots()
62
- sns.histplot(data[selected_column], kde=True, ax=ax)
63
- st.pyplot(fig)
64
-
65
- # Step 10: Correlation Heatmap
66
- st.write("Correlation Heatmap")
67
- fig, ax = plt.subplots(figsize=(10, 6))
68
- sns.heatmap(data.corr(), annot=True, cmap="coolwarm", fmt=".2f", ax=ax)
69
- st.pyplot(fig)
70
-
71
- # Step 11: Train-Test Split
72
- st.write("Train-Test Split & Hyperparameter Optimization using Optuna")
73
- target_column = st.selectbox(" Select the target column", data.columns)
74
- feature_columns = [col for col in data.columns if col != target_column]
75
-
76
- X = data[feature_columns]
77
- y = data[target_column]
78
-
79
- # Label encoding for categorical target variables
80
- if y.dtype == 'object':
81
- le = LabelEncoder()
82
- y = le.fit_transform(y)
83
-
84
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
85
-
86
- # Step 12: Hyperparameter Tuning with Optuna
87
- n_trials = st.slider(" Select number of Optuna trials", 10, 100, 20)
88
-
89
- def objective(trial):
90
- n_estimators = trial.suggest_int("n_estimators", 50, 300, step=50)
91
- max_depth = trial.suggest_int("max_depth", 5, 20, step=5)
92
- min_samples_split = trial.suggest_int("min_samples_split", 2, 10)
93
- min_samples_leaf = trial.suggest_int("min_samples_leaf", 1, 5)
94
-
95
- model = RandomForestClassifier(
96
- n_estimators=n_estimators,
97
- max_depth=max_depth,
98
- min_samples_split=min_samples_split,
99
- min_samples_leaf=min_samples_leaf,
100
- random_state=42
101
- )
102
- model.fit(X_train, y_train)
103
- y_pred = model.predict(X_test)
104
- return accuracy_score(y_test, y_pred)
105
-
106
- with st.spinner("Optimizing hyperparameters..."):
107
- study = optuna.create_study(direction="maximize")
108
- study.optimize(objective, n_trials=n_trials)
109
-
110
- best_params = study.best_params
111
- st.write(f"Best Hyperparameters: {best_params}")
112
-
113
- # Step 13: Train the Best Model
114
- best_model = RandomForestClassifier(**best_params, random_state=42)
115
- best_model.fit(X_train, y_train)
116
- y_pred = best_model.predict(X_test)
117
-
118
- # Step 14: Model Evaluation
119
- st.write("Model Evaluation")
120
- st.write(f"Optimized Accuracy: {accuracy_score(y_test, y_pred):.2f}")
121
- st.write(" Classification Report:")
122
- st.text(classification_report(y_test, y_pred))
123
-
124
- # Confusion Matrix
125
- st.write("Confusion Matrix")
126
- cm = confusion_matrix(y_test, y_pred)
127
- fig, ax = plt.subplots()
128
- sns.heatmap(cm, annot=True, cmap="Blues", fmt="d", ax=ax)
129
- st.pyplot(fig)
130
-
131
- # Feature Importance
132
- st.write("Feature Importance")
133
- feature_importance = pd.Series(best_model.feature_importances_, index=feature_columns).sort_values(ascending=False)
134
- fig, ax = plt.subplots()
135
- feature_importance.plot(kind="bar", ax=ax)
136
- st.pyplot(fig)
137
-
138
- # Model Comparison
139
- st.write("Model Comparison")
140
- models = {
141
- "RandomForest": RandomForestClassifier(**best_params, random_state=42),
142
- "GradientBoosting": GradientBoostingClassifier(),
143
- "LogisticRegression": LogisticRegression()
144
- }
145
- model_accuracies = {}
146
- for model_name, model in models.items():
147
- model.fit(X_train, y_train)
148
- y_pred = model.predict(X_test)
149
- model_accuracies[model_name] = accuracy_score(y_test, y_pred)
150
-
151
- st.write(pd.DataFrame(model_accuracies.items(), columns=["Model", "Accuracy"]))
152
-
153
- # Step 15: Save & Download Model
154
- joblib.dump(best_model, "best_wine_model.pkl")
155
- with open("best_wine_model.pkl", "rb") as file:
156
- st.download_button(" Download Best Model", file, file_name="best_wine_model.pkl")
157
-
158
- # Step 16: Make Predictions on User Input
159
- st.write("Make Predictions with the Best Model")
160
- user_input = {}
161
- for feature in feature_columns:
162
- user_input[feature] = st.number_input(f"Enter value for {feature}", float(data[feature].min()), float(data[feature].max()), float(data[feature].mean()))
163
-
164
- user_df = pd.DataFrame([user_input])
165
- if st.button("Predict"):
166
- prediction = best_model.predict(user_df)
167
- predicted_label = le.inverse_transform([prediction[0]]) if 'le' in locals() else prediction[0]
168
- st.success(f" Predicted Wine Quality: {predicted_label}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
pages/Automatic_machine_leaning_project-2.py DELETED
@@ -1,85 +0,0 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import numpy as np
4
- import seaborn as sns
5
- import matplotlib.pyplot as plt
6
- import optuna
7
- from sklearn.model_selection import train_test_split
8
- from sklearn.preprocessing import StandardScaler, OneHotEncoder
9
- from sklearn.tree import DecisionTreeRegressor
10
- from sklearn.neighbors import KNeighborsRegressor
11
- from sklearn.metrics import mean_squared_error, r2_score
12
- import warnings
13
- warnings.filterwarnings("ignore")
14
-
15
- # Title of the Streamlit App
16
- st.title("📊 Possum Dataset Analysis & Model Optimization with Optuna")
17
-
18
- # Upload Dataset
19
- st.sidebar.header("Upload your CSV file")
20
- uploaded_file = st.sidebar.file_uploader("Choose a file", type=["csv"])
21
-
22
- def load_data(file):
23
- return pd.read_csv(file)
24
-
25
- if uploaded_file is not None:
26
- data = load_data(uploaded_file)
27
- st.write("### Dataset Preview")
28
- st.dataframe(data.head())
29
-
30
- # Data Summary
31
- st.write("### Data Summary")
32
- st.write(data.describe())
33
-
34
- # Data Preprocessing
35
- st.write("### Data Preprocessing")
36
- target_column = st.selectbox("Select Target Column", data.columns)
37
- X = data.drop(columns=[target_column])
38
- y = data[target_column]
39
-
40
- # Splitting Data
41
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
42
- st.write(f"Training Samples: {X_train.shape[0]}, Test Samples: {X_test.shape[0]}")
43
-
44
- # Data Visualization
45
- st.write("### Pairplot Visualization")
46
- fig = sns.pairplot(data)
47
- st.pyplot(fig)
48
-
49
- # Model Selection
50
- model_choice = st.sidebar.radio("Choose a Model", ["Decision Tree", "K-Nearest Neighbors"])
51
-
52
- def objective(trial):
53
- if model_choice == "Decision Tree":
54
- max_depth = trial.suggest_int("max_depth", 1, 20)
55
- model = DecisionTreeRegressor(max_depth=max_depth)
56
- else:
57
- n_neighbors = trial.suggest_int("n_neighbors", 1, 20)
58
- model = KNeighborsRegressor(n_neighbors=n_neighbors)
59
-
60
- model.fit(X_train, y_train)
61
- y_pred = model.predict(X_test)
62
- return mean_squared_error(y_test, y_pred)
63
-
64
- if st.sidebar.button("Optimize Model with Optuna"):
65
- study = optuna.create_study(direction="minimize")
66
- study.optimize(objective, n_trials=20)
67
- st.write("### Best Hyperparameters")
68
- st.write(study.best_params)
69
-
70
- # Train Model with Best Parameters
71
- if model_choice == "Decision Tree":
72
- model = DecisionTreeRegressor(max_depth=study.best_params["max_depth"])
73
- else:
74
- model = KNeighborsRegressor(n_neighbors=study.best_params["n_neighbors"])
75
-
76
- model.fit(X_train, y_train)
77
- y_pred = model.predict(X_test)
78
-
79
- # Performance Metrics
80
- st.write("### Model Performance")
81
- st.write(f"Mean Squared Error: {mean_squared_error(y_test, y_pred):.4f}")
82
- st.write(f"R-Squared Score: {r2_score(y_test, y_pred):.4f}")
83
-
84
- else:
85
- st.write("Upload a dataset to get started!")