def test_online_model(model_name): # Create local_model object print("Creating model from API .... ") predict_storage = os.path.join(PREDICT_STORAGE, model_name) if not os.path.exists(predict_storage): print("Creating predict directory .... ") os.makedirs(predict_storage) API_predict_storage = os.path.join(predict_storage, "API_result") if not os.path.exists(API_predict_storage): print("Creating predict directory .... ") os.makedirs(API_predict_storage) api = BigML(storage=API_predict_storage) print("Reading testing data .... ") test_source = api.create_source( os.path.join(DATASET_STORAGE, model_name, model_name + "_test.csv")) api.ok(test_source) test_dataset = api.create_dataset(test_source) api.ok(test_dataset) print("Start predicting .... ") if which_model == "DT": batch_prediction = api.create_batch_prediction( 'model/{}'.format(models[model_name]), test_dataset, { "all_fields": True, "probabilities": True }) elif which_model == "DN": batch_prediction = api.create_batch_prediction( 'deepnet/{}'.format(models[model_name]), test_dataset, { "all_fields": True, "probabilities": True }) api.ok(batch_prediction) print(batch_prediction) api.download_batch_prediction(batch_prediction, filename=os.path.join( predict_storage, model_name + "_results")) print("Finish batch prediction !!")
def bigml( train_csv, test_csv, result_csv ): api = BigML(dev_mode=True) # train model start_training = timer() source_train = api.create_source(train_csv) dataset_train = api.create_dataset(source_train) model = api.create_model(dataset_train) end_training = timer() print('Training model.') print('Training took %i Seconds.' % (end_training - start_training) ); # test create_model start_test = timer() source_test = api.create_source(test_csv) dataset_test = api.create_dataset(source_test) batch_prediction = api.create_batch_prediction( model, dataset_test, { "name": "census prediction", "all_fields": True, "header": False, "confidence": False } ) # wait until batch processing is finished while api.get_batch_prediction(batch_prediction)['object']['status']['progress'] != 1: print api.get_batch_prediction(batch_prediction)['object']['status']['progress'] time.sleep(1) end_test = timer() print('Testing took %i Seconds' % (end_test - start_test) ); api.download_batch_prediction(batch_prediction['resource'], filename=result_csv) # cleanup api.delete_source(source_train) api.delete_source(source_test) api.delete_dataset(dataset_train) api.delete_dataset(dataset_test) api.delete_model(model)
from bigml.api import BigML api = BigML() source1 = api.create_source("iris.csv") api.ok(source1) dataset1 = api.create_dataset(source1) api.ok(dataset1) model1 = api.create_model(dataset1) api.ok(model1) batchprediction1 = api.create_batch_prediction(model1, dataset1, \ {'name': u'my_batch_prediction_name'}) api.ok(batchprediction1)
from bigml.api import BigML api = BigML() source1 = api.create_source("iris.csv") api.ok(source1) dataset1 = api.create_dataset(source1) api.ok(dataset1) model1 = api.create_model(dataset1) api.ok(model1) batchprediction1 = api.create_batch_prediction(model1, dataset1, {"name": u"my_batch_prediction_name"}) api.ok(batchprediction1)
from bigml.api import BigML api = BigML() source1 = api.create_source("iris.csv") api.ok(source1) dataset1 = api.create_dataset(source1, \ {'name': u'iris'}) api.ok(dataset1) model1 = api.create_model(dataset1, \ {'name': u'iris'}) api.ok(model1) batchprediction1 = api.create_batch_prediction(model1, dataset1, \ {'name': u'my_batch_prediction_name'}) api.ok(batchprediction1)
from bigml.api import BigML api = BigML() source1 = api.create_source("iris.csv") api.ok(source1) dataset1 = api.create_dataset(source1, \ {'name': u'iris dataset'}) api.ok(dataset1) model1 = api.create_model(dataset1, \ {'name': u"iris dataset's model"}) api.ok(model1) batchprediction1 = api.create_batch_prediction(model1, dataset1, \ {'name': u"Batch Prediction of iris dataset's model with iris dataset", 'output_dataset': True}) api.ok(batchprediction1) dataset2 = api.get_dataset( batchprediction1['object']['output_dataset_resource']) api.ok(dataset2) dataset2 = api.update_dataset(dataset2, \ {'fields': {u'000000': {'name': u'species'}}, 'name': u'my_dataset_from_batch_prediction_name'}) api.ok(dataset2)
from bigml.api import BigML api = BigML() source1 = api.create_source("iris.csv") api.ok(source1) dataset1 = api.create_dataset(source1, \ {'name': u'iris dataset'}) api.ok(dataset1) model1 = api.create_model(dataset1, \ {'name': u"iris dataset's model"}) api.ok(model1) batchprediction1 = api.create_batch_prediction(model1, dataset1, \ {'name': u"Batch Prediction of iris dataset's model with iris dataset", 'output_dataset': True}) api.ok(batchprediction1) dataset2 = api.get_dataset(batchprediction1['object']['output_dataset_resource']) api.ok(dataset2) dataset2 = api.update_dataset(dataset2, \ {'name': u'my_dataset_from_batch_prediction_name', 'tags': [u'species']}) api.ok(dataset2)
### Create data source (from local) source = api.create_source('./data/local_data/iris.csv') api.ok(source) dataset = api.create_dataset(source) api.ok(dataset) model = api.create_model(dataset) api.ok(model) ### To set the target objective_field. # model = api.create_model(dataset, {"objective_field": "species"}) ### Model ecaluation # evaluation = api.create_evaluation(model, test_dataset) # api.ok(evaluation) ### You can call the model from 1. API 2. local model # 1. This is for one input data input_data = {"petal width": 1.75, "petal length": 2.45} prediction = api.create_prediction(model, input_data) print(prediction) api.pprint(prediction) # 2. This is for a bunch of input data test_source = api.create_source("./data/test_iris.csv") api.ok(test_source) test_dataset = api.create_dataset(test_source) api.ok(test_dataset) batch_prediction = api.create_batch_prediction(model, test_dataset, {"all_fields": True}) api.ok(batch_prediction) api.download_batch_anomaly_score(batch_prediction, filename="REQUEST_STORAGE")
source1_file = "iris.csv" args = \ {u'fields': {u'000000': {u'name': u'sepal length', u'optype': u'numeric'}, u'000001': {u'name': u'sepal width', u'optype': u'numeric'}, u'000002': {u'name': u'petal length', u'optype': u'numeric'}, u'000003': {u'name': u'petal width', u'optype': u'numeric'}, u'000004': {u'name': u'species', u'optype': u'categorical', u'term_analysis': {u'enabled': True}}}} source2 = api.create_source(source1_file, args) api.ok(source2) args = \ {u'objective_field': {u'id': u'000004'}} dataset1 = api.create_dataset(source2, args) api.ok(dataset1) args = \ {u'split_candidates': 32} model1 = api.create_model(dataset1, args) api.ok(model1) args = \ {u'fields_map': {u'000001': u'000001', u'000002': u'000002', u'000003': u'000003', u'000004': u'000004'}, u'operating_kind': u'probability'} batchprediction1 = api.create_batch_prediction(model1, dataset1, args) api.ok(batchprediction1)
from bigml.api import BigML api = BigML() source1 = api.create_source("iris.csv") api.ok(source1) dataset1 = api.create_dataset(source1, \ {'name': u'iris'}) api.ok(dataset1) model1 = api.create_model(dataset1, \ {'name': u'iris'}) api.ok(model1) batchprediction1 = api.create_batch_prediction(model1, dataset1, \ {'name': u'iris dataset with iris', 'output_dataset': True}) api.ok(batchprediction1) dataset2 = api.get_dataset(batchprediction1['object']['output_dataset_resource']) api.ok(dataset2) dataset2 = api.update_dataset(dataset2, \ {'name': u'my_dataset_from_batch_prediction_name'}) api.ok(dataset2)
EXECUTION_model_or_enemble = API.create_execution( SCRIPT_model_or_ensemble['resource'], {'inputs': [["ts-id", training_set]]}) API.ok(EXECUTION_model_or_enemble) model_or_ensemble = EXECUTION_model_or_enemble["object"]["execution"]["result"] #Locally store the model or ensemble if model_or_ensemble[:1] == 'e': global local_ensemble local_ensemble = Ensemble(model_or_ensemble) picklEoR = local_ensemble else: global local_model local_model = Model(model_or_ensemble) picklEoR = local_model #batch prediction to check if the model is accurate batch_prediction = API.create_batch_prediction(model_or_ensemble, testing_set, {"all_fields": True}) API.ok(batch_prediction) API.download_batch_prediction(batch_prediction, filename=(filename[:-4] + "-Model-or-Ensemble-Check.csv")) #Store the data the has been created from this python file f = open(pickle_store, 'wb') pickle.dump([feature_names, model_or_ensemble, picklEoR], f) f.close()
u'000001': {u'name': u'sepal width', u'optype': u'numeric'}, u'000002': {u'name': u'petal length', u'optype': u'numeric'}, u'000003': {u'name': u'petal width', u'optype': u'numeric'}, u'000004': {u'name': u'species', u'optype': u'categorical', u'term_analysis': {u'enabled': True}}}} source2 = api.create_source(source1_file, args) api.ok(source2) args = \ {u'objective_field': {u'id': u'000004'}} dataset1 = api.create_dataset(source2, args) api.ok(dataset1) model1 = api.create_model(dataset1) api.ok(model1) args = \ {u'operating_kind': u'probability', u'output_dataset': True} batchprediction1 = api.create_batch_prediction(model1, dataset1, args) api.ok(batchprediction1) dataset2 = api.get_dataset(batchprediction1["object"]["output_dataset_resource"]) api.ok(dataset2) args = \ {u'fields': {u'100000': {u'name': u'species', u'preferred': True}}, u'objective_field': {u'id': u'100000'}} dataset3 = api.update_dataset(dataset2, args) api.ok(dataset3)
from bigml.api import BigML api = BigML() source1 = api.create_source("iris.csv") api.ok(source1) dataset1 = api.create_dataset(source1) api.ok(dataset1) model1 = api.create_model(dataset1) api.ok(model1) batchprediction1 = api.create_batch_prediction(model1, dataset1, \ {'output_dataset': True}) api.ok(batchprediction1) dataset2 = api.get_dataset( batchprediction1['object']['output_dataset_resource']) api.ok(dataset2) dataset2 = api.update_dataset(dataset2, \ {'fields': {u'000000': {'name': u'species'}}, 'name': u'my_dataset_from_batch_prediction_name'}) api.ok(dataset2)
from bigml.api import BigML api = BigML() source1 = api.create_source("iris.csv") api.ok(source1) dataset1 = api.create_dataset(source1) api.ok(dataset1) model1 = api.create_model(dataset1) api.ok(model1) batchprediction1 = api.create_batch_prediction(model1, dataset1, \ {'output_dataset': True}) api.ok(batchprediction1) dataset2 = api.get_dataset(batchprediction1['object']['output_dataset_resource']) api.ok(dataset2) dataset2 = api.update_dataset(dataset2, \ {'fields': {u'000000': {'name': u'species'}}, 'name': u'my_dataset_from_batch_prediction_name'}) api.ok(dataset2)