My model always predict only one results regardless of the input

Below is my model training

from sklearn.model_selection import train_test_split
# To be used for min-max normalization
from sklearn.preprocessing import MinMaxScaler

X =  df_train_filtered.drop(columns=['target'], axis=1)
Y = df_train_filtered['target']
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.30, random_state = 0)

# Fit min-max scaler on training data
norm = MinMaxScaler().fit(X_train)
 
# Transform the training data
X_train_norm = norm.transform(X_train)
 
# Use the same scaler to transform the testing set
X_test_norm = norm.transform(X_test)

from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

knn = KNeighborsClassifier()
knn.fit(X_train_norm, y_train)

# accuracy score, confusion matrix and classification report of knn

knn_acc = accuracy_score(y_test, knn.predict(X_test_norm))

print(f"Training Accuracy of KNN is {accuracy_score(y_train, knn.predict(X_train_norm))}")
print(f"Test Accuracy of KNN is {knn_acc} \n")

print(f"Confusion Matrix :- \n{confusion_matrix(y_test, knn.predict(X_test_norm))}\n")
print(f"Classification Report :- \n {classification_report(y_test, knn.predict(X_test_norm))}")

import pickle

# Load the saved model
knnclassifier_file = 'knn_classifier.pkl'
with open(knnclassifier_file, 'wb') as file:  
    pickle.dump(knn, file)

Below is my implementation of the model

import numpy as np
import pandas as pd
import streamlit as st 
from sklearn.preprocessing import LabelEncoder
import pickle

st.set_page_config(
   page_title="KDP App",
   page_icon="🧊",
   layout="wide",
   initial_sidebar_state="expanded",
)

# Load the trained model
model = pickle.load(open('knn_classifier.pkl', 'rb'))

# Define the column names
cols = ["gravity", "ph", "osmo", "cond", "urea", "calc"]

def main():
    st.title("Kidney Disease Prediction Using Hybrid Model")
    
    html_temp = """
    <div style="background:#025246 ;padding:10px">
    <h2 style="color:white;text-align:center;">KD Prediction App </h2>
    </div>
    """
    st.markdown(html_temp, unsafe_allow_html = True)
    
    # Define input fields
    gravity = st.text_input("gravity", 0)
    ph = st.text_input("ph", 0)
    osmo = st.text_input("osmo")
    cond = st.text_input("cond")
    urea = st.text_input("urea")
    calc = st.text_input("calc")
    
    if st.button("Predict"): 
        # Store user input in a dictionary
        data = {
            'gravity': float(gravity), 
            'ph': float(ph), 
            'osmo': int(osmo),
            'cond': float(cond),
            'urea': int(urea),
            'calc': float(calc)
        }
        
        # Convert data to DataFrame
        df = pd.DataFrame([data], columns=cols)
        
        # Make prediction
        prediction = model.predict(df)
        
        # Display prediction result
        if prediction[0] == 1:
            st.success("The patient has Chronic Kidney Disease.")
        else:
            st.success("The patient does not have Chronic Kidney Disease.")

if __name__ == '__main__':
    main()

I will be very much greaful to get any response or solutions from you guys

Since you scale your data during training, do not forget to apply a scaling to your input data in prediction.

I did not scale the data, I just encoded the categorical columns and perform random imputation

You scale your training data when you create your model.

norm = MinMaxScaler().fit(X_train)

Yeah, thanks for the assistance. It worked!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.