예제 #1
0
def extended_gale_shapley(hospital_prefs_dict, resident_prefs_dict, capacities):
    """
    Main algorithm of extend version of Gale-Shapley algorithm for resident-hospital problem
    """
    
    check_inputs(hospital_prefs, resident_prefs)
    # building a empty list for every hospital in dictionary.
    matched_dict = {hosp: [] for hosp in hospital_prefs_dict}
    free_residents_list = free_residents(resident_prefs_dict, matched_dict)
    while free_residents_list:
        # match first prefrence items
        resident = free_residents_list[0]
        hospital = resident_prefs_dict[resident][0]
        matched_dict[hospital].append(resident)
        
        if has_bad_member(hospital, matched_dict, capacities):
            worst_resident = hospital_prefs_dict[hospital][worst_resident_index(hospital, hospital_prefs_dict, matched_dict)]
            matched_dict[hospital].remove(worst_resident)
            
        if not has_bad_member(hospital, matched_dict, capacities):
            worst_idx = worst_resident_index(hospital, hospital_prefs_dict, matched_dict)
            successors = hospital_prefs_dict[hospital][worst_idx + 1:]

            if successors:
                for resident in successors:
                    hospital_prefs_dict[hospital].remove(resident)
                    if hospital in resident_prefs_dict[resident]:
                        resident_prefs_dict[resident].remove(hospital)
        free_residents_list = free_residents(resident_prefs_dict, matched_dict)
        
    return matched_dict
예제 #2
0
def predict():
    if request.method == 'POST':
        # Check inputs
        x = check_inputs(request.json['features'])
        y_hat = model.predict(tf.transform(x))

        return jsonify(output={"y_hat": y_hat.tolist()},
                       status=200,
                       message='Model Working')
예제 #3
0
def predict(item: Item):
    print(item.features)
    #print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: ", len(item.features))
    x = check_inputs(item.features)
    x_tf = tf.transform(x)
    print("passou do transform")
    y_hat = model.predict(x_tf)
    print("y_hat", y_hat[0])
    if (y_hat[0] == 0):
        pred = "Assinante"
    else:
        pred = "Cancelou"
    item.label = pred

    return item
def generate_results(strategy='aggregated'):
    results = collections.defaultdict(dict)

    df_out = create_dataframes(strategy)

    for state in STATES:
        print(state)
        for model in MODELS:
            filename = f'result-{state}-{strategy}-{model}.csv'
            print(filename)
            results[strategy][model] = pd.read_csv(path.join(
                RESULTS_FOLDER, state, filename),
                                                   index_col='data')
            for metric in METRICS:
                df_buffer = create_dataframes(strategy)
                for i in results[strategy][model].columns:
                    # Get dayout
                    dayout = df_state[state]['cases'].index[-1]
                    # Get y_true and y_hat
                    y_hat = results[strategy][model][i].dropna()
                    y_true = df_state[state]['cases']
                    y_true = y_true.loc[y_hat.index.unique()].dropna()
                    y_true, y_hat = utils.check_inputs(y_true, y_hat)

                    if y_true.index[-1] == dayout:
                        break

                    m = calculate_metrics(y_true, y_hat, metric)

                    df_buffer['state'] = [state]
                    df_buffer['model'] = [model]
                    df_buffer['metric'] = [metric]
                    df_buffer[i] = [m]

                df_out = df_out.append(df_buffer, ignore_index=True)

    return df_out
예제 #5
0
STRATEGIES = [
    'aggregated',
    'windowed'
]

METRICS = ['MAE', 'RMSE', 'MSLE']


def get_date(state, to_drop=10, strategy='aggregated'):
    if strategy == 'aggregated':
        df_state = utils.download_state(state=state)
        df_state = df_state[df_state['cases'] != 0]
        df_state = df_state.iloc[:-to_drop]
    if strategy == 'windowed':
        df_state = utils.download_state(state=state)
        df_state = df_state[df_state['cases'] != 0]
        df_state = df_state.iloc[:-to_drop]

    return df_state




dayone = df_state.index[0]
    dayout = datetime.strptime(df_state.index[-1], '%d/%m/%Y')  
    days = np.array(utils.count_days(dayone=dayone, dayout=dayout, date_string='%d/%m/%Y'))
    X = days.reshape(-1,1)
    y = utils.get_labels(df_state['cases']).reshape(-1,1)
    X, y = utils.check_inputs(X, y)
예제 #6
0
            for train_idx, _ in tcvs:
                X_train, y_train = X[train_idx], y[train_idx]
                # Split train and test set
                df_train = df_filtered.iloc[train_idx]

                if df_train.shape[0] == 1:
                    continue

                dayout = df_filtered.iloc[train_idx + 1].index[-1]
                print('{} - dayone: {}'.format(state, dayone))
                print('{} - dayout: {}'.format(state, dayout))
                dayout = datetime.strptime(dayout, '%d/%m/%Y')

                print('Print inputs/outputs shapes: \n X: {} \n y: {}'.format(
                    X_train.shape, y_train.shape))
                X_train, y_train = utils.check_inputs(X_train, y_train)
                print(
                    'Print inputs/outputs shapes corrected: \n X: {} \n y: {}'.
                    format(X_train.shape, y_train.shape))

                # try:
                #     model = HoltLearner(dayone=dayone, dayout=dayout, date_string_output='%d/%m/%Y')
                #     model.fit(df_train)
                #     df_out = model.predict(forecast=DAYS_TO_PREDICT)
                # except:
                #     error += 1
                #     continue

                try:
                    if model_name == 'holt':
                        model = HoltLearner(dayone=dayone,