Spaces:
Sleeping
Sleeping
Commit ·
e4dcefc
1
Parent(s): 8c9dc5f
final fix
Browse files- CO2 Emissions_Canada.csv +0 -0
- final.ipynb → Lab2_ML.ipynb +0 -0
- __pycache__/app.cpython-313.pyc +0 -0
- app.py +71 -16
- best_model.joblib +2 -2
- linear_regression_final.ipynb +0 -0
- model4_parsimonious.joblib +0 -3
CO2 Emissions_Canada.csv
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|
final.ipynb → Lab2_ML.ipynb
RENAMED
|
The diff for this file is too large to render.
See raw diff
|
|
|
__pycache__/app.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-313.pyc and b/__pycache__/app.cpython-313.pyc differ
|
|
|
app.py
CHANGED
|
@@ -25,6 +25,10 @@ MODEL_FEATURES = NUMERIC_FEATURES + CATEGORICAL_FEATURES
|
|
| 25 |
|
| 26 |
def load_dataset() -> pd.DataFrame:
|
| 27 |
df = pd.read_csv(DATA_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return df[MODEL_FEATURES + [TARGET]].copy()
|
| 29 |
|
| 30 |
|
|
@@ -87,7 +91,8 @@ def build_relationship_plot():
|
|
| 87 |
ax.set_title("Fuel Consumption vs CO2 Emissions")
|
| 88 |
ax.set_xlabel("Fuel Consumption Comb (L/100 km)")
|
| 89 |
ax.set_ylabel("CO2 Emissions (g/km)")
|
| 90 |
-
ax.
|
|
|
|
| 91 |
ax.legend(title="Fuel Type", fontsize=8)
|
| 92 |
fig.tight_layout()
|
| 93 |
return fig
|
|
@@ -101,13 +106,46 @@ def build_vehicle_class_plot():
|
|
| 101 |
.head(10)
|
| 102 |
)
|
| 103 |
|
| 104 |
-
fig, ax = plt.subplots(figsize=(
|
| 105 |
ax.barh(class_df["Vehicle Class"], class_df[TARGET], color="#2a9d8f")
|
| 106 |
ax.invert_yaxis()
|
| 107 |
ax.set_title("Top 10 Vehicle Classes by Average CO2 Emissions")
|
| 108 |
ax.set_xlabel("Average CO2 Emissions (g/km)")
|
| 109 |
ax.set_ylabel("Vehicle Class")
|
| 110 |
-
ax.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
fig.tight_layout()
|
| 112 |
return fig
|
| 113 |
|
|
@@ -144,21 +182,20 @@ def predict_co2_emission(
|
|
| 144 |
|
| 145 |
DESCRIPTION = f"""
|
| 146 |
# CO2 Emissions Predictor
|
| 147 |
-
This
|
| 148 |
|
| 149 |
-
|
| 150 |
-
**CO2 Emissions (g/km)** from:
|
| 151 |
- Engine Size(L)
|
| 152 |
- Cylinders
|
| 153 |
- Fuel Consumption Comb (L/100 km)
|
| 154 |
- Vehicle Class
|
| 155 |
- Fuel Type
|
| 156 |
|
| 157 |
-
|
| 158 |
- Test RMSE: {METRICS["Test RMSE"]}
|
| 159 |
- Test R2: {METRICS["Test R2"]}
|
| 160 |
|
| 161 |
-
The
|
| 162 |
"""
|
| 163 |
|
| 164 |
|
|
@@ -198,9 +235,9 @@ with gr.Blocks(title="CO2 Emissions Predictor") as demo:
|
|
| 198 |
label="Fuel Type",
|
| 199 |
)
|
| 200 |
|
| 201 |
-
predict_button = gr.Button("Predict
|
| 202 |
-
prediction = gr.Number(label="Predicted CO2
|
| 203 |
-
summary = gr.Textbox(label="
|
| 204 |
|
| 205 |
predict_button.click(
|
| 206 |
fn=predict_co2_emission,
|
|
@@ -214,15 +251,33 @@ with gr.Blocks(title="CO2 Emissions Predictor") as demo:
|
|
| 214 |
outputs=[prediction, summary],
|
| 215 |
)
|
| 216 |
|
| 217 |
-
gr.Markdown("##
|
| 218 |
gr.Markdown(
|
| 219 |
-
"
|
| 220 |
-
"
|
|
|
|
|
|
|
| 221 |
)
|
| 222 |
|
| 223 |
with gr.Row():
|
| 224 |
-
gr.Plot(
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
gr.Examples(
|
| 228 |
examples=[
|
|
|
|
| 25 |
|
| 26 |
def load_dataset() -> pd.DataFrame:
|
| 27 |
df = pd.read_csv(DATA_PATH)
|
| 28 |
+
df["Make"] = df["Make"].apply(lambda v: v.upper())
|
| 29 |
+
df["Vehicle Class"] = df["Vehicle Class"].apply(
|
| 30 |
+
lambda v: v.upper().replace(": ", " - ")
|
| 31 |
+
)
|
| 32 |
return df[MODEL_FEATURES + [TARGET]].copy()
|
| 33 |
|
| 34 |
|
|
|
|
| 91 |
ax.set_title("Fuel Consumption vs CO2 Emissions")
|
| 92 |
ax.set_xlabel("Fuel Consumption Comb (L/100 km)")
|
| 93 |
ax.set_ylabel("CO2 Emissions (g/km)")
|
| 94 |
+
ax.set_facecolor("#fafafa")
|
| 95 |
+
ax.grid(alpha=0.2, linestyle="--", linewidth=0.6)
|
| 96 |
ax.legend(title="Fuel Type", fontsize=8)
|
| 97 |
fig.tight_layout()
|
| 98 |
return fig
|
|
|
|
| 106 |
.head(10)
|
| 107 |
)
|
| 108 |
|
| 109 |
+
fig, ax = plt.subplots(figsize=(8, 5))
|
| 110 |
ax.barh(class_df["Vehicle Class"], class_df[TARGET], color="#2a9d8f")
|
| 111 |
ax.invert_yaxis()
|
| 112 |
ax.set_title("Top 10 Vehicle Classes by Average CO2 Emissions")
|
| 113 |
ax.set_xlabel("Average CO2 Emissions (g/km)")
|
| 114 |
ax.set_ylabel("Vehicle Class")
|
| 115 |
+
ax.set_facecolor("#fafafa")
|
| 116 |
+
ax.grid(axis="x", alpha=0.2, linestyle="--", linewidth=0.6)
|
| 117 |
+
fig.tight_layout()
|
| 118 |
+
return fig
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def build_emissions_distribution_plot():
|
| 122 |
+
fig, ax = plt.subplots(figsize=(8, 5))
|
| 123 |
+
ax.hist(DATAFRAME[TARGET].dropna(), bins=30, color="#f4a261", edgecolor="white")
|
| 124 |
+
ax.set_title("Distribution of CO2 Emissions")
|
| 125 |
+
ax.set_xlabel("CO2 Emissions (g/km)")
|
| 126 |
+
ax.set_ylabel("Frequency")
|
| 127 |
+
ax.set_facecolor("#fafafa")
|
| 128 |
+
ax.grid(axis="y", alpha=0.2, linestyle="--", linewidth=0.6)
|
| 129 |
+
fig.tight_layout()
|
| 130 |
+
return fig
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def build_engine_size_plot():
|
| 134 |
+
plot_df = DATAFRAME[["Engine Size(L)", TARGET]].dropna()
|
| 135 |
+
|
| 136 |
+
fig, ax = plt.subplots(figsize=(8, 5))
|
| 137 |
+
ax.scatter(
|
| 138 |
+
plot_df["Engine Size(L)"],
|
| 139 |
+
plot_df[TARGET],
|
| 140 |
+
alpha=0.6,
|
| 141 |
+
color="#457b9d",
|
| 142 |
+
s=30,
|
| 143 |
+
)
|
| 144 |
+
ax.set_title("Engine Size vs CO2 Emissions")
|
| 145 |
+
ax.set_xlabel("Engine Size(L)")
|
| 146 |
+
ax.set_ylabel("CO2 Emissions(g/km)")
|
| 147 |
+
ax.set_facecolor("#fafafa")
|
| 148 |
+
ax.grid(alpha=0.2, linestyle="--", linewidth=0.6)
|
| 149 |
fig.tight_layout()
|
| 150 |
return fig
|
| 151 |
|
|
|
|
| 182 |
|
| 183 |
DESCRIPTION = f"""
|
| 184 |
# CO2 Emissions Predictor
|
| 185 |
+
This app predicts **CO2 emissions** for a vehicle.
|
| 186 |
|
| 187 |
+
It uses the following inputs:
|
|
|
|
| 188 |
- Engine Size(L)
|
| 189 |
- Cylinders
|
| 190 |
- Fuel Consumption Comb (L/100 km)
|
| 191 |
- Vehicle Class
|
| 192 |
- Fuel Type
|
| 193 |
|
| 194 |
+
Model performance on test data:
|
| 195 |
- Test RMSE: {METRICS["Test RMSE"]}
|
| 196 |
- Test R2: {METRICS["Test R2"]}
|
| 197 |
|
| 198 |
+
The charts below help show simple patterns in the dataset.
|
| 199 |
"""
|
| 200 |
|
| 201 |
|
|
|
|
| 235 |
label="Fuel Type",
|
| 236 |
)
|
| 237 |
|
| 238 |
+
predict_button = gr.Button("Predict")
|
| 239 |
+
prediction = gr.Number(label="Predicted CO2 (g/km)")
|
| 240 |
+
summary = gr.Textbox(label="Result Summary", lines=7)
|
| 241 |
|
| 242 |
predict_button.click(
|
| 243 |
fn=predict_co2_emission,
|
|
|
|
| 251 |
outputs=[prediction, summary],
|
| 252 |
)
|
| 253 |
|
| 254 |
+
gr.Markdown("## Charts")
|
| 255 |
gr.Markdown(
|
| 256 |
+
"- Distribution of CO2 emissions across the dataset.\n"
|
| 257 |
+
"- Relationship between fuel consumption and CO2 emissions.\n"
|
| 258 |
+
"- Relationship between engine size and CO2 emissions.\n"
|
| 259 |
+
"- Vehicle classes with the highest average CO2 emissions."
|
| 260 |
)
|
| 261 |
|
| 262 |
with gr.Row():
|
| 263 |
+
gr.Plot(
|
| 264 |
+
value=build_emissions_distribution_plot,
|
| 265 |
+
label="Distribution of CO2 Emissions",
|
| 266 |
+
)
|
| 267 |
+
gr.Plot(
|
| 268 |
+
value=build_relationship_plot,
|
| 269 |
+
label="Fuel Consumption vs CO2 Emissions",
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
with gr.Row():
|
| 273 |
+
gr.Plot(
|
| 274 |
+
value=build_engine_size_plot,
|
| 275 |
+
label="Engine Size vs CO2 Emissions",
|
| 276 |
+
)
|
| 277 |
+
gr.Plot(
|
| 278 |
+
value=build_vehicle_class_plot,
|
| 279 |
+
label="Top Vehicle Classes by CO2 Emissions",
|
| 280 |
+
)
|
| 281 |
|
| 282 |
gr.Examples(
|
| 283 |
examples=[
|
best_model.joblib
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5f9c488e0a4cefc6abcd77439f154de629be98a1db2bf67b1b9778de7d895f8b
|
| 3 |
+
size 69989
|
linear_regression_final.ipynb
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model4_parsimonious.joblib
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:12211b8d1aaf92f4520704370b799c5b6b10ef6c841a8d56b061795829d08731
|
| 3 |
-
size 15105
|
|
|
|
|
|
|
|
|
|
|
|