Google Prediction

Create training model

function createTrainingModel() {
  // Create a training model to associate [City, Temperature]
  // with sales conversions.

  var modelName = 'My sales data';
  var trainingInstances = [];

  // To get your project ID, open the Advanced APIs dialog, click the
  // "Google Developers Console" and select the project number from the
  // Overview page.
  var projectId = 'INSERT_PROJECT_ID_HERE';

  trainingInstances.push(createTrainingInstance('NYC', 30, 10));
  trainingInstances.push(createTrainingInstance('NYC', 15, 100));
  trainingInstances.push(createTrainingInstance('NYC', 27, 20));
  trainingInstances.push(createTrainingInstance('SFO', 19, 50));
  trainingInstances.push(createTrainingInstance('SFO', 29, 10));
  trainingInstances.push(createTrainingInstance('SFO', 30, 20));

  var insert = Prediction.newInsert();
  insert.id = modelName;
  insert.trainingInstances = trainingInstances;

  var insertReply = Prediction.Trainedmodels.insert(insert, projectId);
  Logger.log('Trained model with data.');
}

function createTrainingInstance(city, temperature, conversions) {
  var trainingInstances = Prediction.newInsertTrainingInstances();
  trainingInstances.csvInstance = [city, temperature];
  trainingInstances.output = conversions;
  return trainingInstances;
}

Make a prediction

function makePrediction() {
  var modelName = 'My sales data';

  // Change to values for which you want to predict results.
  var city = 'NYC';
  var temperature = '14';

  // To get your project ID, open the Advanced APIs dialog, click the
  // "Google Developers Console" and select the project number from the
  // Overview page.
  var projectId = 'INSERT_PROJECT_ID_HERE';

  var request = Prediction.newInput();
  request.input = Prediction.newInputInput();
  request.input.csvInstance = [city, temperature];
  var predictionResult = Prediction.Trainedmodels.predict(
      request, projectId, modelName);
  Logger.log("Prediction for City = '%s' and Temperature = '%s' is %s.",
             city, temperature, predictionResult.outputValue);
}

Update trained model

function updateTrainedModelData() {
  // Update the training model, to associate [NYC, 30] with 25
  // sales conversions.

  var modelName = 'My sales data';

  // To get your project ID, open the Advanced APIs dialog, click the
  // "Google Developers Console" and select the project number from the
  // Overview page.
  var projectId = 'INSERT_PROJECT_ID_HERE';

  var update = Prediction.newUpdate();
  update.csvInstance = ['NYC', 30];
  update.output = 25;

  var updateResponse = Prediction.Trainedmodels.update(update, projectId,
      modelName);
  Logger.log('Trained model updated with new data.');
}

Send feedback about...

AdWords Scripts
AdWords Scripts
Need help? Visit our support page.