campusx-official / sms-spam-classifier

End to end code for the email spam classifier project
97 stars 197 forks source link

"NotFittedError: #7

Open himanshujosfi opened 1 year ago

himanshujosfi commented 1 year ago

Can you plz help how to sole this problem "NotFittedError: This MultinomialNB instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator." plz respond

keshavSvishwakarma commented 1 year ago

import pickle mnb.fit(X_train,y_train) y_pred2 = mnb.predict(X_test) pickle.dump(tfidf,open('vectorizer.pkl','wb')) pickle.dump(mnb,open('model.pkl','wb'))

"Use this with dum and create its work"

Anushka05877 commented 9 months ago

File "/Users/anushka/anaconda3/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script exec(code, module.dict) File "/Users/anushka/PycharmProjects/mail/main.py", line 49, in result = model.predict(vector_input)[0] ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/anushka/anaconda3/lib/python3.11/site-packages/sklearn/naive_bayes.py", line 104, in predict check_is_fitted(self) File "/Users/anushka/anaconda3/lib/python3.11/site-packages/sklearn/utils/validation.py", line 1390, in check_is_fitted raise NotFittedError(msg % {"name": type(estimator).name}) sklearn.exceptions.NotFittedError: This MultinomialNB instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator. can anyone help me in solving this error

AbdullahKhanKakar commented 5 months ago

# importing vectorizer from sklearn.feature_extraction.text import TfidfVectorizer tf_idf = TfidfVectorizer(max_features=3500)

# create vectors of transformed text feature and then stored it in x variable x = tf_idf.fit_transform(df["transformed_text"]).toarray() y = df["target"].values

# now try to train MultinomialNB(), it will work from sklearn.naive_bayes import MultinomialNB model = MultinomialNB() model.fit(x_train, y_train) y_pred = model.predict(x_test) print(accuracy_score(y_test, y_pred)) print(precision_score(y_test, y_pred))

Hope you understand

BE20F06F054 commented 4 months ago

how to resolve not fitted error for the Spam classifier Project?? Anyone can please help me

himanshujosfi commented 4 months ago

Exact what error u are facing Please share

On Mon, 15 Apr 2024 at 6:13 PM, Priti Samse(Government college of Enginnering,Aurangabad) @.***> wrote:

how to resolve not fitted error for the Spam classifier Project?? Anyone can please help me

— Reply to this email directly, view it on GitHub https://github.com/campusx-official/sms-spam-classifier/issues/7#issuecomment-2056764279, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVCE43EAVYPPZHC2ZIAEN3DY5PDOZAVCNFSM6AAAAAAZH2RZQSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANJWG43DIMRXHE . You are receiving this because you modified the open/close state.Message ID: @.***>

BE20F06F054 commented 4 months ago

File Load Error for model.pkl C:\Users\DELL\OneDrive\Desktop\numpy project\model.pkl is not UTF-8 encoded

this happen for both the files i.e. vectorizer.pkl and model.pkl

BE20F06F054 commented 4 months ago

NotFittedError: The TF-IDF vectorizer is not fitted Traceback:

File "C:\Users\DELL\PycharmProjects\SMS Spam Project\venv\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 542, in _run_script exec(code, module.dict) File "C:\Users\DELL\PycharmProjects\SMS Spam Project\app.py", line 42, in vector_input = tfidf.transform([transformed_sms]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\DELL\PycharmProjects\SMS Spam Project\venv\Lib\site-packages\sklearn\feature_extraction\text.py", line 2161, in transform check_is_fitted(self, msg="The TF-IDF vectorizer is not fitted") File "C:\Users\DELL\PycharmProjects\SMS Spam Project\venv\Lib\site-packages\sklearn\utils\validation.py", line 1461, in check_is_fitted raise NotFittedError(msg % {"name": type(estimator).name})

and this is the error after streamlit running

ParthYuki commented 1 month ago

import pandas as pd import joblib from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.svm import SVC from sklearn.naive_bayes import MultinomialNB from sklearn.ensemble import ExtraTreesClassifier, VotingClassifier from sklearn.model_selection import train_test_split

Load the dataset

data = pd.read_csv('spam.csv', encoding='latin-1') data = data[['v1', 'v2']] data.columns = ['label', 'text'] data['label'] = data['label'].map({'ham': 0, 'spam': 1})

Split the data

x_train, x_test, y_train, y_test = train_test_split(data['text'], data['label'], test_size=0.2, random_state=42)

Fit the TF-IDF vectorizer

tfidf_vectorizer = TfidfVectorizer(max_features=3500) x_train_tfidf = tfidf_vectorizer.fit_transform(x_train)

Define the classifiers

svc = SVC(kernel='sigmoid', gamma=1.0, probability=True) mnb = MultinomialNB() etc = ExtraTreesClassifier(n_estimators=50, random_state=2)

Create a voting classifier

voting = VotingClassifier(estimators=[('svm', svc), ('nb', mnb), ('et', etc)], voting='soft') voting.fit(x_train_tfidf, y_train)

Save the trained model and vectorizer

joblib.dump(voting, 'spam_classifier.pkl') joblib.dump(tfidf_vectorizer, 'tfidf_vectorizer.pkl')

I've used joblib instead of pickle and it worked. Hope it works for you.