Is there a parameter problem, or not working with this method?

I wanted to predict the disease type using a function with a given string parameter. The parameter can contain more than one string. But it always gives an error message: KeyError: ‘skin_rash’

This is a code sample which I found on Geeks for geeks: https://www.geeksforgeeks.org/disease-prediction-using-machine-learning/

The datasets for this function can be found on Kaggle: https://www.kaggle.com/datasets/kaushil268/disease-prediction-using-machine-learning/data

Here is my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>symptoms = X.columns.values
Here are the symptoms:
['itching' 'skin_rash' 'nodal_skin_eruptions' 'continuous_sneezing'
'shivering' 'chills' 'joint_pain' 'stomach_pain' 'acidity'
'ulcers_on_tongue' 'muscle_wasting' 'vomiting' 'burning_micturition'
'spotting_ urination' 'fatigue' 'weight_gain' 'anxiety'
'cold_hands_and_feets' 'mood_swings' 'weight_loss' 'restlessness'
'lethargy' 'patches_in_throat' 'irregular_sugar_level' 'cough'
'high_fever' 'sunken_eyes' 'breathlessness' 'sweating' 'dehydration'
'indigestion' 'headache' 'yellowish_skin' 'dark_urine' 'nausea'
'loss_of_appetite' 'pain_behind_the_eyes' 'back_pain' 'constipation'
'abdominal_pain' 'diarrhoea' 'mild_fever' 'yellow_urine'
'yellowing_of_eyes' 'acute_liver_failure' 'fluid_overload'
'swelling_of_stomach' 'swelled_lymph_nodes' 'malaise'
'blurred_and_distorted_vision' 'phlegm' 'throat_irritation'
'redness_of_eyes' 'sinus_pressure' 'runny_nose' 'congestion' 'chest_pain'
'weakness_in_limbs' 'fast_heart_rate' 'pain_during_bowel_movements'
'pain_in_anal_region' 'bloody_stool' 'irritation_in_anus' 'neck_pain'
'dizziness' 'cramps' 'bruising' 'obesity' 'swollen_legs'
'swollen_blood_vessels' 'puffy_face_and_eyes' 'enlarged_thyroid'
'brittle_nails' 'swollen_extremeties' 'excessive_hunger'
'extra_marital_contacts' 'drying_and_tingling_lips' 'slurred_speech'
'knee_pain' 'hip_joint_pain' 'muscle_weakness' 'stiff_neck'
'swelling_joints' 'movement_stiffness' 'spinning_movements'
'loss_of_balance' 'unsteadiness' 'weakness_of_one_body_side'
'loss_of_smell' 'bladder_discomfort' 'foul_smell_of urine'
'continuous_feel_of_urine' 'passage_of_gases' 'internal_itching'
'toxic_look_(typhos)' 'depression' 'irritability' 'muscle_pain'
'altered_sensorium' 'red_spots_over_body' 'belly_pain'
'abnormal_menstruation' 'dischromic _patches' 'watering_from_eyes'
'increased_appetite' 'polyuria' 'family_history' 'mucoid_sputum'
'rusty_sputum' 'lack_of_concentration' 'visual_disturbances'
'receiving_blood_transfusion' 'receiving_unsterile_injections' 'coma'
'stomach_bleeding' 'distention_of_abdomen'
'history_of_alcohol_consumption' 'fluid_overload.1' 'blood_in_sputum'
'prominent_veins_on_calf' 'palpitations' 'painful_walking'
'pus_filled_pimples' 'blackheads' 'scurring' 'skin_peeling'
'silver_like_dusting' 'small_dents_in_nails' 'inflammatory_nails'
'blister' 'red_sore_around_nose' 'yellow_crust_ooze']
# Creating a symptom index dictionary to encode the
# input symptoms into numerical form
symptom_index = {}
for index, value in enumerate(symptoms):
symptom = " ".join([i for i in value.split("_")])
symptom_index[symptom] = index
data_dict = {
"symptom_index":symptom_index,
"predictions_classes":encoder.classes_
}
# Defining the Function
# Input: string containing symptoms separated by commas
# Output: Generated predictions by models
def predictDisease(symptoms):
symptoms = symptoms.split(",")
# creating input data for the models
input_data = [0] * len(data_dict["symptom_index"])
for symptom in symptoms:
index = data_dict["symptom_index"][symptom]
input_data[index] = 1
# reshaping the input data and converting it
# into suitable format for model predictions
input_data = np.array(input_data).reshape(1,-1)
# generating individual outputs
rf_prediction = data_dict["predictions_classes"][final_rf_model.predict(input_data)[0]]
nb_prediction = data_dict["predictions_classes"][final_nb_model.predict(input_data)[0]]
svm_prediction = data_dict["predictions_classes"][final_svm_model.predict(input_data)[0]]
# making final prediction by taking mode of all predictions
final_prediction = mode([rf_prediction, nb_prediction, svm_prediction])[0][0]
predictions = {
"rf_model_prediction":rf_prediction,
"naive_bayes_prediction":nb_prediction,
"svm_model_prediction":svm_prediction,
"final_prediction":final_prediction
}
return predictions
# Testing the function
print(predictDisease("itching,skin_rash,nodal_skin_eruptions"))
</code>
<code>symptoms = X.columns.values Here are the symptoms: ['itching' 'skin_rash' 'nodal_skin_eruptions' 'continuous_sneezing' 'shivering' 'chills' 'joint_pain' 'stomach_pain' 'acidity' 'ulcers_on_tongue' 'muscle_wasting' 'vomiting' 'burning_micturition' 'spotting_ urination' 'fatigue' 'weight_gain' 'anxiety' 'cold_hands_and_feets' 'mood_swings' 'weight_loss' 'restlessness' 'lethargy' 'patches_in_throat' 'irregular_sugar_level' 'cough' 'high_fever' 'sunken_eyes' 'breathlessness' 'sweating' 'dehydration' 'indigestion' 'headache' 'yellowish_skin' 'dark_urine' 'nausea' 'loss_of_appetite' 'pain_behind_the_eyes' 'back_pain' 'constipation' 'abdominal_pain' 'diarrhoea' 'mild_fever' 'yellow_urine' 'yellowing_of_eyes' 'acute_liver_failure' 'fluid_overload' 'swelling_of_stomach' 'swelled_lymph_nodes' 'malaise' 'blurred_and_distorted_vision' 'phlegm' 'throat_irritation' 'redness_of_eyes' 'sinus_pressure' 'runny_nose' 'congestion' 'chest_pain' 'weakness_in_limbs' 'fast_heart_rate' 'pain_during_bowel_movements' 'pain_in_anal_region' 'bloody_stool' 'irritation_in_anus' 'neck_pain' 'dizziness' 'cramps' 'bruising' 'obesity' 'swollen_legs' 'swollen_blood_vessels' 'puffy_face_and_eyes' 'enlarged_thyroid' 'brittle_nails' 'swollen_extremeties' 'excessive_hunger' 'extra_marital_contacts' 'drying_and_tingling_lips' 'slurred_speech' 'knee_pain' 'hip_joint_pain' 'muscle_weakness' 'stiff_neck' 'swelling_joints' 'movement_stiffness' 'spinning_movements' 'loss_of_balance' 'unsteadiness' 'weakness_of_one_body_side' 'loss_of_smell' 'bladder_discomfort' 'foul_smell_of urine' 'continuous_feel_of_urine' 'passage_of_gases' 'internal_itching' 'toxic_look_(typhos)' 'depression' 'irritability' 'muscle_pain' 'altered_sensorium' 'red_spots_over_body' 'belly_pain' 'abnormal_menstruation' 'dischromic _patches' 'watering_from_eyes' 'increased_appetite' 'polyuria' 'family_history' 'mucoid_sputum' 'rusty_sputum' 'lack_of_concentration' 'visual_disturbances' 'receiving_blood_transfusion' 'receiving_unsterile_injections' 'coma' 'stomach_bleeding' 'distention_of_abdomen' 'history_of_alcohol_consumption' 'fluid_overload.1' 'blood_in_sputum' 'prominent_veins_on_calf' 'palpitations' 'painful_walking' 'pus_filled_pimples' 'blackheads' 'scurring' 'skin_peeling' 'silver_like_dusting' 'small_dents_in_nails' 'inflammatory_nails' 'blister' 'red_sore_around_nose' 'yellow_crust_ooze'] # Creating a symptom index dictionary to encode the # input symptoms into numerical form symptom_index = {} for index, value in enumerate(symptoms): symptom = " ".join([i for i in value.split("_")]) symptom_index[symptom] = index data_dict = { "symptom_index":symptom_index, "predictions_classes":encoder.classes_ } # Defining the Function # Input: string containing symptoms separated by commas # Output: Generated predictions by models def predictDisease(symptoms): symptoms = symptoms.split(",") # creating input data for the models input_data = [0] * len(data_dict["symptom_index"]) for symptom in symptoms: index = data_dict["symptom_index"][symptom] input_data[index] = 1 # reshaping the input data and converting it # into suitable format for model predictions input_data = np.array(input_data).reshape(1,-1) # generating individual outputs rf_prediction = data_dict["predictions_classes"][final_rf_model.predict(input_data)[0]] nb_prediction = data_dict["predictions_classes"][final_nb_model.predict(input_data)[0]] svm_prediction = data_dict["predictions_classes"][final_svm_model.predict(input_data)[0]] # making final prediction by taking mode of all predictions final_prediction = mode([rf_prediction, nb_prediction, svm_prediction])[0][0] predictions = { "rf_model_prediction":rf_prediction, "naive_bayes_prediction":nb_prediction, "svm_model_prediction":svm_prediction, "final_prediction":final_prediction } return predictions # Testing the function print(predictDisease("itching,skin_rash,nodal_skin_eruptions")) </code>
symptoms = X.columns.values

