AttributeError: transform not found in Countvectorizer , using sklearn
Попытка загрузить мою модель машинного обучения для обнаружения спамных текстов в API с помощью фреймворка Django.
Моя ошибка при отправке запроса с текстом: "Ahmed,,,, Awwad.... Тест"
File "path/to/API.py", line 39, in predict_text
vector = selector.transform(clean_text)
File "/usr/lib/python3/dist-packages/scipy/sparse/base.py", line 687, in __getattr__
raise AttributeError(attr + " not found")
AttributeError: transform not found
[30/Jun/2022 00:11:23] "POST /api/spamurai HTTP/1.1" 500 103644
Это моя модель:
#convert a collection of text to amatrix of tokens (Bag of words)
from sklearn.feature_extraction.text import CountVectorizer
from joblib import dump
import pickle
#process_text previously defined function to preprocess text in model
msg_vector= CountVectorizer(analyzer=process_text)
message_bow = msg_vector.fit_transform(df['text'])
pickle.dump(message_bow, open("vector_text.pkl", "wb"))
dump(MultinomialNB,'spam_model_text.joblib')
Я использовал joblib в обоих дампах, но я получил много ошибок с countervectorizer и joblib, поэтому я пробую pickle и joblib
.
В свой файл API.py я загружаю текстовый вектор и файл MultinomialNB следующим образом:
from joblib import load
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer
MultinomialNB_file = os.path.join(BASE_DIR, 'tutorials/data', 'spam_model_text.joblib')
CountVectorize_file = os.path.join(BASE_DIR, 'tutorials/data','vector_text.pkl')
txt_model = load(MultinomialNB_file)
selector = pickle.load(open(CountVectorize_file, "rb"))
# # pre processing email text to be used in ML Model without affecting accuracy of model
def process_text(text):
#processing_code
return clean_text
# # function uses trainig utilities used in TEXT ML Model
def predict_text(text):
# call pre processing email text in here
# text is my input
# text: Ahmed,,, Awwad.. Test
clean_text = process_text(text)
# clean_TXT: ['Ahmed', 'Awwad', 'Test']
#clean_txt is my input after preprocessing
vector = selector.transform(clean_text)
result = txt_model.predict(vector)
Мои мысли могут помочь: 'vector_text.pkl', вероятно, не читается как файл дампа countvectorizer