Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"NotFittedError: #7

Open
himanshujosfi opened this issue Jun 15, 2023 · 8 comments
Open

"NotFittedError: #7

himanshujosfi opened this issue Jun 15, 2023 · 8 comments

Comments

@himanshujosfi
Copy link

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
Copy link

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
Copy link

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
Copy link

# 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
Copy link

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

@himanshujosfi
Copy link
Author

himanshujosfi commented Apr 15, 2024 via email

@BE20F06F054
Copy link

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
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants