Problem sklearn

import numpy as np
import pickle
import streamlit as st
import pandas as pd
import sklearn
from sklearn import svm
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

loading the diabetes dataset to a pandas DataFrame

diabetes_dataset = pd.read_csv(‘diabetes.csv’)

printing the first 5 rows of the dataset

diabetes_dataset.head()

separating the data and labels

X = diabetes_dataset.drop(columns=‘Outcome’, axis=1)
Y = diabetes_dataset[‘Outcome’]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, stratify=Y, random_state=2)

classifier = svm.SVC(kernel=‘linear’)

training the support vector Machine Classifier

classifier.fit(X_train, Y_train)

accuracy score on the test data

X_test_prediction = classifier.predict(X_test)
test_data_accuracy = accuracy_score(X_test_prediction, Y_test)

Saving the trained model

#filename = ‘trained_model.sav’
#pickle.dump(classifier, open(filename, ‘wb’))

Loading the saved model

loaded_model = pickle.load(open(‘trained_model.sav’, ‘rb’))

Creating a function for prediction

def diabetes_prediction(input_data):
# changing the input_data to numpy array
input_data_as_numpy_array = np.asarray(input_data)

# reshape the array as we are predicting for one instance
input_data_reshaped = input_data_as_numpy_array.reshape(1, -1)

prediction = loaded_model.predict(input_data_reshaped)

if prediction[0] == 0:
    return 'The person is not diabetic'
else:
    return 'The person is diabetic'

def main():
# Giving title
st.title(‘Diabetes Prediction App’)
# Getting the input data
Pregnancies = st.text_input(‘No. of Pregnancies’)
Glucose = st.text_input(‘No. of Glucose’)
BloodPressure = st.text_input(‘No. of Blood Pressure’)
SkinThickness = st.text_input(‘No. of Skin Thickness’)
Insulin = st.text_input(‘No. of Insulin’)
BMI = st.text_input(‘No. of BMI’)
DiabetesPedigreeFunction = st.text_input(‘No. of Diabetes Pedigree Function’)
Age = st.text_input(‘No. of Age’)

# Code for prediction
diagnosis = ""
if st.button('Diabetes test result'):
    diagnosis = diabetes_prediction([Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, BMI,
                                     DiabetesPedigreeFunction, Age])

st.success(diagnosis)

if name == ‘main’:
main()

ModuleNotFoundError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you’re on Streamlit Cloud, click on ‘Manage app’ in the lower right of your app).

Traceback:

File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)File "/app/diabetes-prediction/ss.py", line 6, in <module>
    from sklearn import svm

how can i solve this error

hi @mis_salwa do you have a requirements.txt file to specify your packages?

On my device, yes, and the results appeared on the local host, but the problem appeared when using streamlit site

I think the correct way importing svm as follows:-

from sklearn.svc import SVM

the same proplem

[07:49:11] 📦 Processing dependencies...
[07:49:11] 📦 Processed dependencies!
[07:49:13] 🔄 Updated app!
[07:55:01] 🐙 Pulling code changes from Github...
2023-09-04 07:55:02.580 Uncaught app exception
Traceback (most recent call last):
  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
  File "/mount/src/hellow/h.py", line 6, in <module>
    from sklearn.svc import SVM
ModuleNotFoundError: No module named 'sklearn'
[07:55:02] 📦 Processing dependencies...
[07:55:02] 📦 Processed dependencies!
[07:55:04] 🔄 Updated app!

i think i missing somehing
Do I have to install sklearn in the site ?
iam confued

Yes.

check your requirements.txt if its included, if not include it, if yes reboot your streamlit app - it happened to me so maybe this helps

Thank you, I wasn’t know the importance of the .txt requirements which is the cause of the problem

Before that, create an test environment in anaconda with python 3.9 versions and test the app locally. If it’s successful then deploy that application into streamlit cloud. In the time deploying, select python version as 3.9 from Advance Settings options of the cloud app dashboard.! Then no errors will occured.

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