Spaces:
Build error
Build error
| import pandas as pd | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| from datetime import datetime | |
| from datetime import timedelta | |
| from sklearn.model_selection import RandomizedSearchCV, GridSearchCV, train_test_split | |
| from sklearn.ensemble import RandomForestRegressor | |
| from sklearn.metrics import r2_score | |
| from sklearn.preprocessing import LabelEncoder | |
| from sklearn.preprocessing import StandardScaler | |
| import streamlit as st | |
| st.title("Next Failure Prediction") | |
| # Loading Dataset | |
| df1 = pd.read_csv(r'Final_Next_failure_Dataset.csv') | |
| # replace values in the Manufacturer column with company names | |
| replace_dict1 = {1: 'ABC Company', 2: 'DEF Company', 3: 'GHI Company', 4: 'JKL Company', 5: 'XYZ Company'} | |
| df1['Manufacturer'] = df1['Manufacturer'].replace(replace_dict1) | |
| # replace values in the Last_Maintenance_Type column again | |
| replace_dict2 = {1: 'Corrective', 2: 'Preventive'} | |
| df1['Last_Maintenance_Type'] = df1['Last_Maintenance_Type'].replace(replace_dict2) | |
| # replace values in the Prior_Maintenance column again | |
| replace_dict3 = {1: 'Irregular', 2: 'Regular'} | |
| df1['Prior_Maintenance'] = df1['Prior_Maintenance'].replace(replace_dict3) | |
| # replace values in the Repair_Type column again | |
| replace_dict4 = {1: 'Hardware', 2: 'Software'} | |
| df1['Repair_Type'] = df1['Repair_Type'].replace(replace_dict4) | |
| df = df1.copy() | |
| # For Manufacturer | |
| le_manu = LabelEncoder() | |
| df['Manufacturer'] = le_manu.fit_transform(df['Manufacturer']) | |
| # For Last_Maintenance_Type | |
| le_last = LabelEncoder() | |
| df['Last_Maintenance_Type'] = le_last.fit_transform(df['Last_Maintenance_Type']) | |
| # For Prior_Maintenance | |
| le_prior = LabelEncoder() | |
| df['Prior_Maintenance'] = le_prior.fit_transform(df['Prior_Maintenance']) | |
| # For Repair_Type | |
| le_repair = LabelEncoder() | |
| df['Repair_Type'] = le_repair.fit_transform(df['Repair_Type']) | |
| #Splitting the data train ans test data | |
| X = df.drop('Time_to_Failure_(hours)', axis = 1) | |
| y = df['Time_to_Failure_(hours)'] | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state = 0) | |
| # Train Random Forest Regression model | |
| model = RandomForestRegressor(random_state = 0) | |
| model.fit(X_train, y_train) | |
| # Make predictions on train data | |
| y_pred_train = model.predict(X_train) | |
| # DATA from user | |
| def user_report(): | |
| manufacturer = st.sidebar.selectbox("Manufacturer", | |
| ("JKL Company", "GHI Company","DEF Company","ABC Company","XYZ Company" )) | |
| if manufacturer=='JKL Company': | |
| manufacturer=3 | |
| elif manufacturer=="GHI Company": | |
| manufacturer=2 | |
| elif manufacturer=="DEF Company": | |
| manufacturer=1 | |
| elif manufacturer=="ABC Company": | |
| manufacturer =0 | |
| else: | |
| manufacturer=4 | |
| total_operating_hours = st.sidebar.slider('Total Operating Hours)', 1000,2500, 1500 ) | |
| Usage_Intensity = st.sidebar.slider("Usage_Intensity(hous/day)",1,10,4) | |
| Last_Maintenance_Type = st.sidebar.selectbox("Last Maintainece Type",("Corrective","Preventive")) | |
| if Last_Maintenance_Type =='Corrective': | |
| Last_Maintenance_Type=0 | |
| else: | |
| Last_Maintenance_Type=1 | |
| Prior_Maintenance = st.sidebar.selectbox("Prior Maintainece",("Regular","Irregular")) | |
| if Prior_Maintenance =='Regular': | |
| Prior_Maintenance=1 | |
| else: | |
| Prior_Maintenance=0 | |
| Average_Temperature= st.sidebar.slider('Average Temperature', 20,40, 35 ) | |
| humidity = st.sidebar.slider('Humidity', 52,70, 55 ) | |
| Vibration_Level = st.sidebar.slider('Vibration Level', 2,4, 2 ) | |
| Pressure = st.sidebar.slider('Pressure', 28,32, 30 ) | |
| Power_Input_Voltage= st.sidebar.slider('Power Input Voltage (V)',105,120,115) | |
| Repair_Type = st.sidebar.selectbox("Repair Type",("Hardware","Software")) | |
| if Repair_Type =='Software': | |
| Repair_Type=1 | |
| else: | |
| Repair_Type=0 | |
| 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) | |
| engine_speed=st.sidebar.slider('Engine Speed',7000,8000,7800) | |
| Oil_Temperature=st.sidebar.slider('Oil Temperature',170,185,172) | |
| user_report_data = { | |
| 'Manufacturer': manufacturer, | |
| 'Total_Operating_Hours': total_operating_hours, | |
| 'Usage_Intensity_(hours/day)': Usage_Intensity , | |
| 'Last_Maintenance_Type': Last_Maintenance_Type, | |
| "Prior_Maintenance":Prior_Maintenance, | |
| 'Average_Temperature':Average_Temperature, | |
| 'Humidity': humidity, | |
| 'Vibration_Level': Vibration_Level, | |
| 'Pressure': Pressure, | |
| 'Power_Input_Voltage': Power_Input_Voltage, | |
| 'Repair_Type': Repair_Type , | |
| 'Load_Factor': load_factor, | |
| 'Engine_Speed': engine_speed, | |
| 'Oil_Temperature':Oil_Temperature | |
| } | |
| report_data = pd.DataFrame(user_report_data, index=[0]) | |
| return report_data | |
| #Customer Data | |
| user_data = user_report() | |
| st.subheader("Component Details") | |
| st.write(user_data) | |
| # define the prediction function | |
| def prediction(user_data): | |
| predicted_max_number_of_repairs = model.predict(user_data) | |
| # return the predicted max number of repairs as output | |
| return np.round(predicted_max_number_of_repairs[0]) | |
| # Function calling | |
| y_pred = prediction(user_data) | |
| st.write("Click here to see the Predictions") | |
| if st.button("Predict"): | |
| st.subheader(f"Next Failure is {y_pred} hours ") |