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

Senthil Murugan RAMACHANDRAN 21 Reputation points
2021-02-24T12:28:53.667+00:00

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
Azure Machine Learning
An Azure machine learning service for building and deploying models.
2,728 questions
{count} votes

3 answers

Sort by: Most helpful
  1. GiftA-MSFT 11,161 Reputation points
    2021-02-25T18:46:23.327+00:00

    Please review my response on this thread, thanks!


  2. GiftA-MSFT 11,161 Reputation points
    2021-03-02T02:42:35.233+00:00

    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!

    0 comments No comments

  3. Senthil Murugan RAMACHANDRAN 21 Reputation points
    2021-03-05T05:47:40.24+00:00

    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

    0 comments No comments