import streamlit as st import numpy as np import pickle from sklearn.linear_model import LinearRegression from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA import joblib import pandas as pd def load_data(): # Load data from CSV files df = pd.read_csv('slump_test.data.csv') df.drop(columns=['SLUMP(cm)'], inplace=True) df.dropna(inplace=True) # df = pd.get_dummies(df, drop_first=True) # numeric_columns = df.select_dtypes(include=['int64', 'float64']).columns # # Creating a MinMaxScaler object # scaler = MinMaxScaler() # # Normalizing numeric features # df[numeric_columns] = scaler.fit_transform(df[numeric_columns]) # scaler = StandardScaler() # df_scaled = pd.DataFrame(scaler.fit_transform(df), columns=df.columns) # # Perform PCA # pca = PCA() # df_pca = pca.fit_transform(df_scaled) # Calculate explained variance # explained_variance = pca.explained_variance_ratio_ # Create a DataFrame with the PCA results pca_columns = [f'PC{i+1}' for i in range(df_scaled.shape[1])] df_pca = pd.DataFrame(df_pca, columns=pca_columns) X_train = pd.read_csv('slump_test.data.csv').values[:,:-1] y_train = pd.read_csv('slump_test.data.csv').values[:, -1] return X_train, y_train def load_model_artifacts(): with open('slump_regressor.pkl', 'rb') as f: regressor = pickle.load(f) with open('scaler.pkl', 'rb') as f: scaler = pickle.load(f) with open('pca.pkl', 'rb') as f: pca = pickle.load(f) return regressor, scaler, pca def save_model_artifacts(regressor, scaler, pca): with open('slump_regressor.pkl', 'wb') as f: pickle.dump(regressor, f, protocol=pickle.HIGHEST_PROTOCOL) with open('scaler.pkl', 'wb') as f: pickle.dump(scaler, f, protocol=pickle.HIGHEST_PROTOCOL) with open('pca.pkl', 'wb') as f: pickle.dump(pca, f, protocol=pickle.HIGHEST_PROTOCOL) def preprocess_data(X): # Load the trained scaler and PCA scaler = joblib.load('scaler.pkl') pca = joblib.load('pca.pkl') # Check if the input has 8 features if X.shape[1] != 8: raise ValueError("Input data should have 8 features.") # Scale the input data using the loaded scaler X_scaled = scaler.transform(X) # Apply PCA using the loaded PCA transformer X_pca = pca.transform(X_scaled) return X_pca def predict_slump(cement, blast_furnace_slag, fly_ash, water, superplasticizer, coarse_aggregate, fine_aggregate, FLOW): # Prepare the input data X = np.array([[cement, blast_furnace_slag, fly_ash, water, superplasticizer, coarse_aggregate, fine_aggregate, FLOW]]) # Preprocess the data X_preprocessed = preprocess_data(X) # Load the trained model regressor = joblib.load('slump_regressor.pkl') # Make the prediction slump_prediction = regressor.predict(X_preprocessed)[0] return slump_prediction def main(): st.set_page_config(page_title="Concrete Slump Strength Prediction") st.title("Concrete Slump Strength Prediction") st.write("Enter the concrete mix parameters to predict the slump.") try: regressor, scaler, pca = load_model_artifacts() except (FileNotFoundError, pickle.UnpicklingError): X_train, y_train = load_data() regressor = LinearRegression() regressor.fit(X_train, y_train) scaler = StandardScaler() scaler.fit(X_train) pca = PCA(n_components=4) pca.fit(scaler.transform(X_train)) save_model_artifacts(regressor, scaler, pca) cement = st.number_input("Cement (kg/m^3)", min_value=0.0, step=1.0) blast_furnace_slag = st.number_input("Blast Furnace Slag (kg/m^3)", min_value=0.0, step=1.0) fly_ash = st.number_input("Fly Ash (kg/m^3)", min_value=0.0, step=1.0) water = st.number_input("Water (kg/m^3)", min_value=0.0, step=1.0) superplasticizer = st.number_input("Superplasticizer (kg/m^3)", min_value=0.0, step=1.0) coarse_aggregate = st.number_input("Coarse Aggregate (kg/m^3)", min_value=0.0, step=1.0) fine_aggregate = st.number_input("Fine Aggregate (kg/m^3)", min_value=0.0, step=1.0) FLOW = st.number_input("FLOW (cm)", min_value=0.0, step=1.0) if st.button("Predict Slump Strength"): slump_prediction = predict_slump(cement, blast_furnace_slag, fly_ash, water, superplasticizer, coarse_aggregate, fine_aggregate, FLOW) st.write(f"Predicted Slump Strength: {slump_prediction:.2f} MPA") if __name__ == '__main__': main()