Step 4: Send prediction requests for the Random Forest® model

Let's go back and use the test data set (AmesHousingTrainingTest.csv) to predict on the Random Forests model directly from Python.

Use the following code to construct the appropriate headers for JSON objects, apply the API key, and convert our previously created test data to the expected data type. Model scoring is directly from Minitab Model Ops. Then, the prediction results are used to calculate R2 and MAD values.

#MSS Prediction
import json
import requests
from sklearn.metrics import r2_score
#Format the request in Python

apiKey="abcdef..." #(use your own apikey)
scoreHeader = {'ApiKey': apiKey,
              'Content-Type': 'application/json'}
test=xTest.to_dict(orient="list")
scoringURL = "https://modelops.minitab.com/api/score"
json.dumps(test)

#obtain scores
post_scores=requests.post(scoringURL,headers=scoreHeader,data=json.dumps(test))

dataFromModelOps = json.loads(post_scores.content.decode("utf-8"))

predictionsFromModelOps = dataFromModelOps['predictions'] #raw scored values
R2Value_MSS=r2_score(yTest,predictionsFromModelOps) #calculate sample R-squared from the scored values
MADValue_MSS = mean_absolute_error(yTest,predictionsFromModelOps)

Now that we have the predictions and the R2and MAD values from the Minitab model, let's obtain them from the MLPRegressor model. Because we made the model object already, we can refer to the MLPRegressor documentation to use the correct function to get the scores and performance values. Then, we will compare the predicted values, as well as the R2 and MAD values, to assess model performance.

#Python Prediction
#We already made the model above, all we need to do is run the prediction and R-squared values. 
pyPred=regr.predict(X_test)
R2Value_Python=regr.score(X_test, y_test)

#MAD Calculation, also known as Mean Absolute Error
MADValue_Python = mean_absolute_error(y_test, pyPred)
MADValue_MSS = mean_absolute_error(y_test,predictionsFromModelOps)