question

SenthilMuruganRAMACHANDRAN-7389 avatar image
0 Votes"
SenthilMuruganRAMACHANDRAN-7389 asked SenthilMuruganRAMACHANDRAN-7389 answered

predict() missing 1 required positional argument: 'X' while consuming a deployed web service through python in azure

I'm trying to consume a web service that I deployed and I get predict() missing 1 required positional argument: 'X' error. Here is a link for reference about m previous question: error-while-consuming-the-deployed-web-service-thr.html



Here is my train.py file

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')

Here is my score.py file:

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()






azure-machine-learning
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the follow up details. We will merge the two issue and update to you. ^^

Regards,
Yutong

1 Vote 1 ·
GiftA-MSFT avatar image
0 Votes"
GiftA-MSFT answered SenthilMuruganRAMACHANDRAN-7389 commented

Please review my response on this thread, thanks!


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the idea. I tried it multiple time. It is also not working.

0 Votes 0 ·
GiftA-MSFT avatar image
0 Votes"
GiftA-MSFT answered GiftA-MSFT edited

Hi, can you try testing your model locally first to view the results and the expected format for your test data? It seems you performed some transformations when fitting the model, so you need to ensure that you are providing your test data with the expected shape and dimension of the array. Hope this helps!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SenthilMuruganRAMACHANDRAN-7389 avatar image
0 Votes"
SenthilMuruganRAMACHANDRAN-7389 answered

Hi, 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.

clf = Multinomial_model.fit(X_train_tfidf, y_train)

Hope you understand the issue. Thanks for the continuous response. I think the issue is also with the score.py. Any help is appreciated Thanks

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.