Raj-d commited on
Commit
0780959
·
verified ·
1 Parent(s): ee03620

Upload 2 files

Browse files
Files changed (2) hide show
  1. MLalgoapp.py +286 -0
  2. requirements.txt +6 -0
MLalgoapp.py ADDED
@@ -0,0 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from sklearn.model_selection import train_test_split
4
+ from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, RandomForestClassifier, GradientBoostingClassifier
5
+ from sklearn.linear_model import LinearRegression, Ridge, Lasso, LogisticRegression
6
+ from sklearn.svm import SVR, SVC
7
+ from sklearn.metrics import mean_squared_error, r2_score, accuracy_score, classification_report
8
+ from sklearn.impute import SimpleImputer
9
+ from sklearn.datasets import load_iris, fetch_california_housing
10
+ import base64
11
+
12
+ # Set page config
13
+ st.set_page_config(
14
+ page_title="Machine Learning App",
15
+ page_icon="https://i.imgur.com/C6lAamP.png",
16
+ layout="wide",
17
+ initial_sidebar_state="expanded",
18
+ )
19
+
20
+ # Sidebar logo
21
+ st.sidebar.image("https://i.imgur.com/kpkwAUT.png", use_column_width=True)
22
+
23
+ #function to load a bg image
24
+ def set_bg_hack_url():
25
+ '''
26
+ A function to unpack an image from url and set as bg.
27
+ Returns
28
+ -------
29
+ The background.
30
+ '''
31
+
32
+ st.markdown(
33
+ f"""
34
+ <style>
35
+ .stApp {{
36
+ background: url("https://c.wallhere.com/photos/91/50/mountain_top_black_dark_nature_monochrome_landscape_mountains-1296041.jpg!d");
37
+ background-size: cover
38
+ }}
39
+ </style>
40
+ """,
41
+ unsafe_allow_html=True
42
+ )
43
+
44
+ # Call the function to set background image
45
+ set_bg_hack_url()
46
+
47
+ # Custom CSS styles with base64-encoded background image
48
+ st.markdown(
49
+ f"""
50
+ <style>
51
+ .sidebar .sidebar-content {{
52
+ background-image: linear-gradient(#D5DBDB, #F2F3F4);
53
+ }}
54
+ .st-cc {{
55
+ color: #566573;
56
+ }}
57
+ .st-cq {{
58
+ color: #566573;
59
+ }}
60
+ .st-c8 {{
61
+ color: #FFCDD2;
62
+ }}
63
+ .st-top-right {{
64
+ position: fixed;
65
+ top: 60px;
66
+ right: 10px;
67
+ font-size: 16px;
68
+ color: #FF0000; /* Red text color */
69
+ background-color: rgba(255, 255, 255, 0.8); /* White background color with some transparency */
70
+ padding: 5px 10px; /* Add padding to the text */
71
+ border-radius: 5px; /* Add border radius for rounded corners */
72
+ font-family: Arial, sans-serif;
73
+ }}
74
+ </style>
75
+ """,
76
+ unsafe_allow_html=True,
77
+ )
78
+
79
+
80
+ # Function to build regression model
81
+ def build_regression_model(df, algorithm, hyperparameters):
82
+ X = df.iloc[:, :-1] # Features
83
+ y = df.iloc[:, -1] # Target
84
+
85
+ # Data splitting
86
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
87
+
88
+ st.markdown('**1.2. Data splits**')
89
+ st.write('Training set')
90
+ st.info(X_train.shape)
91
+ st.write('Test set')
92
+ st.info(X_test.shape)
93
+
94
+ st.markdown('**1.3. Variable details**:')
95
+ st.write('X variable')
96
+ st.info(list(X.columns))
97
+ st.write('Y variable')
98
+ st.info(y.name)
99
+
100
+ model = algorithm(**hyperparameters)
101
+ model.fit(X_train, y_train)
102
+
103
+ # Model performance
104
+ st.subheader('2. Model Performance')
105
+
106
+ st.markdown('**2.1. Training set**')
107
+ y_pred_train = model.predict(X_train)
108
+ st.write('Coefficient of determination ($R^2$):')
109
+ st.info(r2_score(y_train, y_pred_train))
110
+
111
+ st.write('Error (MSE or MAE):')
112
+ st.info(mean_squared_error(y_train, y_pred_train))
113
+
114
+ st.markdown('**2.2. Test set**')
115
+ y_pred_test = model.predict(X_test)
116
+ st.write('Coefficient of determination ($R^2$):')
117
+ st.info(r2_score(y_test, y_pred_test))
118
+
119
+ st.write('Error (MSE or MAE):')
120
+ st.info(mean_squared_error(y_test, y_pred_test))
121
+
122
+ # Model parameters
123
+ st.subheader('3. Model Parameters')
124
+ st.write(model.get_params())
125
+
126
+ return model
127
+
128
+
129
+ # Function to build classification model
130
+ def build_classification_model(df, algorithm, hyperparameters):
131
+ X = df.iloc[:, :-1] # Features
132
+ y = df.iloc[:, -1] # Target
133
+
134
+ # Data splitting
135
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
136
+
137
+ st.markdown('**1.2. Data splits**')
138
+ st.write('Training set')
139
+ st.info(X_train.shape)
140
+ st.write('Test set')
141
+ st.info(X_test.shape)
142
+
143
+ st.markdown('**1.3. Variable details**:')
144
+ st.write('X variable')
145
+ st.info(list(X.columns))
146
+ st.write('Y variable')
147
+ st.info(y.name)
148
+
149
+ model = algorithm(**hyperparameters)
150
+ model.fit(X_train, y_train)
151
+
152
+ # Model performance
153
+ st.subheader('2. Model Performance')
154
+
155
+ st.markdown('**2.1. Training set**')
156
+ y_pred_train = model.predict(X_train)
157
+ st.write('Accuracy Score:')
158
+ st.info(accuracy_score(y_train, y_pred_train))
159
+
160
+ st.markdown('**2.2. Test set**')
161
+ y_pred_test = model.predict(X_test)
162
+ st.write('Accuracy Score:')
163
+ st.info(accuracy_score(y_test, y_pred_test))
164
+
165
+ # Model parameters
166
+ st.subheader('3. Model Parameters')
167
+ st.write(model.get_params())
168
+
169
+ return model
170
+
171
+
172
+ # Disclaimer
173
+ st.markdown(
174
+ '<p style="color: green; font-family: monospace;">Disclaimer: This app works best when viewed in dark mode.<br> Click three dots in the top right corner -> Settings -> Choose app theme dark.</p>',
175
+ unsafe_allow_html=True
176
+ )
177
+
178
+
179
+ # The Machine Learning App
180
+ st.write(
181
+ """
182
+ # Machine Learning App
183
+ Select the problem type, dataset, model, and hyperparameters.
184
+ """
185
+ )
186
+
187
+ # Sidebar - Select problem type
188
+ problem_type = st.sidebar.selectbox("Select Problem Type", ["Regression", "Classification"])
189
+
190
+ # Sidebar - Select dataset or upload CSV file
191
+ if problem_type == "Classification":
192
+ st.sidebar.write("### Select Dataset or Upload CSV File for Classification")
193
+ dataset_option = st.sidebar.selectbox("Select Dataset", ["Iris", "Upload CSV File"])
194
+ elif problem_type == "Regression":
195
+ st.sidebar.write("### Select Dataset or Upload CSV File for Regression")
196
+ dataset_option = st.sidebar.selectbox("Select Dataset", ["California Housing", "Upload CSV File"])
197
+
198
+ # Load dataset
199
+ df = None
200
+ if dataset_option == "Iris":
201
+ iris = load_iris()
202
+ df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
203
+ df["target"] = iris.target
204
+ elif dataset_option == "California Housing":
205
+ housing = fetch_california_housing()
206
+ df = pd.DataFrame(data=housing.data, columns=housing.feature_names)
207
+ df["target"] = housing.target
208
+ elif dataset_option == "Upload CSV File":
209
+ uploaded_file = st.sidebar.file_uploader("Upload your CSV file", type=["csv"])
210
+ if uploaded_file is not None:
211
+ df = pd.read_csv(uploaded_file)
212
+
213
+ # Sidebar - Select model and hyperparameters
214
+ st.sidebar.write("### Select Model and Hyperparameters")
215
+
216
+ if problem_type == "Regression":
217
+ regression_algorithm = st.sidebar.selectbox(
218
+ "Select Regression Algorithm",
219
+ ["Linear Regression", "Ridge Regression", "Lasso Regression", "Random Forest Regression", "Gradient Boosting Regression"]
220
+ )
221
+ if regression_algorithm == "Linear Regression":
222
+ hyperparameters = {}
223
+ algorithm = LinearRegression
224
+ elif regression_algorithm == "Ridge Regression":
225
+ alpha = st.sidebar.slider("Alpha", 0.0, 1.0, 0.5, 0.01)
226
+ hyperparameters = {"alpha": alpha}
227
+ algorithm = Ridge
228
+ elif regression_algorithm == "Lasso Regression":
229
+ alpha = st.sidebar.slider("Alpha", 0.0, 1.0, 0.5, 0.01)
230
+ hyperparameters = {"alpha": alpha}
231
+ algorithm = Lasso
232
+ elif regression_algorithm == "Random Forest Regression":
233
+ n_estimators = st.sidebar.slider("Number of Estimators", 1, 1000, 100)
234
+ max_features = st.sidebar.selectbox("Max Features", ["sqrt", "log2"])
235
+ hyperparameters = {"n_estimators": n_estimators, "max_features": max_features}
236
+ algorithm = RandomForestRegressor
237
+ elif regression_algorithm == "Gradient Boosting Regression":
238
+ n_estimators = st.sidebar.slider("Number of Estimators", 1, 1000, 100)
239
+ learning_rate = st.sidebar.slider("Learning Rate", 0.01, 1.0, 0.1, 0.01)
240
+ hyperparameters = {"n_estimators": n_estimators, "learning_rate": learning_rate}
241
+ algorithm = GradientBoostingRegressor
242
+
243
+ elif problem_type == "Classification":
244
+ classification_algorithm = st.sidebar.selectbox(
245
+ "Select Classification Algorithm",
246
+ ["Logistic Regression", "Support Vector Classifier", "Random Forest Classifier", "Gradient Boosting Classifier"]
247
+ )
248
+ if classification_algorithm == "Logistic Regression":
249
+ hyperparameters = {"max_iter": 1000}
250
+ algorithm = LogisticRegression
251
+ elif classification_algorithm == "Support Vector Classifier":
252
+ kernel = st.sidebar.selectbox("Kernel", ["linear", "poly", "rbf", "sigmoid"])
253
+ hyperparameters = {"kernel": kernel}
254
+ algorithm = SVC
255
+ elif classification_algorithm == "Random Forest Classifier":
256
+ n_estimators = st.sidebar.slider("Number of Estimators", 1, 1000, 100)
257
+ max_features = st.sidebar.selectbox("Max Features", ["sqrt", "log2"])
258
+ hyperparameters = {"n_estimators": n_estimators, "max_features": max_features}
259
+ algorithm = RandomForestClassifier
260
+ elif classification_algorithm == "Gradient Boosting Classifier":
261
+ n_estimators = st.sidebar.slider("Number of Estimators", 1, 1000, 100)
262
+ learning_rate = st.sidebar.slider("Learning Rate", 0.01, 1.0, 0.1, 0.01)
263
+ hyperparameters = {"n_estimators": n_estimators, "learning_rate": learning_rate}
264
+ algorithm = GradientBoostingClassifier
265
+
266
+ # Main panel
267
+ st.write("## Results")
268
+
269
+ if df is not None:
270
+ st.write("### Data Preview")
271
+ st.write("First 20 rows of the dataset:")
272
+ st.write(df.head(20)) # Displaying the first 20 rows of the dataset
273
+
274
+ if problem_type == "Regression":
275
+ model = build_regression_model(df, algorithm, hyperparameters)
276
+ st.write("### Regression Model")
277
+ st.write("Model:", algorithm.__name__)
278
+ elif problem_type == "Classification":
279
+ model = build_classification_model(df, algorithm, hyperparameters)
280
+ st.write("### Classification Model")
281
+ st.write("Model:", algorithm.__name__)
282
+ else:
283
+ st.info("Please upload a CSV file or select an example dataset.")
284
+
285
+ # Text in the top-right corner
286
+ st.markdown('<p class="st-top-right">Created by - RAJDEEP CHAKRAVORTY</p>', unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # Automatically generated by https://github.com/damnever/pigar.
2
+
3
+ pandas==2.1.4
4
+ scikit-learn==1.3.2
5
+ streamlit==1.33.0
6
+ scipy