I'm trying to consume a web service that I deployed and I get predict() missing 1 required positional argument: 'X' error when I try to consume it with the REST end point. Here is a link for reference about my previous questions in Microsoft:
Here are my train.py and score.py
train.py
df = pd.read_csv('prediction_data01.csv')
df = df[pd.notnull(df['DESCRIPTION'])]
df = df[pd.notnull(df['CUSTOMERCODE'])]
col = ['CUSTOMERCODE', 'DESCRIPTION']
df = df[col]
df.columns = ['CUSTOMERCODE', 'DESCRIPTION']
df['category_id'] = df['DESCRIPTION'].factorize()[0]
tfidf = TfidfVectorizer(sublinear_tf=True, min_df=5, norm='l2', encoding='latin-1', ngram_range=(1, 4), stop_words='english')
features = tfidf.fit_transform(df.DESCRIPTION).toarray()
labels = df.category_id
df = df.applymap(str)
X_train, X_test, y_train, y_test = train_test_split(df['CUSTOMERCODE'], df['DESCRIPTION'], random_state=0)
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(X_train)
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
clf = MultinomialNB().fit(X_train_tfidf, y_train)
os.makedirs("./outputs", exist_ok=True)
joblib.dump(clf, 'prediction-model.pickle')
Score.py
def init():
global model
# AZUREML_MODEL_DIR is an environment variable created during deployment.
# It is the path to the model folder (./azureml-models/$MODEL_NAME/$VERSION)
# For multiple models, it points to the folder containing all deployed models (./azureml-models)
model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), "prediction-model.pickle")
model = joblib.load(model_path)
def run(raw_data):
data = np.array(json.loads(raw_data)['data'])
# make prediction
y_hat = model.predict(data)
# you can return any data type as long as it is JSON-serializable
return y_hat.tolist()
I have tested the model results locally and it's working fine. I predicted the results of the model and with the below code.
clf = MultinomialNB().fit(X_train_tfidf, y_train)
with open("prediction.pickle", "wb") as f:
pickle.dump(MultinomialNB, f)
print(clf.predict(count_vect.transform(["18339"])))
I'm able to predict successfully with the above code and also I'm able to predict with loading the saved model pickle file using the below code.
pickle_in = open("prediction.pickle", "rb")
Multinomial_model = pickle.load(pickle_in)
clf = Multinomial_model().fit(X_train_tfidf, y_train)
print(clf.predict(count_vect.transform(["18339"])))
I get this error -- fit() missing 1 required positional argument: 'y'-- when I do not use parenthesis in the above code fit method. I dont know if it helps.
clf = Multinomial_model.fit(X_train_tfidf, y_train)
Any help is appreciated. Thanks in advance.