Esempio n. 1
0
def get_best_instance_model(instance_models, device_type, key_for_model_name):
    dfs_model = {}
    best_model_score = 0
    for model_name in instance_models:
        instances_of_model = []
        for instance in device_type.instances:
            test_trace = instance.traces[0]
            instance_name = test_trace.metadata[key_for_model_name]
            test = utils.trace_series_to_numpy_array(test_trace.series)
            model_score = instance_models[model_name].score(test)
            instances_of_model.append([model_name, instance_name, model_score])
            if (model_score > best_model_score):
                best_model = instance_models[model_name]
        dfs_model[model_name] = pd.DataFrame(
            data=instances_of_model,
            columns=['Model_Instance', 'Test_Instance', 'Value'])
    model_averages = []
    for key in dfs_model:
        sum = 0
        for row in dfs_model[key].iterrows():
            sum = sum + row[1]['Value']
        model_averages.append([key, sum / len(dfs_model[key].index)])
    print
    avg_model_df = pd.DataFrame(data=model_averages,
                                columns=['Model_Instance', 'Avg Probability'])
    print avg_model_df._sort('Avg Probability', ascending=False)
    bestModel = avg_model_df._sort('Avg Probability', ascending=False)._sort(
        'Avg Probability', ascending=False).head(1)['Model_Instance'].values[0]
    print str(bestModel) + ' is best.'
    return bestModel
Esempio n. 2
0
def get_best_instance_model(instance_models,device_type,key_for_model_name):
    dfs_model = {}
    best_model_score = 0
    for model_name in instance_models:
        instances_of_model = []
        for instance in device_type.instances:
            test_trace = instance.traces[0]
            instance_name = test_trace.metadata[key_for_model_name]
            test = utils.trace_series_to_numpy_array(test_trace.series)
            model_score = instance_models[model_name].score(test)
            instances_of_model.append([model_name,instance_name,model_score])
            if(model_score > best_model_score):
                best_model = instance_models[model_name]
        dfs_model[model_name] = pd.DataFrame(data=instances_of_model,
                columns=['Model_Instance','Test_Instance','Value'])
    model_averages = []
    for key in dfs_model:
        sum=0
        for row in dfs_model[key].iterrows():
            sum = sum+row[1]['Value']
        model_averages.append([key,sum/len(dfs_model[key].index)])
    print
    avg_model_df = pd.DataFrame(data=model_averages,
            columns=['Model_Instance','Avg Probability'])
    print avg_model_df._sort('Avg Probability',ascending=False)
    bestModel = avg_model_df._sort('Avg Probability',
            ascending=False)._sort('Avg Probability',
                    ascending=False).head(1)['Model_Instance'].values[0]
    print str(bestModel) + ' is best.'
    return bestModel
Esempio n. 3
0
def fit_trace_to_HMM(model, trace):
    '''
    Fits the given trace to the model. NaNs are turned into zeroes.
    '''
    trace_values = utils.trace_series_to_numpy_array(trace.series)
    model.fit([trace_values])
    startprob, means, covars, transmat = _sort_learnt_parameters(
        model.startprob_, model.means_, model.covars_, model.transmat_)
    model = hmm.GaussianHMM(startprob.size, 'full', startprob, transmat)
    model.means_ = means
    model.covars_ = covars
    return model
Esempio n. 4
0
def fit_trace_to_HMM(model,trace):
    '''
    Fits the given trace to the model. NaNs are turned into zeroes.
    '''
    trace_values = utils.trace_series_to_numpy_array(trace.series)
    model.fit([trace_values])
    startprob, means, covars, transmat = _sort_learnt_parameters(model.startprob_,
            model.means_, model.covars_ , model.transmat_)
    model=hmm.GaussianHMM(startprob.size, 'full', startprob, transmat)
    model.means_ = means
    model.covars_ = covars
    return model
Esempio n. 5
0
def disaggregate_data(model_tuple, trace):
    data=[]
    power_total=utils.trace_series_to_numpy_array(trace.series)
    [decoded_states, decoded_power]=predict_with_FHMM(model_tuple[0],
            model_tuple[1],model_tuple[2],power_total)
    for i,v in enumerate(decoded_power['air1']):
        date_time=trace.series.index[i]
        value=trace.series[i]
        data.append({'date':date_time.strftime('%Y-%m-%d %H:%M'),
            'dg': float(v),'reading':float(value)})
    json_string = json.dumps(data, ensure_ascii=False,indent=4,
            separators=(',', ': '))
    return json_string