Sowmith22 commited on
Commit
e93c7f5
Β·
verified Β·
1 Parent(s): cb96223

Delete final.py

Browse files
Files changed (1) hide show
  1. final.py +0 -224
final.py DELETED
@@ -1,224 +0,0 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import seaborn as sns
4
- import matplotlib.pyplot as plt
5
- import numpy as np
6
- import sklearn
7
- import warnings
8
- from sklearn.linear_model import LogisticRegression
9
- from sklearn.model_selection import train_test_split
10
- from sklearn.metrics import accuracy_score
11
-
12
- warnings.filterwarnings("ignore")
13
-
14
- # Load the dataset
15
- df = pd.read_csv(r"data.csv")
16
- df.fillna(df.mean(), inplace=True)
17
-
18
- # Create Tabs
19
- tab1, tab2, tab3 = st.tabs(["πŸ“˜ Project Overview", "πŸ“Š EDA", "πŸ” Fault Prediction"])
20
-
21
- # ----------------------------- TAB 1 ---------------------------------
22
- with tab1:
23
- st.header("πŸš— Brake System Fault Detection")
24
- st.markdown("### 🧩 Business Problem")
25
-
26
- st.markdown("""
27
- In the automotive industry, ensuring the safety and reliability of braking systems is **mission-critical**. Traditional brake inspections are typically **manual and reactive**, often identifying problems **only after they occur** or during scheduled maintenance.
28
-
29
- However, undetected faults in braking systems can lead to:
30
- - **Brake failure during operation**
31
- - **Reduced vehicle control**
32
- - **Increased risk of accidents**
33
- - **Expensive emergency repairs**
34
-
35
- Manufacturers and fleet managers need a **real-time fault detection system** using **sensor data** to:
36
- - Monitor brake system health continuously
37
- - **Predict faults proactively**
38
- - **Minimize vehicle downtime**
39
- - Enhance **safety, reliability, and cost-efficiency**
40
- """)
41
-
42
-
43
- feature_desc = {
44
- 'Brake_Pressure': "Pressure applied to the brake pedal.",
45
- 'Pad_Wear_Level': "Indicates the wear level of brake pads.",
46
- 'ABS_Status': "1 if Anti-lock Braking System is active, else 0.",
47
- 'Wheel_Speed_FL': "Speed of the front-left wheel.",
48
- 'Wheel_Speed_FR': "Speed of the front-right wheel.",
49
- 'Wheel_Speed_RL': "Speed of the rear-left wheel.",
50
- 'Wheel_Speed_RR': "Speed of the rear-right wheel.",
51
- 'Fluid_Temperature': "Temperature of the brake fluid.",
52
- 'Pedal_Position': "How far the brake pedal is pressed."
53
- }
54
-
55
- selected = st.selectbox("Select a feature to understand:", list(feature_desc.keys()))
56
- st.info(f"πŸ“˜ **{selected}**: {feature_desc[selected]}")
57
-
58
- # 🎯 Goal
59
- st.markdown("### 🎯 Goal")
60
- st.markdown("""
61
- Build a data-driven model that detects braking system faults using sensor data such as brake pressure, wheel speeds, fluid temperature, and pedal position.
62
- """)
63
-
64
- # πŸ’Ό Business Objective
65
- st.markdown("### πŸ“Œ Business Objective")
66
- st.markdown("""
67
- - Detect faults early to reduce vehicle failure risks.
68
- - Analyze sensor behavior during fault vs non-fault conditions.
69
- - Support preventive maintenance using historical data patterns.
70
- """)
71
-
72
- st.markdown("### πŸ“Š Data Understanding")
73
- st.markdown("""
74
- The dataset contains **real-time sensor readings** collected from a vehicle's braking system to detect faults.
75
-
76
- #### πŸ”’ Numerical Features:
77
- - **Brake_Pressure**
78
- - **Pad_Wear_Level**
79
- - **Wheel_Speed_FL**, **Wheel_Speed_FR**, **Wheel_Speed_RL**, **Wheel_Speed_RR**
80
- - **Fluid_Temperature**
81
- - **Pedal_Position**
82
-
83
- #### πŸ”  Categorical Feature:
84
- - **ABS_Status**: `1` = Active, `0` = Inactive
85
-
86
- #### 🎯 Target Variable:
87
- - **Fault**: `1` = Fault Detected, `0` = No Fault
88
- """)
89
-
90
- # ----------------------------- TAB 2 ---------------------------------
91
- with tab2:
92
- st.title("πŸ“Š Exploratory Data Analysis")
93
-
94
- st.subheader("πŸ“„ View Dataset Preview")
95
- if st.button("πŸ” Show Dataset Head"):
96
- st.dataframe(df.head())
97
-
98
- st.subheader("⚠️ Fault Distribution")
99
- fault_counts = df['Fault'].value_counts()
100
- st.bar_chart(fault_counts)
101
- st.write(df['Fault'].value_counts(normalize=True) * 100)
102
-
103
- st.subheader("πŸ“Š Correlation Heatmap")
104
- corr = df.corr()
105
- fig, ax = plt.subplots(figsize=(10, 8))
106
- sns.heatmap(corr, annot=True, fmt=".2f", cmap="coolwarm", ax=ax)
107
- st.pyplot(fig)
108
-
109
- st.markdown("### πŸ“‰ Feature Distributions by Fault")
110
- features = ['Brake_Pressure', 'Pad_Wear_Level', 'Wheel_Speed_FL', 'Wheel_Speed_FR',
111
- 'Wheel_Speed_RL', 'Wheel_Speed_RR', 'Fluid_Temperature', 'Pedal_Position']
112
-
113
- for feature in features:
114
- st.markdown(f"#### πŸ” {feature}")
115
- fig, ax = plt.subplots()
116
- sns.kdeplot(data=df, x=feature, hue="Fault", fill=True, common_norm=False, alpha=0.4, ax=ax)
117
- st.pyplot(fig)
118
-
119
- st.markdown("### πŸ“¦ Boxplots to Compare Fault vs Normal")
120
- for feature in features:
121
- st.markdown(f"#### πŸ“¦ {feature} vs Fault")
122
- fig, ax = plt.subplots()
123
- sns.boxplot(data=df, x='Fault', y=feature, palette="Set2", ax=ax)
124
- st.pyplot(fig)
125
-
126
- st.markdown("### πŸ“ Scatterplots: Detect Patterns or Anomalies")
127
- st.markdown("These help you check combinations of features with color-coded fault info.")
128
-
129
- fig, ax = plt.subplots()
130
- sns.scatterplot(data=df, x="Brake_Pressure", y="Pad_Wear_Level", hue="Fault", palette="Set1", ax=ax)
131
- ax.set_title("Brake Pressure vs Pad Wear Level")
132
- st.pyplot(fig)
133
-
134
- fig, ax = plt.subplots()
135
- sns.scatterplot(data=df, x="Pedal_Position", y="Fluid_Temperature", hue="Fault", palette="Set2", ax=ax)
136
- ax.set_title("Pedal Position vs Fluid Temperature")
137
- st.pyplot(fig)
138
-
139
- # ----------------------------- TAB 3 ---------------------------------
140
- with tab3:
141
- st.markdown(
142
- """
143
- <style>
144
- .stApp {
145
- background-color: #e3f2fd;
146
- padding: 12px;
147
- }
148
- </style>
149
- """,
150
- unsafe_allow_html=True
151
- )
152
-
153
- st.markdown("## πŸš— Vehicle Brake System Fault Detection")
154
- st.markdown("#### Enter Brake Sensor Values to Predict Any System Fault")
155
-
156
- # Prepare data
157
- X = df.drop("Fault", axis=1)
158
- y = df["Fault"]
159
-
160
- # UI for user input
161
- Brake_Pressure = st.slider("πŸ’¨ Brake Pressure (psi)", 50.0, 500.0, step=0.1)
162
- Pad_Wear_Level = st.slider("πŸ›ž Pad Wear Level (%)", 0.0, 100.0, step=0.1)
163
- ABS_Status = st.slider("πŸ›‘ ABS Status (0 = Off, 1 = On)", 0, 1, step=1)
164
- Wheel_Speed_FL = st.slider("βš™οΈ Wheel Speed FL (km/h)", 0.0, 400.0, step=0.1)
165
- Wheel_Speed_FR = st.slider("βš™οΈ Wheel Speed FR (km/h)", 0.0, 400.0, step=0.1)
166
- Wheel_Speed_RL = st.slider("βš™οΈ Wheel Speed RL (km/h)", 0.0, 300.0, step=0.1)
167
- Wheel_Speed_RR = st.slider("βš™οΈ Wheel Speed RR (km/h)", 0.0, 300.0, step=0.1)
168
- Fluid_Temperature = st.slider("🌑️ Fluid Temperature (°C)", -20.0, 150.0, step=0.1)
169
- Pedal_Position = st.slider("🦢 Pedal Position (%)", 0.0, 100.0, step=0.1)
170
-
171
- # Train model
172
- x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=29)
173
- model = LogisticRegression()
174
- model.fit(x_train, y_train)
175
-
176
- user_input = pd.DataFrame([[Brake_Pressure, Pad_Wear_Level, ABS_Status, Wheel_Speed_FL,
177
- Wheel_Speed_FR, Wheel_Speed_RL, Wheel_Speed_RR,
178
- Fluid_Temperature, Pedal_Position]],
179
- columns=X.columns)
180
-
181
- if st.button("πŸ” Predict Brake Fault"):
182
- y_pred = model.predict(user_input)
183
- prob = model.predict_proba(user_input)[0][1]
184
-
185
- if y_pred[0] == 1:
186
- st.error(f"🚨 Fault Detected in Brake System! (Confidence: {prob:.2%})")
187
- issues = []
188
-
189
- if Brake_Pressure < 60 or Brake_Pressure > 130:
190
- issues.append("πŸ”΄ Abnormal Brake Pressure")
191
-
192
- if Pad_Wear_Level >= 80:
193
- issues.append("🟠 Brake Pads Critically Worn")
194
- elif Pad_Wear_Level >= 60:
195
- issues.append("🟑 Brake Pads Heavily Worn")
196
-
197
- if ABS_Status == 0:
198
- issues.append("πŸ”΅ ABS System Not Active")
199
-
200
- if Wheel_Speed_FL < 0 or Wheel_Speed_FL > 130:
201
- issues.append("πŸ”΄ Front Left Wheel Speed Abnormal")
202
- if Wheel_Speed_FR < 0 or Wheel_Speed_FR > 130:
203
- issues.append("πŸ”΄ Front Right Wheel Speed Abnormal")
204
- if Wheel_Speed_RL < 0 or Wheel_Speed_RL > 130:
205
- issues.append("πŸ”΄ Rear Left Wheel Speed Abnormal")
206
- if Wheel_Speed_RR < 0 or Wheel_Speed_RR > 130:
207
- issues.append("πŸ”΄ Rear Right Wheel Speed Abnormal")
208
-
209
- if Fluid_Temperature < -20 or Fluid_Temperature > 120:
210
- issues.append("πŸ”₯ Abnormal Brake Fluid Temperature")
211
-
212
- if 20 < Pedal_Position < 60:
213
- issues.append("🟑 Moderate Brake Pedal Pressed")
214
- if 60 <= Pedal_Position <= 100:
215
- issues.append("πŸ›‘ Brake Pedal Fully Pressed")
216
- if Pedal_Position <= 20:
217
- issues.append("πŸ” Low Brake Pedal Engagement")
218
-
219
- for issue in issues:
220
- st.markdown(f"- {issue}")
221
-
222
- else:
223
- st.success(f"βœ… No Fault Detected. (Confidence: {1 - prob:.2%})")
224
- st.info("πŸš— Your vehicle's brake system appears healthy.")