Here are the symptoms:
['itching' 'skin_rash' 'nodal_skin_eruptions' 'continuous_sneezing'
 'shivering' 'chills' 'joint_pain' 'stomach_pain' 'acidity'
 'ulcers_on_tongue' 'muscle_wasting' 'vomiting' 'burning_micturition'
 'spotting_ urination' 'fatigue' 'weight_gain' 'anxiety'
 'cold_hands_and_feets' 'mood_swings' 'weight_loss' 'restlessness'
 'lethargy' 'patches_in_throat' 'irregular_sugar_level' 'cough'
 'high_fever' 'sunken_eyes' 'breathlessness' 'sweating' 'dehydration'
 'indigestion' 'headache' 'yellowish_skin' 'dark_urine' 'nausea'
 'loss_of_appetite' 'pain_behind_the_eyes' 'back_pain' 'constipation'
 'abdominal_pain' 'diarrhoea' 'mild_fever' 'yellow_urine'
 'yellowing_of_eyes' 'acute_liver_failure' 'fluid_overload'
 'swelling_of_stomach' 'swelled_lymph_nodes' 'malaise'
 'blurred_and_distorted_vision' 'phlegm' 'throat_irritation'
 'redness_of_eyes' 'sinus_pressure' 'runny_nose' 'congestion' 'chest_pain'
 'weakness_in_limbs' 'fast_heart_rate' 'pain_during_bowel_movements'
 'pain_in_anal_region' 'bloody_stool' 'irritation_in_anus' 'neck_pain'
 'dizziness' 'cramps' 'bruising' 'obesity' 'swollen_legs'
 'swollen_blood_vessels' 'puffy_face_and_eyes' 'enlarged_thyroid'
 'brittle_nails' 'swollen_extremeties' 'excessive_hunger'
 'extra_marital_contacts' 'drying_and_tingling_lips' 'slurred_speech'
 'knee_pain' 'hip_joint_pain' 'muscle_weakness' 'stiff_neck'
 'swelling_joints' 'movement_stiffness' 'spinning_movements'
 'loss_of_balance' 'unsteadiness' 'weakness_of_one_body_side'
 'loss_of_smell' 'bladder_discomfort' 'foul_smell_of urine'
 'continuous_feel_of_urine' 'passage_of_gases' 'internal_itching'
 'toxic_look_(typhos)' 'depression' 'irritability' 'muscle_pain'
 'altered_sensorium' 'red_spots_over_body' 'belly_pain'
 'abnormal_menstruation' 'dischromic _patches' 'watering_from_eyes'
 'increased_appetite' 'polyuria' 'family_history' 'mucoid_sputum'
 'rusty_sputum' 'lack_of_concentration' 'visual_disturbances'
 'receiving_blood_transfusion' 'receiving_unsterile_injections' 'coma'
 'stomach_bleeding' 'distention_of_abdomen'
 'history_of_alcohol_consumption' 'fluid_overload.1' 'blood_in_sputum'
 'prominent_veins_on_calf' 'palpitations' 'painful_walking'
 'pus_filled_pimples' 'blackheads' 'scurring' 'skin_peeling'
 'silver_like_dusting' 'small_dents_in_nails' 'inflammatory_nails'
 'blister' 'red_sore_around_nose' 'yellow_crust_ooze']



 
