Snippets
Revised by
Frederick Vallaeys
2642a38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | var MODEL_NAME = 'Query and Conversion Rate';
var PROJECT_ID = '40310033428';
// These are the names of your columns from the training
// data to ignore. Change these to create variations of your
// model for testing
var COLS_TO_IGNORE = [
];
// This is the output column for your training data, or
// what value the model is supposed to predict
var OUTPUT_COLUMN = 'Conv. rate';
function main() {
var sheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1kKLYXMGDNFRJ-sAtW5xAdXoG_dK0pNItXov2FVLOq5g/edit#gid=2144226213").getSheetByName("Initial Training Data");
//createTrainingModel(sheet);
var sheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1kKLYXMGDNFRJ-sAtW5xAdXoG_dK0pNItXov2FVLOq5g/edit#gid=2144226213").getSheetByName("Additional Training Data");
//updateTrainedModelData(sheet);
var trainingStatus = Prediction.Trainedmodels.get(PROJECT_ID, MODEL_NAME).trainingStatus;
Logger.log("Training Status: " + trainingStatus);
var queries = [];
// We are going to test it by querying with training data
var sheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1kKLYXMGDNFRJ-sAtW5xAdXoG_dK0pNItXov2FVLOq5g/edit#gid=2144226213").getSheetByName("Initial Training Data");
var testData = sheet.getDataRange().getValues();
var headers = testData.shift();
for(var r in testData) {
var query = [];
var row = testData[r];
for(var i in headers) {
if(COLS_TO_IGNORE.indexOf(headers[i]) == -1 && headers[i] != OUTPUT_COLUMN) {
query.push(row[i])
}
}
queries.push(query);
}
Logger.log(makePrediction(queries));
}
/***********************************
* This function accepts a sheet full of training
* data and creates a trained model for you to query.
***********************************/
function createTrainingModel(sheet) {
var trainingInstances = [];
// get the spreadsheet values
var trainingData = sheet.getDataRange().getValues();
var headers = trainingData.shift();
for(var r in trainingData) {
var inputs = [];
var row = trainingData[r];
for(var i in headers) {
if(COLS_TO_IGNORE.indexOf(headers[i]) == -1 && headers[i] != OUTPUT_COLUMN) {
inputs.push(row[i])
}
}
var output = row[headers.indexOf(OUTPUT_COLUMN)];
trainingInstances.push(createTrainingInstance(inputs,output));
}
var insert = Prediction.newInsert();
insert.id = MODEL_NAME;
insert.trainingInstances = trainingInstances;
var insertReply = Prediction.Trainedmodels.insert(insert, PROJECT_ID);
Logger.log('Trained model with data.');
}
// Helper function to create the training instance.
function createTrainingInstance(inputs,output) {
var trainingInstances = Prediction.newInsertTrainingInstances();
trainingInstances.csvInstance = inputs;
trainingInstances.output = output;
return trainingInstances;
}
function updateTrainedModelData(sheet) {
var updateData = sheet.getDataRange().getValues();
var headers = updateData.shift();
for(var r in updateData) {
Logger.log("r: " + r);
var inputs = [];
var row = updateData[r];
for(var i in headers) {
if(COLS_TO_IGNORE.indexOf(headers[i]) == -1 && headers[i] != OUTPUT_COLUMN) {
inputs.push(row[i])
}
}
Logger.log("inputs: " + inputs);
var output = row[headers.indexOf(OUTPUT_COLUMN)];
Logger.log("output: " + output);
var update = createUpdateInstance(inputs,output)
var updateResponse = Prediction.Trainedmodels.update(update, PROJECT_ID, MODEL_NAME);
Logger.log('Trained model updated with new data.');
sheet.deleteRow(2);
}
}
// Helper function to create the update instance.
function createUpdateInstance(inputs,output) {
var updateInstance = Prediction.newUpdate();
updateInstance.csvInstance = inputs;
updateInstance.output = output;
return updateInstance;
}
/***************************
* Accepts a 2d array of query data and returns the
* predicted output in an array.
***************************/
function makePrediction(data) {
var retVal = [];
for(var r in data) {
var request = Prediction.newInput();
request.input = Prediction.newInputInput();
request.input.csvInstance = data[r];
var predictionResult = Prediction.Trainedmodels.predict(
request, PROJECT_ID, MODEL_NAME);
Logger.log("Prediction for data: %s is %s",
JSON.stringify(data[r]), predictionResult.outputValue);
retVal.push(predictionResult.outputValue);
}
return retVal;
}
|
You can clone a snippet to your computer for local editing. Learn more.