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| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from datetime import timedelta
|
| 8 |
+
from sklearn.model_selection import RandomizedSearchCV, GridSearchCV, train_test_split
|
| 9 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 10 |
+
from sklearn.metrics import r2_score
|
| 11 |
+
from sklearn.preprocessing import LabelEncoder
|
| 12 |
+
from sklearn.preprocessing import StandardScaler
|
| 13 |
+
import streamlit as st
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
st.title("Next Failure Prediction")
|
| 17 |
+
# Loading Dataset
|
| 18 |
+
df1 = pd.read_csv(r'Final_Next_failure_Dataset.csv')
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# replace values in the Manufacturer column with company names
|
| 22 |
+
|
| 23 |
+
replace_dict1 = {1: 'ABC Company', 2: 'DEF Company', 3: 'GHI Company', 4: 'JKL Company', 5: 'XYZ Company'}
|
| 24 |
+
df1['Manufacturer'] = df1['Manufacturer'].replace(replace_dict1)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# replace values in the Last_Maintenance_Type column again
|
| 28 |
+
|
| 29 |
+
replace_dict2 = {1: 'Corrective', 2: 'Preventive'}
|
| 30 |
+
df1['Last_Maintenance_Type'] = df1['Last_Maintenance_Type'].replace(replace_dict2)
|
| 31 |
+
|
| 32 |
+
# replace values in the Prior_Maintenance column again
|
| 33 |
+
|
| 34 |
+
replace_dict3 = {1: 'Irregular', 2: 'Regular'}
|
| 35 |
+
df1['Prior_Maintenance'] = df1['Prior_Maintenance'].replace(replace_dict3)
|
| 36 |
+
|
| 37 |
+
# replace values in the Repair_Type column again
|
| 38 |
+
|
| 39 |
+
replace_dict4 = {1: 'Hardware', 2: 'Software'}
|
| 40 |
+
df1['Repair_Type'] = df1['Repair_Type'].replace(replace_dict4)
|
| 41 |
+
|
| 42 |
+
df = df1.copy()
|
| 43 |
+
|
| 44 |
+
# For Manufacturer
|
| 45 |
+
|
| 46 |
+
le_manu = LabelEncoder()
|
| 47 |
+
df['Manufacturer'] = le_manu.fit_transform(df['Manufacturer'])
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# For Last_Maintenance_Type
|
| 51 |
+
|
| 52 |
+
le_last = LabelEncoder()
|
| 53 |
+
df['Last_Maintenance_Type'] = le_last.fit_transform(df['Last_Maintenance_Type'])
|
| 54 |
+
|
| 55 |
+
# For Prior_Maintenance
|
| 56 |
+
|
| 57 |
+
le_prior = LabelEncoder()
|
| 58 |
+
df['Prior_Maintenance'] = le_prior.fit_transform(df['Prior_Maintenance'])
|
| 59 |
+
|
| 60 |
+
# For Repair_Type
|
| 61 |
+
|
| 62 |
+
le_repair = LabelEncoder()
|
| 63 |
+
df['Repair_Type'] = le_repair.fit_transform(df['Repair_Type'])
|
| 64 |
+
|
| 65 |
+
#Splitting the data train ans test data
|
| 66 |
+
X = df.drop('Time_to_Failure_(hours)', axis = 1)
|
| 67 |
+
y = df['Time_to_Failure_(hours)']
|
| 68 |
+
|
| 69 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state = 0)
|
| 70 |
+
|
| 71 |
+
# Train Random Forest Regression model
|
| 72 |
+
|
| 73 |
+
model = RandomForestRegressor(random_state = 0)
|
| 74 |
+
model.fit(X_train, y_train)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# Make predictions on train data
|
| 78 |
+
|
| 79 |
+
y_pred_train = model.predict(X_train)
|
| 80 |
+
|
| 81 |
+
# DATA from user
|
| 82 |
+
def user_report():
|
| 83 |
+
manufacturer = st.sidebar.selectbox("Manufacturer",
|
| 84 |
+
("JKL Company", "GHI Company","DEF Company","ABC Company","XYZ Company" ))
|
| 85 |
+
if manufacturer=='JKL Company':
|
| 86 |
+
manufacturer=3
|
| 87 |
+
elif manufacturer=="GHI Company":
|
| 88 |
+
manufacturer=2
|
| 89 |
+
elif manufacturer=="DEF Company":
|
| 90 |
+
manufacturer=1
|
| 91 |
+
elif manufacturer=="ABC Company":
|
| 92 |
+
manufacturer =0
|
| 93 |
+
else:
|
| 94 |
+
manufacturer=4
|
| 95 |
+
total_operating_hours = st.sidebar.slider('Total Operating Hours)', 1000,2500, 1500 )
|
| 96 |
+
Usage_Intensity = st.sidebar.slider("Usage_Intensity(hous/day)",1,10,4)
|
| 97 |
+
Last_Maintenance_Type = st.sidebar.selectbox("Last Maintainece Type",("Corrective","Preventive"))
|
| 98 |
+
if Last_Maintenance_Type =='Corrective':
|
| 99 |
+
Last_Maintenance_Type=0
|
| 100 |
+
else:
|
| 101 |
+
Last_Maintenance_Type=1
|
| 102 |
+
Prior_Maintenance = st.sidebar.selectbox("Prior Maintainece",("Regular","Irregular"))
|
| 103 |
+
if Prior_Maintenance =='Regular':
|
| 104 |
+
Prior_Maintenance=1
|
| 105 |
+
else:
|
| 106 |
+
Prior_Maintenance=0
|
| 107 |
+
|
| 108 |
+
Average_Temperature= st.sidebar.slider('Average Temperature', 20,40, 35 )
|
| 109 |
+
humidity = st.sidebar.slider('Humidity', 52,70, 55 )
|
| 110 |
+
Vibration_Level = st.sidebar.slider('Vibration Level', 2,4, 2 )
|
| 111 |
+
Pressure = st.sidebar.slider('Pressure', 28,32, 30 )
|
| 112 |
+
Power_Input_Voltage= st.sidebar.slider('Power Input Voltage (V)',105,120,115)
|
| 113 |
+
Repair_Type = st.sidebar.selectbox("Repair Type",("Hardware","Software"))
|
| 114 |
+
if Repair_Type =='Software':
|
| 115 |
+
Repair_Type=1
|
| 116 |
+
else:
|
| 117 |
+
Repair_Type=0
|
| 118 |
+
load_factor = st.sidebar.number_input('Enter the Load Factor (any number between 0 to 1 )',min_value=0.0,max_value=1.0,step=0.1)
|
| 119 |
+
engine_speed=st.sidebar.slider('Engine Speed',7000,8000,7800)
|
| 120 |
+
Oil_Temperature=st.sidebar.slider('Oil Temperature',170,185,172)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
user_report_data = {
|
| 124 |
+
'Manufacturer': manufacturer,
|
| 125 |
+
'Total_Operating_Hours': total_operating_hours,
|
| 126 |
+
'Usage_Intensity_(hours/day)': Usage_Intensity ,
|
| 127 |
+
'Last_Maintenance_Type': Last_Maintenance_Type,
|
| 128 |
+
"Prior_Maintenance":Prior_Maintenance,
|
| 129 |
+
'Average_Temperature':Average_Temperature,
|
| 130 |
+
'Humidity': humidity,
|
| 131 |
+
'Vibration_Level': Vibration_Level,
|
| 132 |
+
'Pressure': Pressure,
|
| 133 |
+
'Power_Input_Voltage': Power_Input_Voltage,
|
| 134 |
+
'Repair_Type': Repair_Type ,
|
| 135 |
+
'Load_Factor': load_factor,
|
| 136 |
+
'Engine_Speed': engine_speed,
|
| 137 |
+
'Oil_Temperature':Oil_Temperature
|
| 138 |
+
}
|
| 139 |
+
report_data = pd.DataFrame(user_report_data, index=[0])
|
| 140 |
+
|
| 141 |
+
return report_data
|
| 142 |
+
|
| 143 |
+
#Customer Data
|
| 144 |
+
user_data = user_report()
|
| 145 |
+
st.subheader("Component Details")
|
| 146 |
+
st.write(user_data)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# define the prediction function
|
| 150 |
+
def prediction(user_data):
|
| 151 |
+
|
| 152 |
+
predicted_max_number_of_repairs = model.predict(user_data)
|
| 153 |
+
|
| 154 |
+
# return the predicted max number of repairs as output
|
| 155 |
+
return np.round(predicted_max_number_of_repairs[0])
|
| 156 |
+
# Function calling
|
| 157 |
+
y_pred = prediction(user_data)
|
| 158 |
+
st.write("Click here to see the Predictions")
|
| 159 |
+
if st.button("Predict"):
|
| 160 |
+
st.subheader(f"Next Failure is {y_pred} hours ")
|