# Creating a symptom index dictionary to encode the
# input symptoms into numerical form
symptom_index = {}
for index, value in enumerate(symptoms):
    symptom = " ".join([i for i in value.split("_")])
    symptom_index[symptom] = index
 
data_dict = {
    "symptom_index":symptom_index,
    "predictions_classes":encoder.classes_
}
 
# Defining the Function
# Input: string containing symptoms separated by commas
# Output: Generated predictions by models
def predictDisease(symptoms):
    symptoms = symptoms.split(",")
     
    # creating input data for the models
    input_data = [0] * len(data_dict["symptom_index"])
    for symptom in symptoms:
        index = data_dict["symptom_index"][symptom]
        input_data[index] = 1
         
    # reshaping the input data and converting it
    # into suitable format for model predictions
    input_data = np.array(input_data).reshape(1,-1)
     
    # generating individual outputs
    rf_prediction = data_dict["predictions_classes"][final_rf_model.predict(input_data)[0]]
    nb_prediction = data_dict["predictions_classes"][final_nb_model.predict(input_data)[0]]
    svm_prediction = data_dict["predictions_classes"][final_svm_model.predict(input_data)[0]]
     
    # making final prediction by taking mode of all predictions
    final_prediction = mode([rf_prediction, nb_prediction, svm_prediction])[0][0]
    predictions = {
        "rf_model_prediction":rf_prediction,
        "naive_bayes_prediction":nb_prediction,
        "svm_model_prediction":svm_prediction,
        "final_prediction":final_prediction
    }
    return predictions
 
