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