# Testing the function
print(predictDisease("itching,skin_rash,nodal_skin_eruptions"))

But i get this error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>KeyError Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_28380/3227896746.py in <module>
45
46 # Testing the function
---> 47 print(predictDisease("itching,skin_rash,nodal_skin_eruptions"))
~AppDataLocalTemp/ipykernel_28380/3227896746.py in predictDisease(symptoms)
22 input_data = [0] * len(data_dict["symptom_index"])
23 for symptom in symptoms:
---> 24 index = data_dict["symptom_index"][symptom]
25 input_data[index] = 1
26
KeyError: 'skin_rash'
</code>
<code>KeyError Traceback (most recent call last) ~AppDataLocalTemp/ipykernel_28380/3227896746.py in <module> 45 46 # Testing the function ---> 47 print(predictDisease("itching,skin_rash,nodal_skin_eruptions")) ~AppDataLocalTemp/ipykernel_28380/3227896746.py in predictDisease(symptoms) 22 input_data = [0] * len(data_dict["symptom_index"]) 23 for symptom in symptoms: ---> 24 index = data_dict["symptom_index"][symptom] 25 input_data[index] = 1 26 KeyError: 'skin_rash' </code>
KeyError                                  Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_28380/3227896746.py in <module>
     45 
     46 # Testing the function
---> 47 print(predictDisease("itching,skin_rash,nodal_skin_eruptions"))

~AppDataLocalTemp/ipykernel_28380/3227896746.py in predictDisease(symptoms)
     22     input_data = [0] * len(data_dict["symptom_index"])
     23     for symptom in symptoms:
---> 24         index = data_dict["symptom_index"][symptom]
     25         input_data[index] = 1
     26 

KeyError: 'skin_rash'

I tried to give the parameter with different formats(set, array, list), but it does not work and always gives the KeyError: ‘skin_rash’

How can I resolve this problem?
If anyone could help me, i really would appreciate it! thanks

New contributor

crm revesz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật